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

all(2520 %% 1:10 == 0) # TRUE

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

i <- 20
y <- FALSE
while(!y) {
  i <- i + 20
  y <- all(i %% 1:20 == 0)
}
i # 232792560

### CORRECT

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

   user  system elapsed 
 26.150   0.000  26.192 

Leave a Reply