September 7, 2008
at
Sunday, September 07, 2008
Labels:
Computer Science,
Lisp,
Project Euler
Posted by
Billy
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)))
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment