Determine the smallest positive integer divisible by every integer from 1 to 20. Formally, compute L = lcm(1, 2, 3,..., 20).
Source syncApr 19, 2026
Problem#0005
LevelLevel 00
Solved By525,850
LanguagesC++, Python
Answer232792560
Length541 words
number_theoryarithmetic
Problem statement
Problem Statement
This archive keeps the full statement, math, and original media on the page.
\(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\).
Problem 5: Smallest Multiple
Mathematical Development
Definitions and Notation
Definition 1. For a prime p and a positive integer n, the p-adic valuationvp(n) is the largest exponent e≥0 such that pe∣n. By convention, vp(0)=+∞.
Definition 2. The least common multiple of positive integers a1,…,ak is the smallest positive integer m such that ai∣m for all 1≤i≤k. Equivalently, vp(lcm(a1,…,ak))=maxivp(ai) for every prime p.
Theorem 1 (Prime factorization of the LCM). For any positive integer N,
lcm(1,2,…,N)=p≤Np prime∏p⌊logpN⌋.
Proof. Let L=∏p≤N,p primep⌊logpN⌋. We must show (i) L is a common multiple of {1,…,N}, and (ii) L is minimal among all such common multiples.
(i) L is a common multiple. Let m be any integer with 1≤m≤N, and let p be any prime dividing m. Then pvp(m)∣m≤N, so vp(m)≤logpN, whence vp(m)≤⌊logpN⌋=vp(L). Since this holds for every prime p, we have m∣L.
(ii) L is minimal. Suppose L′ is a common multiple of {1,…,N} with L′<L. Then there exists a prime p with vp(L′)<⌊logpN⌋. Consider the integer q=p⌊logpN⌋. By definition, ⌊logpN⌋ is the largest integer e with pe≤N, so q≤N. Since L′ is a common multiple of {1,…,N}, we require q∣L′, giving vp(L′)≥⌊logpN⌋. This contradicts vp(L′)<⌊logpN⌋. □
Lemma 1 (GCD-LCM identity). For positive integers a and b,
lcm(a,b)=gcd(a,b)a⋅b.
Proof. For every prime p, write α=vp(a) and β=vp(b). Then:
where the second equality uses the identity x+y−min(x,y)=max(x,y), valid for all x,y∈R. Since the p-adic valuations agree for every prime p, the two positive integers are equal. □
Lemma 2 (Iterative reduction). Define L1=1 and Lk=lcm(Lk−1,k) for k=2,3,…,N. Then LN=lcm(1,2,…,N).
Proof. By induction on N.
Base case:L1=1=lcm(1).
Inductive step: Assume Lk−1=lcm(1,…,k−1). Then
Lk=lcm(Lk−1,k)=lcm(lcm(1,…,k−1),k).
Since the LCM operation is associative (which follows from the characterization vp(lcm(a1,…,am))=maxivp(ai)), this equals lcm(1,…,k). □
Theorem 2 (Explicit computation for N=20). We have lcm(1,2,…,20)=232,792,560.
Proof. The primes up to 20 are {2,3,5,7,11,13,17,19}. By Theorem 1,
We build the least common multiple incrementally. Starting from L=1, we traverse k=2,3,…,N, compute g=gcd(L,k), and update L to (L/g)k, which equals lcm(L,k). This is sufficient because after each step the accumulator is the least common multiple of all integers seen so far, so the final value is lcm(1,2,…,N).
Remark. The division by g=gcd(L,k) is exact by Lemma 1, because
lcm(L,k)=gcd(L,k)Lk.
Computing L <- (L / g) * k is therefore mathematically sound and also reduces the risk of overflow in fixed-precision arithmetic.
Proof. Let Lk denote the value stored in L after the iteration with index k. Initially L1=1. At iteration k≥2, the update rule sets
Lk=gcd(Lk−1,k)Lk−1⋅k=lcm(Lk−1,k)
by Lemma 1. By Lemma 2, this implies
Lk=lcm(1,2,…,k)
for every k. In particular, after the final iteration,
LN=lcm(1,2,…,N).
Thus the algorithm is correct. □
Pseudocode
Algorithm: Least Common Multiple of an Initial SegmentRequire: An integer N ≥ 1.Ensure: L = lcm(1, 2, ..., N).1: Initialize L ← 1.2: For each k in {2, 3, ..., N} do:3: Compute g ← gcd(L, k) and update L ← (L / g) · k.4: Return L.
Complexity Analysis
Theorem 4 (Complexity). Assuming machine-word arithmetic is constant time for the values encountered, SmallestMultiple(N) runs in O(NlogN) time and O(1) space.
Proof. The loop executes exactly N−1 iterations. In the k-th iteration, the algorithm computes gcd(Lk−1,k). By the Euclidean algorithm, this requires O(logk) divisions because the running time depends on the smaller argument, namely k.
Therefore the total running time is
k=2∑NO(logk)=O(NlogN).
The algorithm stores only the variables L, g, and k, so the extra space is O(1). For the present problem, N=20 and the final value 232,792,560 fits comfortably in a standard 64-bit integer. □
Answer
232792560
Source code
Code
Each problem page includes the exact C++ and Python source files from the local archive.
C++project_euler/problem_005/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
// Iterative LCM via GCD-LCM identity: lcm(a,b) = a / gcd(a,b) * b
long long L = 1;
for (int k = 2; k <= 20; k++) {
L = L / __gcd(L, (long long)k) * k;
}
cout << L << endl;
return 0;
}
Pythonproject_euler/problem_005/solution.py
"""Project Euler Problem 5: Smallest Multiple"""
import math
from functools import reduce
def solve(n: int = 20) -> int:
"""Compute lcm(1, 2, ..., n) via iterative GCD-LCM identity."""
return reduce(lambda a, b: a * b // math.gcd(a, b), range(1, n + 1))
answer = solve()
assert answer == 232792560
print(answer)