Jump to content

Sum multiples of 3 and 5: Difference between revisions

Added XPL0 version
(Added XPL0 version)
Line 927:
23333341666668,23333341666668
2333333333333333333416666666666666666668
</pre>
 
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\stdlib;
 
func Sum1; \Return sum the straightforward way
int N, S;
[S:= 0;
for N:= 1 to 999 do
if rem(N/3)=0 or rem(N/5)=0 then S:= S+N;
];
 
func Sum2(D); \Return sum of sequence using N*(N+1)/2
int D;
int Q;
[Q:= (1000-1)/D;
return Q*(Q+1)/2*D;
];
 
string 0; \don't terminate strings by setting most significant bit
func Sum3(D); \Return sum of sequence for really big number
int D; \divisor
int I;
char P(40), Q(40), R(40); \product, quotient, result
[StrNDiv("99999999999999999999", D, Q, 20); \Q:= (1E20-1)/D
for I:= 0 to 17 do R(I):= ^0; \R:= D
R(18):= D/10 +^0;
R(19):= rem(0) +^0;
StrNMul(Q, R, P, 20); \P:= Q*R = Q*D
StrNAdd("00000000000000000001", Q, 20); \Q:= Q+1
StrNMul(P+20, Q, R, 20); \R:= P*Q = Q*D*(Q+1)
StrNDiv(R, 2, Q, 40); \Q:= P/2 = Q*D*(Q+1)/2
return Q; \(very temporary location)
];
 
char S(40), T;
[IntOut(0, Sum1); CrLf(0);
IntOut(0, Sum2(3) + Sum2(5) - Sum2(3*5)); CrLf(0);
StrNCopy(Sum3(3), S, 40);
StrNAdd(Sum3(5), S, 40);
T:= Sum3(3*5);
StrNSub(S, T, 40);
TextN(0, T, 40); CrLf(0);
]</lang>
 
{{out}}
<pre>
233168
233168
2333333333333333333316666666666666666668
</pre>
772

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.