Sum multiples of 3 and 5: Difference between revisions

Added Lua version
m (→‎version 3: added/changed whitespace and comments.)
(Added Lua version)
Line 1,058:
The sum of multiples of 3 or 5 between 1 and 9999 is: 23331668
The sum of multiples of 3 or 5 between 1 and 99999 is: 2333316668</pre>
 
=={{header|Lua}}==
{{trans|Tcl}}
<lang Lua>
function tri (n) return n * (n + 1) / 2 end
 
function sum35 (n)
n = n - 1
return ( 3 * tri(math.floor(n / 3)) +
5 * tri(math.floor(n / 5)) -
15 * tri(math.floor(n / 15))
)
end
 
print(sum35(1000))
print(sum35(1e+20))
</lang>
{{out}}
<pre>
233168
2.3333333333333e+39
</pre>
 
=={{header|Maple}}==
Anonymous user