본문 바로가기

problem solving/Project Euler in Haskell

ProjectEuler Problem 5 - Smallest multiple

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:


by pancil and paper