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.




var sum35 = Fn.new { |n|
    n = n - 1
    var s3 = (n/3).floor
    var s5 = (n/5).floor
    var s15 = (n/15).floor
    s3 = 3 * s3 * (s3+1)
    s5 = 5 * s5 * (s5+1)
    s15 = 15 * s15 * (s15+1)
    return (s3 + s5 - s15)/2
}
 
System.print(sum35.call(1000))