\(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 for the least common multiple of the first twenty natural numbers. We can deduce this without any programming by writing out their unique prime factorizations:
\[ \begin{aligned} 1 &= 1 \\ 2 &= 2 \\ 3 &= 3 \\ 4 &= 2^2 \\ 5 &= 5 \\ 6 &= 2 \cdot 3 \\ 7 &= 7 \\ 8 &= 2^3 \\ 9 &= 3^2 \\ 10 &= 2 \cdot 5 \\ 11 &= 11 \\ 12 &= 2^2 \cdot 3 \\ 13 &= 13 \\ 14 &= 2 \cdot 7 \\ 15 &= 3 \cdot 5 \\ 16 &= 2^4 \\ 17 &= 17 \\ 18 &= 2 \cdot 3^2 \\ 19 &= 19 \\ 20 &= 2^2 \cdot 5 \end{aligned} \]We can find the least common multiple by considering each prime, taking the largest power of that prime among all these factorizations, and multiplying those powers together. This turns out to be \(2^4 \cdot 3^2 \cdot 5 \cdot 7 \cdot 11 \cdot 13 \cdot 17 \cdot 19 = 232792560\).
For general \(n\), we can traverse up the list of integers, multiplying our answer by powers of primes as we go. For each prime \(p\), we can identify the largest power of \(p\) which is at most \(n\): this will be \(p^{\lfloor \log_p n \rfloor}\). To save time and energy, I use SymPy's function for generating primes; an approach to finding primes manually is covered in my writeup of problem 7.
from math import log
from sympy import primerange
def p5(n=20):
res = 1
for p in primerange(n):
if res % p != 0: # p is prime
res *= p ** int(log(n, p))
return res