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.




 
GET "libhdr"
 
LET sumdiv(n, d) = VALOF {
    LET m = n/d
    RESULTIS m*(m + 1)/2 * d
}
 
LET sum3or5(n) = sumdiv(n, 3) + sumdiv(n, 5) - sumdiv(n, 15)
 
LET start() = VALOF {
    LET sum = 0
    LET n = 1
 
    FOR k = 1 TO 999 DO
        IF k MOD 3 = 0 | k MOD 5 = 0 THEN sum +:= k
    writef("The sum of the multiples of 3 and 5 < 1000 is %d *n", sum)
 
    writef("Next, the awesome power of inclusion/exclusion...*n");
    FOR i = 1 TO 10 {
        writef("%11d %d *n", n, sum3or5(n - 1))
        n *:= 10
    }
 
    RESULTIS 0
}
 