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 syncApr 19, 2026
Problem#0001
LevelLevel 00
Solved By1,035,865
LanguagesC++, Python
Answer233168
Length478 words
number_theoryarithmetic
Problem statement
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 m and a positive integer N, define the set of positive multiples of m strictly below N as
Am(N)={k∈Z+:m∣k and k<N}={m,2m,3m,…,pm}
where p=⌊(N−1)/m⌋.
Definition 2. For a finite set X⊂Z, define the sum function σ(X)=∑x∈Xx.
The Arithmetic Series Identity
Theorem 1 (Sum of the First n Natural Numbers). For every positive integer n,
k=1∑nk=2n(n+1).
Proof. We proceed by induction on n.
Base case. For n=1: ∑k=11k=1=21⋅2. The identity holds.
Inductive step. Assume the identity holds for some n≥1. Then
Lemma 1 (Intersection of Multiples via LCM). For positive integers m1,m2 and N≥2,
Am1(N)∩Am2(N)=Alcm(m1,m2)(N).
Proof. Let ℓ=lcm(m1,m2). We show set equality by double inclusion.
(⊇) If k∈Aℓ(N), then ℓ∣k and k<N. Since m1∣ℓ and m2∣ℓ, we have m1∣k and m2∣k, so k∈Am1(N)∩Am2(N).
(⊆) If k∈Am1(N)∩Am2(N), then m1∣k and m2∣k and k<N. By the universal property of the least common multiple, m1∣k and m2∣k implies ℓ∣k. Hence k∈Aℓ(N). □
Corollary 2. Since gcd(3,5)=1, we have lcm(3,5)=3⋅5/gcd(3,5)=15. Therefore A3(N)∩A5(N)=A15(N).
Main Result
Theorem 3 (Closed-Form Solution). The sum of all positive multiples of 3 or 5 below N is
where pm=⌊(N−1)/m⌋. Substituting these three identities yields the stated formula. □
Numerical Evaluation
Proposition 1.S(1000)=233168.
Proof. We compute each component:
p3=⌊999/3⌋=333, so σ(A3)=3⋅2333⋅334=3⋅55611=166833.
p5=⌊999/5⌋=199, so σ(A5)=5⋅2199⋅200=5⋅19900=99500.
p15=⌊999/15⌋=66, so σ(A15)=15⋅266⋅67=15⋅2211=33165.
Therefore S(1000)=166833+99500−33165=233168. □
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)) for all N≥2.
Proof. The algorithm computes exactly the three integers
p3=⌊3N−1⌋,p5=⌊5N−1⌋,p15=⌊15N−1⌋,
and then returns
3⋅2p3(p3+1)+5⋅2p5(p5+1)−15⋅2p15(p15+1).
By Theorem 3, this quantity is precisely σ(A3(N)∪A5(N)). Therefore the algorithm is correct. □
Pseudocode
Algorithm: Sum of Multiples Below a BoundRequire: 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) time and 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 N. The algorithm stores exactly 6 intermediate values (p3,p5,p15 and the three partial sums), which is O(1) space.
Remark. A brute-force approach testing each integer in {1,2,…,N−1} for divisibility by 3 or 5 requires Θ(N) time and O(1) space. The closed-form solution removes the scan entirely and reduces the running time to Θ(1). □
Answer
233168
Source code
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;
}
Pythonproject_euler/problem_001/solution.py
"""Problem 1: Multiples of 3 and 5"""
def sum_multiples(m: int, n: int) -> int:
p = (n - 1) // m
return m * p * (p + 1) // 2
def solve(n: int = 1000) -> int:
return sum_multiples(3, n) + sum_multiples(5, n) - sum_multiples(15, n)
answer = solve()
assert answer == 233168
print(answer)