Project Euler Q5 :: Smallest multiple

Explanation. Standard caveat: don’t look here if you are trying to do these yourself.

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?

I’m getting the feeling that brute-force is going to be quite the useful tool for these questions. Thankfully R can churn through numbers really fast.

So, we’re after a number divisible by 1, 2, 3, …, 10. Let’s vectorise that and check the stated answer

[code language=“r”] all(2520 %% 1:10 == 0) # TRUE [/code]

Easy enough. The solution value must be divisible by 20, so we can just test multiples of 20 for the above property

[code language=“r”] i <- 20 y <- FALSE while(!y) { i <- i + 20 y <- all(i %% 1:20 == 0) } i # 232792560



Wrapping a system.time() call around that assures us that this is still done in under a minute, as per the guidelines

[code language=“r”] user system elapsed 26.150 0.000 26.192 [/code]


See also