projecteuler_problem1

 15th March 2024 at 2:02pm

Find the sum of all multiples of 3 or 5 below 1000

(import test)

; Integer -> Boolean
; determines whether a number is divisible by 3 or 5
(define (divisible-by-three-or-five n)
  (or (eq? (remainder n 5) 0)
	  (eq? (remainder n 3) 0)))
(test #t (divisible-by-three-or-five 5))
(test #f (divisible-by-three-or-five 7))
 
; Integer, Integer -> Integer
; iterates until n, recursively summing 
; i is the current iteration (starts at 1)
; res is the sum of divisors up to i
(define (iterate-through-n n i res)
  (if (eq? i n)
	res
	(if (divisible-by-three-or-five i)
		(iterate-through-n n (+ i 1) (+ res i))
		(iterate-through-n n (+ i 1) res))))

(test 3 (iterate-through-n 5 1 0))
(test (+ 3 5 6 9) (iterate-through-n 10 1 0))

(define solve-problem-1
  (iterate-through-n 1000 1 0))

(print solve-problem-1)