Code
[1] 232792560
0.077 sec elapsed
Vincent Clemson
January 17, 2026
January 24, 2026

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?
https://projecteuler.net/problem=5
To start out, we write a simple program to test numbers from 1 to 11, 1 to 12, and so forth to test runtime feasibility.
e.g.
The runtime begins to become non-trivial at x2=19.
We notice that all answers from x2={11,12,..,18} are multiples of 2520.
Thus, we can increase n incrementally by 2520 instead of incrementally by 1.
[1] 232792560
0.077 sec elapsed
We don’t need much code here.
We need to compute the LCM (Least Common Multiple) of 2520 & a number that is the product of 1-20 using prime factorization.
The LCM is the product of the highest power of each prime. Prime factorization of 1-10 do not have higher powers.
| Multiple | Prime Factorization |
|---|---|
| 11 | 11 |
| 12 | 2^2 * 3 |
| 13 | 13 |
| 14 | 2 * 7 |
| 15 | 3 * 5 |
| 16 | 2^4 |
| 17 | 17 |
| 18 | 2 * 3^2 |
| 19 | 19 |
| 20 | 2^2 * 5 |
\(LCM = 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19\)
2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19[1] 232792560
LCM & Prime Factorization, just code:
l_pf <- primes::prime_factors(1:20)
l_pf_c <- l_pf |> lapply(table)
p <- unique(unlist(l_pf))
d_lcm <- data.frame(p = p)
l_lcm <- c()
for (p_i in p) {
i_p <- lapply(l_pf, function(i) i == p_i) |> lapply(sum) |> unlist() |> which.max()
l_lcm[[as.character(p_i)]] <- l_pf_c[[i_p]][[1]]
}
d_lcm$y <- l_lcm
prod(d_lcm$p ^ unlist(d_lcm$y))[1] 232792560