Sum multiples of 3 and 5: Difference between revisions

Added Algol 68
(Lingo added)
(Added Algol 68)
Line 9:
'''Extra credit:''' do this efficiently for ''n'' = 1e20 or higher.
<br><br>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT to handle large numbers.
<lang algol68># returns the sum of the multiples of 3 and 5 below n #
PROC sum of multiples of 3 and 5 below = ( LONG LONG INT n )LONG LONG INT:
BEGIN
# calculate the sum of the multiples of 3 below n #
LONG LONG INT multiples of 3 = ( n - 1 ) OVER 3;
LONG LONG INT multiples of 5 = ( n - 1 ) OVER 5;
LONG LONG INT multiples of 15 = ( n - 1 ) OVER 15;
( # twice the sum of multiples of 3 #
( 3 * multiples of 3 * ( multiples of 3 + 1 ) )
# plus twice the sum of multiples of 5 #
+ ( 5 * multiples of 5 * ( multiples of 5 + 1 ) )
# less twice the sum of multiples of 15 #
- ( 15 * multiples of 15 * ( multiples of 15 + 1 ) )
) OVER 2
END # sum of multiples of 3 and 5 below # ;
 
print( ( "Sum of multiples of 3 and 5 below 1000: "
, whole( sum of multiples of 3 and 5 below( 1000 ), 0 )
, newline
)
);
print( ( "Sum of multiples of 3 and 5 below 1e20: "
, whole( sum of multiples of 3 and 5 below( 100 000 000 000 000 000 000 ), 0 )
, newline
)
)</lang>
{{out}}
<pre>
Sum of multiples of 3 and 5 below 1000: 233168
Sum of multiples of 3 and 5 below 1e20: 2333333333333333333316666666666666666668
</pre>
 
== {{header|APL}} ==
Line 15 ⟶ 50:
{{out}}
<pre>233168</pre>
 
 
== {{header|AppleScript}} ==
3,044

edits