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 positive number that is evenly divisible by all of the numbers from 1 to 20?
This problem is asking what the LCM(least common multiple) of [1..20] is.
Haskell provide lcm function in builtin though it is easy to implement.
We can compute LCM of more than two numbers by recursively applying the LCM of two numbers:
And we can do this kind of calculation in very neat way in Haskell by using fold.
One of the fold functions foldl is implemented like this:
So this problem can be solved by:
'problem solving > Project Euler in Haskell' 카테고리의 다른 글
ProjectEuler Problem 6 - Sum square difference (0) | 2014.12.08 |
---|---|
ProjectEuler Problem 4 - Largest palindrome product (0) | 2014.12.03 |
ProjectEuler Problem 3 - Largest prime factor (0) | 2014.12.03 |
ProjectEuler Problem 2 - Even Fibonacci numbers (0) | 2014.12.01 |
ProjectEuler Problem 1 - Multiples of 3 and 5 (0) | 2014.12.01 |