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.




 
needs combinators/bi
 
: mul3or5?  ( 3 mod 0 = ) ( 5 mod 0 = ) bi or ;
 
"The sum of the multiples of 3 or 5 below 1000 is " .
0 ( mul3or5? if I n:+ then ) 1 999 loop . cr
 
with: n
 
: >triangular SED: n -- n
    dup 1+ * 2 / ;
 
: sumdiv SED: n n -- n
    dup >r /mod nip >triangular r> * ;
 
: sumdiv_3,5 SED: n -- n
    ( swap sumdiv ) curry [3, 5, 15] swap a:map a:open neg + + ;
 
;with
 
"For 10^20 - 1, the sum is " .  10 20 ^ 1- sumdiv_3,5 . cr
bye
 