All Euler problems
Project Euler

Smallest Multiple

Determine the smallest positive integer divisible by every integer from 1 to 20. Formally, compute L = lcm(1, 2, 3,..., 20).

Source sync Apr 19, 2026
Problem #0005
Level Level 00
Solved By 525,850
Languages C++, Python
Answer 232792560
Length 541 words
number_theoryarithmetic

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 pp and a positive integer nn, the pp-adic valuation vp(n)v_p(n) is the largest exponent e0e \ge 0 such that penp^e \mid n. By convention, vp(0)=+v_p(0) = +\infty.

Definition 2. The least common multiple of positive integers a1,,aka_1, \ldots, a_k is the smallest positive integer mm such that aima_i \mid m for all 1ik1 \le i \le k. Equivalently, vp(lcm(a1,,ak))=maxivp(ai)v_p(\operatorname{lcm}(a_1, \ldots, a_k)) = \max_i v_p(a_i) for every prime pp.

Theorem 1 (Prime factorization of the LCM). For any positive integer NN,

lcm(1,2,,N)=pNp primeplogpN.\operatorname{lcm}(1, 2, \ldots, N) = \prod_{\substack{p \le N \\ p \text{ prime}}} p^{\lfloor \log_p N \rfloor}.

Proof. Let L=pN,p primeplogpNL = \prod_{p \le N,\, p \text{ prime}} p^{\lfloor \log_p N \rfloor}. We must show (i) LL is a common multiple of {1,,N}\{1, \ldots, N\}, and (ii) LL is minimal among all such common multiples.

(i) LL is a common multiple. Let mm be any integer with 1mN1 \le m \le N, and let pp be any prime dividing mm. Then pvp(m)mNp^{v_p(m)} \mid m \le N, so vp(m)logpNv_p(m) \le \log_p N, whence vp(m)logpN=vp(L)v_p(m) \le \lfloor \log_p N \rfloor = v_p(L). Since this holds for every prime pp, we have mLm \mid L.

