Project Euler Problem 5
You can solve this by creating a loop that increments by 20 (20, 40 60, etc.) and then see if every increment is divisible by all the numbers from 1 to 19. You can improve this even further by not bothering to see if every increment is divisible by 1 to 10 because if the increment is divisible by 20, 18, 16, and 14 then it's divisible by all the numbers from 1 to 10. And finally you can improve it even further by incrementing by a number equal to 20 * 19 * 17 * 13 * 11 which is 923780. What is special about 19, 17, 13, and 11 is that they are prime numbers. So that just leaves incrementing by 923780 and seeing if the increment is divisible by 18, 16, 15, 14, and 12.
(defconstant +inc+ (* 20 19 17 13 11))
(defun problem5 ()
(loop for i upfrom +inc+ by +inc+
if (every #'(lambda (j) (zerop (mod i j)))
'(18 16 15 14 12))
return i))
Another solution inspired by Haskell.
(lcm 20 19 18 17 16 15 14 13 12 11)
No comments:
Post a Comment