Sum multiples of 3 and 5: Difference between revisions

→‎{{header|Julia}}: removed extra version, not needed
(→‎{{header|Julia}}: removed extra version, not needed)
Line 864:
 
=={{header|Julia}}==
Simple version: sums multiples of each, minus multiples of the least common multiple (lcm).
Limits: bound to values and results below 2^63 (Int64)
<lang Julia>multsum(n, m, lim) = sum(0:n:lim-1) + sum(0:m:lim-1) - sum(0:lcm(n,m):lim-1)</lang>
Output:
<pre>julia> multsum(3, 5, 1000)
233168</pre>
a more generic version, cabable of handling large integers (BigInt)
<lang Julia>multsum(n, lim) = (occ = div(lim-1, n); div(n*occ*(occ+1), 2))
multsum(n, m, lim) = multsum(n, lim) + multsum(m, lim) - multsum(lcm(n,m), lim)</lang>
Output:
<pre>julia> @time multsum(3, 5, 1000)
elapsed time: 5.366e-6 seconds (96 bytes allocated)
233168
 
<pre>julia> @time multsum(3, 5, 1000BigInt(10)^20)
2333333333333333333316666666666666666668
 
julia> @time multsum(3, 5, BigInt(10)^20)
elapsed time: 35.9082e8114e-5 seconds seconds (18643968 bytes allocated)
2333333333333333333316666666666666666668</pre>
 
Anonymous user