Jump to content

Sum multiples of 3 and 5: Difference between revisions

→‎{{header|Simula}}: Number found to SYSOUT
No edit summary
(→‎{{header|Simula}}: Number found to SYSOUT)
Line 3,007:
20: 100000000000000000000 2333333333333333333316666666666666666668
</pre>
=={{header|Simula}}==
(referenced from [[Greatest common divisor#Simula|Greatest common divisor]])
<lang algol68>! Find the sum of multiples of two factors below a limit -
! Project Euler problem 1: multiples of 3 or 5 below 1000 & 10**20;
BEGIN
INTEGER PROCEDURE GCD(a, b); INTEGER a, b;
GCD := IF b = 0 THEN a ELSE GCD(b, MOD(a, b));
 
! sum of multiples of n up to limit;
INTEGER PROCEDURE multiples(n, limit); INTEGER n, limit;
BEGIN
INTEGER m;
m := limit // n;
! moving //2 to sumMultiples() looked just too silly ;
multiples := n*((m*(m+1)) // 2) ! and risks overflow;
END
! sum of multiples of n or m below limit;
INTEGER PROCEDURE sumMultiples(n, m, limit);
INTEGER n, m, limit;
BEGIN
INTEGER LCM;
LCM:= (n // GCD(n, m)) * m;
limit := limit-1;
sumMultiples := multiples(n, limit) + multiples(m, limit)
- multiples(LCM, limit)
END sumMultiples;
! Extra creditable: math is about avoiding calculation tedium;
TEXT PROCEDURE repeat(c, n); CHARACTER c; INTEGER n; BEGIN
TEXT r; r :- BLANKS(n);
FOR n := n STEP -1 UNTIL 1 DO r.PUTCHAR(c);
repeat :- r;
END;
TEXT PROCEDURE sumOfMultiplesOf3or5below10toThePowerOf(e);
INTEGER e;
sumOfMultiplesOf3or5below10toThePowerOf :-
IF e < 1 THEN "0" ELSE IF e = 1 THEN "23"
ELSE "23" & repeat('3', e-2)
& "1" & repeat('6', e-2) & "8";
 
INTEGER factor, n;
FOR factor := 5 !, 2, 6;
DO BEGIN
OUTTEXT("sum of positive multiples of 3 and");
OUTINT(factor, 2); OUTCHAR(':');
FOR n := ! 1 STEP 1 UNTIL 15, 100,;
1000 DO BEGIN
OUTCHAR(' '); OUTINT(sumMultiples(3, factor, n), 0)
END;
OUTIMAGE
END;
FOR n := 0, 1, 3, 5, 10, 20, 40 DO BEGIN
OUTTEXT(sumOfMultiplesOf3or5below10toThePowerOf(n));
OUTIMAGE
END
END</lang>
{{out}}
sum of positive multiples of 3 and 5: 233168<br />
0<br />
23<br />
233168<br />
2333316668<br />
23333333331666666668<br />
2333333333333333333316666666666666666668<br />
23333333333333333333333333333333333333331666666666666666666666666666666666666668
 
=={{header|Stata}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.