Project Euler in Haskell, Problem 5:

Nov 15, 2009 12:17

I just finished another of the Project Euler problems. This is problem 5:

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?

And this is the solution:


eliminateFactor f [] = []
eliminateFactor f (x:xs) = if (x `mod` f == 0)
then if (x `div` f == 1)
then eliminateFactor f xs
else (x `div` f:eliminateFactor f xs)
else (x:eliminateFactor f xs)

commonFactors [] = []
commonFactors (x:xs) = (x:commonFactors (eliminateFactor x xs))

main = do print (product (commonFactors [2..20]))

Now on to some real work!

euler, haskell

Previous post Next post
Up