All Euler problems
Project Euler

Multiples of 3 and 5

Find the sum of all the multiples of 3 or 5 below 1000. That is, compute S = sum_(1 <= k < 1000, 3 | k or 5 | k) k.

Source sync Apr 19, 2026
Problem #0001
Level Level 00
Solved By 1,035,865
Languages C++, Python
Answer 233168
Length 478 words
number_theoryarithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

If we list all the natural numbers below \(10\) that are multiples of \(3\) or \(5\), we get \(3\), \(5\), \(6\) and \(9\). The sum of these multiples is \(23\). Find the sum of all the multiples of \(3\) or \(5\) below \(1000\).

Problem 1: Multiples of 3 and 5

Mathematical Development

Preliminaries

Definition 1. For a positive integer mm and a positive integer NN, define the set of positive multiples of mm strictly below NN as

Am(N)={kZ+:mk and k<N}={m,2m,3m,,pm}A_m(N) = \{k \in \mathbb{Z}^+ : m \mid k \text{ and } k < N\} = \{m, 2m, 3m, \ldots, pm\}

where p=(N1)/mp = \lfloor (N-1)/m \rfloor.

Definition 2. For a finite set XZX \subset \mathbb{Z}, define the sum function σ(X)=xXx\sigma(X) = \sum_{x \in X} x.

The Arithmetic Series Identity

Theorem 1 (Sum of the First nn Natural Numbers). For every positive integer nn,

k=1nk=n(n+1)2.\sum_{k=1}^{n} k = \frac{n(n+1)}{2}.

Proof. We proceed by induction on nn.

Base case. For n=1n = 1: k=11k=1=122\sum_{k=1}^{1} k = 1 = \frac{1 \cdot 2}{2}. The identity holds.

Inductive step. Assume the identity holds for some n1n \ge 1. Then

k=1n+1k=(k=1nk)+(n+1)=n(n+1)2+(n+1)=(n+1)(n2+1)=(n+1)(n+2)2.\sum_{k=1}^{n+1} k = \left(\sum_{k=1}^{n} k\right) + (n+1) = \frac{n(n+1)}{2} + (n+1) = (n+1)\left(\frac{n}{2} + 1\right) = \frac{(n+1)(n+2)}{2}.

This is the identity with nn replaced by n+1n+1. By the principle of mathematical induction, the identity holds for all n1n \ge 1. \square

Corollary 1 (Sum of Multiples). Let m,Nm, N be positive integers with m1m \ge 1 and N2N \ge 2. Then

σ(Am(N))=mp(p+1)2,where p=N1m.\sigma(A_m(N)) = m \cdot \frac{p(p+1)}{2}, \quad \text{where } p = \left\lfloor \frac{N-1}{m} \right\rfloor.

Proof. The elements of Am(N)A_m(N) are exactly m,2m,,pmm, 2m, \ldots, pm. Factoring:

σ(Am(N))=j=1pjm=mj=1pj=mp(p+1)2\sigma(A_m(N)) = \sum_{j=1}^{p} jm = m \sum_{j=1}^{p} j = m \cdot \frac{p(p+1)}{2}

where the last equality follows from Theorem 1. \square

Inclusion-Exclusion

Theorem 2 (Inclusion-Exclusion Principle for Sums over Two Sets). Let AA and BB be finite subsets of Z\mathbb{Z}. Then

σ(AB)=σ(A)+σ(B)σ(AB).\sigma(A \cup B) = \sigma(A) + \sigma(B) - \sigma(A \cap B).

Proof. Partition ABA \cup B into three pairwise disjoint sets:

AB=(AB)    (BA)    (AB).A \cup B = (A \setminus B) \;\sqcup\; (B \setminus A) \;\sqcup\; (A \cap B).

Since these are disjoint, additivity of summation gives

σ(AB)=σ(AB)+σ(BA)+σ(AB).(1)\sigma(A \cup B) = \sigma(A \setminus B) + \sigma(B \setminus A) + \sigma(A \cap B). \tag{1}

