Sum multiples of 3 and 5: Difference between revisions

Added zkl
(Added zkl)
Line 1,672:
233168
2333333333333333333316666666666666666668
</pre>
 
=={{header|zkl}}==
Brute force:
<lang zkl>[3..999,3].reduce('+,0) + [5..999,5].reduce('+,0) - [15..999,15].reduce('+,0)
233168</lang>
{{trans|Groovy}}
Using a formula, making sure the input will cast the result to the same type (ie if called with a BigNum, the result is a BigNum).
<lang zkl>fcn sumMul(N,m){N=(N-1)/m; N*(N+1)*m/2}
fcn sum35(N){sumMul(N,3) + sumMul(N,5) - sumMul(N,15)}</lang>
{{out}}
<pre>
zkl: sum35(1000) // int-->int
233168
 
zkl: var BN=Import("zklBigNum");
zkl: sum35(BN("1"+"0"*21)) // 1 with 21 zeros, BigNum-->BigNum
233333333333333333333166666666666666666668
sum35(BN("1"+"0"*15)) : "%,d".fmt(_)// 1e15, BigNum don't like float format input
233,333,333,333,333,166,666,666,666,668
</pre>
Anonymous user