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.




function sumMul(atom n, f)
    n = floor((n-1)/f)
    return f*n*(n+1)/2
end function
 
function sum35(atom n)
    return sumMul(n,3) +
           sumMul(n,5) -
           sumMul(n,15)
end function
 
for i=0 to 8 do
    string sp = repeat(' ',9-i),
           pt = "1"&repeat('0',i)
    printf(1,"%s%s%s %d\n",{sp,pt,sp,sum35(power(10,i))})
end for