Similarly, A=(AB)(AB)A = (A \setminus B) \sqcup (A \cap B), so σ(A)=σ(AB)+σ(AB)\sigma(A) = \sigma(A \setminus B) + \sigma(A \cap B), whence

σ(AB)=σ(A)σ(AB).(2)\sigma(A \setminus B) = \sigma(A) - \sigma(A \cap B). \tag{2}

Likewise, σ(BA)=σ(B)σ(AB)\sigma(B \setminus A) = \sigma(B) - \sigma(A \cap B). Substituting into (1):

σ(AB)=[σ(A)σ(AB)]+[σ(B)σ(AB)]+σ(AB)=σ(A)+σ(B)σ(AB).\sigma(A \cup B) = [\sigma(A) - \sigma(A \cap B)] + [\sigma(B) - \sigma(A \cap B)] + \sigma(A \cap B) = \sigma(A) + \sigma(B) - \sigma(A \cap B). \quad \square

Characterizing the Intersection

Lemma 1 (Intersection of Multiples via LCM). For positive integers m1,m2m_1, m_2 and N2N \ge 2,

Am1(N)Am2(N)=Alcm(m1,m2)(N).A_{m_1}(N) \cap A_{m_2}(N) = A_{\operatorname{lcm}(m_1, m_2)}(N).

Proof. Let =lcm(m1,m2)\ell = \operatorname{lcm}(m_1, m_2). We show set equality by double inclusion.

(\supseteq) If kA(N)k \in A_\ell(N), then k\ell \mid k and k<Nk < N. Since m1m_1 \mid \ell and m2m_2 \mid \ell, we have m1km_1 \mid k and m2km_2 \mid k, so kAm1(N)Am2(N)k \in A_{m_1}(N) \cap A_{m_2}(N).

(\subseteq) If kAm1(N)Am2(N)k \in A_{m_1}(N) \cap A_{m_2}(N), then m1km_1 \mid k and m2km_2 \mid k and k<Nk < N. By the universal property of the least common multiple, m1km_1 \mid k and m2km_2 \mid k implies k\ell \mid k. Hence kA(N)k \in A_\ell(N). \square

Corollary 2. Since gcd(3,5)=1\gcd(3, 5) = 1, we have lcm(3,5)=35/gcd(3,5)=15\operatorname{lcm}(3, 5) = 3 \cdot 5 / \gcd(3,5) = 15. Therefore A3(N)A5(N)=A15(N)A_3(N) \cap A_5(N) = A_{15}(N).

Main Result

Theorem 3 (Closed-Form Solution). The sum of all positive multiples of 3 or 5 below NN is

S(N)=σ(A3(N)A5(N))=3p3(p3+1)2+5p5(p5+1)215p15(p15+1)2S(N) = \sigma(A_3(N) \cup A_5(N)) = 3 \cdot \frac{p_3(p_3+1)}{2} + 5 \cdot \frac{p_5(p_5+1)}{2} - 15 \cdot \frac{p_{15}(p_{15}+1)}{2}

where pm=(N1)/mp_m = \lfloor (N-1)/m \rfloor for m{3,5,15}m \in \{3, 5, 15\}.

Proof. Apply Theorem 2 with A=A3(N)A = A_3(N) and B=A5(N)B = A_5(N):

σ(A3(N)A5(N))=σ(A3(N))+σ(A5(N))σ(A3(N)A5(N)).\sigma(A_3(N) \cup A_5(N)) = \sigma(A_3(N)) + \sigma(A_5(N)) - \sigma(A_3(N) \cap A_5(N)).

By Corollary 2, A3(N)A5(N)=A15(N)A_3(N) \cap A_5(N) = A_{15}(N). Then Corollary 1 gives

σ(A3(N))=3p3(p3+1)2,σ(A5(N))=5p5(p5+1)2,σ(A15(N))=15p15(p15+1)2,\sigma(A_3(N)) = 3 \cdot \frac{p_3(p_3+1)}{2}, \qquad \sigma(A_5(N)) = 5 \cdot \frac{p_5(p_5+1)}{2}, \qquad \sigma(A_{15}(N)) = 15 \cdot \frac{p_{15}(p_{15}+1)}{2},

where pm=(N1)/mp_m = \lfloor (N-1)/m \rfloor. Substituting these three identities yields the stated formula. \square

Numerical Evaluation

Proposition 1. S(1000)=233168S(1000) = 233168.

Proof. We compute each component:

  • p3=999/3=333p_3 = \lfloor 999/3 \rfloor = 333, so σ(A3)=33333342=355611=166833\sigma(A_3) = 3 \cdot \frac{333 \cdot 334}{2} = 3 \cdot 55611 = 166833.
  • p5=999/5=199p_5 = \lfloor 999/5 \rfloor = 199, so σ(A5)=51992002=519900=99500\sigma(A_5) = 5 \cdot \frac{199 \cdot 200}{2} = 5 \cdot 19900 = 99500.
  • p15=999/15=66p_{15} = \lfloor 999/15 \rfloor = 66, so σ(A15)=1566672=152211=33165\sigma(A_{15}) = 15 \cdot \frac{66 \cdot 67}{2} = 15 \cdot 2211 = 33165.

Therefore S(1000)=166833+9950033165=233168S(1000) = 166833 + 99500 - 33165 = 233168. \square

Editorial

We use the closed-form arithmetic-series formula rather than scanning every integer. The procedure counts how many multiples of 3, 5, and 15 lie below the limit, converts each count into the corresponding sum of multiples, and then applies inclusion-exclusion to subtract the overlap. This is sufficient because every qualifying integer is a multiple of 3 or 5, and the only double-counted terms are multiples of 15.

Theorem 4 (Algorithm Correctness). SumMultiples(N) returns σ(A3(N)A5(N))\sigma(A_3(N) \cup A_5(N)) for all N2N \ge 2.

Proof. The algorithm computes exactly the three integers

p3=N13,p5=N15,p15=N115,p_3 = \left\lfloor \frac{N-1}{3} \right\rfloor, \qquad p_5 = \left\lfloor \frac{N-1}{5} \right\rfloor, \qquad p_{15} = \left\lfloor \frac{N-1}{15} \right\rfloor,

and then returns

3p3(p3+1)2+5p5(p5+1)215p15(p15+1)2.3 \cdot \frac{p_3(p_3+1)}{2} + 5 \cdot \frac{p_5(p_5+1)}{2} - 15 \cdot \frac{p_{15}(p_{15}+1)}{2}.

By Theorem 3, this quantity is precisely σ(A3(N)A5(N))\sigma(A_3(N) \cup A_5(N)). Therefore the algorithm is correct. \square

Pseudocode

Algorithm: Sum of Multiples Below a Bound
Require: An integer N ≥ 2.
Ensure: The sum S of all positive integers below N that are divisible by 3 or 5.
1: For each modulus m in {3, 5, 15}, compute p_m ← floor((N - 1) / m), the number of positive multiples of m below N.
2: For each such m, evaluate T_m ← m · p_m(p_m + 1) / 2.
3: Form S ← T_3 + T_5 - T_15 by inclusion-exclusion.
4: Return S.

Complexity Analysis

Theorem 5. SumMultiples(N) runs in O(1)O(1) time and O(1)O(1) space.

Proof. The algorithm performs exactly 3 floor divisions, 3 additions, 6 multiplications, 3 divisions by 2, 1 addition, and 1 subtraction. This is a fixed number of arithmetic operations independent of NN. The algorithm stores exactly 6 intermediate values (p3,p5,p15p_3, p_5, p_{15} and the three partial sums), which is O(1)O(1) space.

Remark. A brute-force approach testing each integer in {1,2,,N1}\{1, 2, \ldots, N-1\} for divisibility by 3 or 5 requires Θ(N)\Theta(N) time and O(1)O(1) space. The closed-form solution removes the scan entirely and reduces the running time to Θ(1)\Theta(1). \square

Answer

233168\boxed{233168}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_001/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    auto S = [](long long m, long long N) -> long long {
        long long p = (N - 1) / m;
        return m * p * (p + 1) / 2;
    };
    cout << S(3, 1000) + S(5, 1000) - S(15, 1000) << endl;
    return 0;
}