Sum multiples of 3 and 5: Difference between revisions

Line 8:
{{out}}
<pre>233168</pre>
 
=={{header|AWK}}==
Save this into file "sum_multiples_of3and5.awk"
<lang AWK>#!/usr/bin/awk -f
{
n = $1-1;
print sum(n,3)+sum(n,5)-sum(n,15);
}
function sum(n,d) {
m = int(n/d);
return (d*m*(m+1)/2);
}</lang>
Output
<pre>$ echo -e '1000\n1e20' |awk -f sum_multiples_of3and5.awk
233168
2333333333333332940795175780693005303808</pre>
 
== {{header|BASIC}} ==
Anonymous user