#lang scheme ; If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. ; The sum of these multiples is 23. ; Find the sum of all the multiples of 3 or 5 below 1000. (define sum 0) ; sum of the multiples of 3 or 5 below 1000 ; for i = 3 to 999 (define (findSumOfMultiples i) (cond [(< i 1000) ; test all values less than 1000 (cond [ ; if i is a mod value of 0 in mod 3 or 5 (meaning i is a multiple of 3 or 5) (or (= (modulo i 3) 0) (= (modulo i 5) 0)) (set! sum (+ sum i)) ; add i to the sum ] ) (findSumOfMultiples (+ i 1)) ; call function again with incremented parameter value ] [else sum] ; hit 1000, so the sum of the multiples needs to be returned ) ) ; call the function (findSumOfMultiples 3)DOWNLOAD
And call it first with the parameter 3
because that's the least possible multiple of 3 or 5.
Created: September 6, 2014
Completed in full by: Michael Yaworski