(ii) LL is minimal. Suppose LL' is a common multiple of {1,,N}\{1, \ldots, N\} with L<LL' < L. Then there exists a prime pp with vp(L)<logpNv_p(L') < \lfloor \log_p N \rfloor. Consider the integer q=plogpNq = p^{\lfloor \log_p N \rfloor}. By definition, logpN\lfloor \log_p N \rfloor is the largest integer ee with peNp^e \le N, so qNq \le N. Since LL' is a common multiple of {1,,N}\{1, \ldots, N\}, we require qLq \mid L', giving vp(L)logpNv_p(L') \ge \lfloor \log_p N \rfloor. This contradicts vp(L)<logpNv_p(L') < \lfloor \log_p N \rfloor. \square

Lemma 1 (GCD-LCM identity). For positive integers aa and bb,

lcm(a,b)=abgcd(a,b).\operatorname{lcm}(a, b) = \frac{a \cdot b}{\gcd(a, b)}.

Proof. For every prime pp, write α=vp(a)\alpha = v_p(a) and β=vp(b)\beta = v_p(b). Then:

vp ⁣(abgcd(a,b))=α+βmin(α,β)=max(α,β)=vp(lcm(a,b)),v_p\!\left(\frac{ab}{\gcd(a,b)}\right) = \alpha + \beta - \min(\alpha, \beta) = \max(\alpha, \beta) = v_p(\operatorname{lcm}(a,b)),

where the second equality uses the identity x+ymin(x,y)=max(x,y)x + y - \min(x,y) = \max(x,y), valid for all x,yRx, y \in \mathbb{R}. Since the pp-adic valuations agree for every prime pp, the two positive integers are equal. \square

Lemma 2 (Iterative reduction). Define L1=1L_1 = 1 and Lk=lcm(Lk1,k)L_k = \operatorname{lcm}(L_{k-1}, k) for k=2,3,,Nk = 2, 3, \ldots, N. Then LN=lcm(1,2,,N)L_N = \operatorname{lcm}(1, 2, \ldots, N).

Proof. By induction on NN.

Base case: L1=1=lcm(1)L_1 = 1 = \operatorname{lcm}(1).

Inductive step: Assume Lk1=lcm(1,,k1)L_{k-1} = \operatorname{lcm}(1, \ldots, k-1). Then

Lk=lcm(Lk1,k)=lcm ⁣(lcm(1,,k1),k).L_k = \operatorname{lcm}(L_{k-1}, k) = \operatorname{lcm}\!\bigl(\operatorname{lcm}(1, \ldots, k-1),\, k\bigr).

Since the LCM operation is associative (which follows from the characterization vp(lcm(a1,,am))=maxivp(ai)v_p(\operatorname{lcm}(a_1, \ldots, a_m)) = \max_i v_p(a_i)), this equals lcm(1,,k)\operatorname{lcm}(1, \ldots, k). \square

Theorem 2 (Explicit computation for N=20N = 20). We have lcm(1,2,,20)=232,792,560\operatorname{lcm}(1, 2, \ldots, 20) = 232{,}792{,}560.

Proof. The primes up to 20 are {2,3,5,7,11,13,17,19}\{2, 3, 5, 7, 11, 13, 17, 19\}. By Theorem 1,

L=p20plogp20.L = \prod_{p \le 20} p^{\lfloor \log_p 20 \rfloor}.

We compute each prime power:

Prime pplogp20\lfloor \log_p 20 \rfloorJustificationplogp20p^{\lfloor \log_p 20 \rfloor}
2424=1620<32=252^4 = 16 \le 20 < 32 = 2^516
3232=920<27=333^2 = 9 \le 20 < 27 = 3^39
5151=520<25=525^1 = 5 \le 20 < 25 = 5^25
7171=720<49=727^1 = 7 \le 20 < 49 = 7^27
111111=1120<121=11211^1 = 11 \le 20 < 121 = 11^211
131131=1320<169=13213^1 = 13 \le 20 < 169 = 13^213
171171=1720<289=17217^1 = 17 \le 20 < 289 = 17^217
191191=1920<361=19219^1 = 19 \le 20 < 361 = 19^219

Therefore:

L=16×9×5×7×11×13×17×19.L = 16 \times 9 \times 5 \times 7 \times 11 \times 13 \times 17 \times 19.

Step-by-step evaluation:

16×9=144,144×5=720,720×7=5,040,16 \times 9 = 144, \quad 144 \times 5 = 720, \quad 720 \times 7 = 5{,}040, 5,040×11=55,440,55,440×13=720,720,5{,}040 \times 11 = 55{,}440, \quad 55{,}440 \times 13 = 720{,}720, 720,720×17=12,252,240,12,252,240×19=232,792,560.720{,}720 \times 17 = 12{,}252{,}240, \quad 12{,}252{,}240 \times 19 = 232{,}792{,}560. \quad\square

Editorial

We build the least common multiple incrementally. Starting from L=1L = 1, we traverse k=2,3,,Nk = 2, 3, \ldots, N, compute g=gcd(L,k)g = \gcd(L, k), and update LL to (L/g)k(L / g)k, which equals lcm(L,k)\operatorname{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)\operatorname{lcm}(1, 2, \ldots, N).

Remark. The division by g=gcd(L,k)g = \gcd(L,k) is exact by Lemma 1, because

lcm(L,k)=Lkgcd(L,k).\operatorname{lcm}(L,k) = \frac{Lk}{\gcd(L,k)}.

Computing L <- (L / g) * k is therefore mathematically sound and also reduces the risk of overflow in fixed-precision arithmetic.

Theorem 3 (Algorithm correctness). SmallestMultiple(N) returns lcm(1,2,,N)\operatorname{lcm}(1,2,\ldots,N).

Proof. Let LkL_k denote the value stored in L after the iteration with index kk. Initially L1=1L_1 = 1. At iteration k2k \ge 2, the update rule sets

Lk=Lk1kgcd(Lk1,k)=lcm(Lk1,k)L_k = \frac{L_{k-1} \cdot k}{\gcd(L_{k-1}, k)} = \operatorname{lcm}(L_{k-1}, k)

by Lemma 1. By Lemma 2, this implies

Lk=lcm(1,2,,k)L_k = \operatorname{lcm}(1,2,\ldots,k)

for every kk. In particular, after the final iteration,

LN=lcm(1,2,,N).L_N = \operatorname{lcm}(1,2,\ldots,N).

Thus the algorithm is correct. \square

Pseudocode

Algorithm: Least Common Multiple of an Initial Segment
Require: 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)O(N \log N) time and O(1)O(1) space.

Proof. The loop executes exactly N1N-1 iterations. In the kk-th iteration, the algorithm computes gcd(Lk1,k)\gcd(L_{k-1}, k). By the Euclidean algorithm, this requires O(logk)O(\log k) divisions because the running time depends on the smaller argument, namely kk.

Therefore the total running time is

k=2NO(logk)=O(NlogN).\sum_{k=2}^{N} O(\log k) = O(N \log N).

The algorithm stores only the variables L, g, and k, so the extra space is O(1)O(1). For the present problem, N=20N = 20 and the final value 232,792,560232{,}792{,}560 fits comfortably in a standard 64-bit integer. \square

Answer

232792560\boxed{232792560}

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;
}