Task

The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n.

Show output for n = 1000.

This is is the same as Project Euler problem 1.

Extra credit: do this efficiently for n = 1e20 or higher.




 
sumdiv(N, D) = S =>
    M = N div D,
    S = (M*(M + 1) div 2) * D.
 
sum35big(N) = sumdiv(N, 3) + sumdiv(N, 5) - sumdiv(N, 15).
 
main =>
    Upto1K = [N: N in 1..999, (N mod 3 = 0; N mod 5 = 0)].sum,
    writef("The sum of all multiples of 3 and 5 below 1000 is %w%n", Upto1K),
    writef("The sum of all multiples less than 1e20 is %w%n", sum35big(99999_99999_99999_99999)).
 