Project Euler No. 1 in Emacs elisp

My first Project Euler solution, and my first emacs elisp program.
Multiples of 3 and 5.
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.
My solution:

  (defun divisibleby (dividend divisor)
    "check if dividend is divisible by divisor"
    (if (not(integerp dividend))
        (error "dividend must be an integer"))
    (if (not(integerp divisor))
        (error "divisor must be an integer"))
    (zerop (mod dividend divisor))
    )
(let  ((lower 1)
       (upper 1000)
       (sum 0))
  (loop for i from lower to (- upper 1) do (if (or (divisibleby i 3)
                                              (divisibleby i 5))
                                         (setq sum (+ sum i)))
        )
  (message "sum is: %d" sum)
  )

Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *