September 7, 2008

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?


Here is a quick and simple solution in Common Lisp:

(defun simple-euler-5 ()
(lcm 11 12 13 14 15 16 17 18 19 20))


However, coding our own solution isn't too difficult. Using Euclid's algorithm for the greatest common divisor, here is another solution is Common Lisp:

(defun range (start end)
"Generates a list of integers from start to end"
(loop for i from start to end collect i))

(defun my-lcm (lst)
(let ((result 1))
(dolist (n lst)
(setq result (/ (* result n) (gcd result n))))
result))

(defun euler-5 ()
(my-lcm (range 11 20)))

0 comments: