All Euler problems
Project Euler

Summation of Primes

Compute sum_(p prime, p < 2,000,000) p.

Source sync Apr 19, 2026
Problem #0010
Level Level 00
Solved By 353,274
Languages C++, Python
Answer 142913828922
Length 428 words
number_theorybrute_forceconstructive

Problem Statement

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

The sum of the primes below \(10\) is \(2 + 3 + 5 + 7 = 17\).

Find the sum of all the primes below two million.

Problem 10: Summation of Primes

Mathematical Development

Sieve of Eratosthenes

Definition 1. The Sieve of Eratosthenes on [0,N)[0, N) is the following procedure:

  1. Initialize a Boolean array A[0N1]A[0 \ldots N-1] with every entry set to true.
  2. Set A[0]=A[1]=falseA[0] = A[1] = \mathrm{false}.
  3. For each integer pp from 22 to N1\lfloor \sqrt{N-1} \rfloor, if A[p]=trueA[p] = \mathrm{true} then mark every multiple p2,p2+p,p2+2p,<Np^2, p^2 + p, p^2 + 2p, \ldots < N as composite by setting the corresponding array entries to false.

Theorem 1 (Sieve correctness). After the sieve terminates, A[i]=trueA[i] = \mathrm{true} if and only if ii is prime, for every integer 0i<N0 \le i < N.

Proof. We prove both directions.

Completeness. Let m[2,N1]m \in [2,N-1] be composite. Then m=abm = ab for some integers 2ab2 \le a \le b. Hence

amN1.a \le \sqrt{m} \le \sqrt{N-1}.

Let pp be the smallest prime factor of mm. Then paN1p \le a \le \sqrt{N-1}. During iteration pp, the sieve marks every multiple of pp starting at p2p^2. Since mm is a multiple of pp and m=p(m/p)p2m = p(m/p) \ge p^2, the entry A[m]A[m] is marked false.

Soundness. Let q[2,N1]q \in [2,N-1] be prime. The only numbers marked during iteration pp are multiples of pp. If p<qp < q, then pqp \nmid q because qq is prime. If p=qp = q, then the marking starts at q2>qq^2 > q, so qq is not marked during its own iteration. Therefore no sieve step ever changes A[q]A[q] from true to false.

The entries A[0]A[0] and A[1]A[1] are explicitly set to false, so the theorem follows. \square

Theorem 2 (Summation correctness). If

S=0i<NA[i]=truei,S = \sum_{\substack{0 \le i < N \\ A[i] = \mathrm{true}}} i,

then

S=p<Np primep.S = \sum_{\substack{p < N \\ p \text{ prime}}} p.

Proof. By Theorem 1, the set of indices ii with A[i]=trueA[i] = \mathrm{true} is exactly the set of primes less than NN. Summing over either description gives the same value. \square

Editorial

We use the Sieve of Eratosthenes to classify all integers below NN as prime or composite. After initializing the table, we traverse prime bases up to N1\sqrt{N-1} and mark their multiples starting at p2p^2, then sum the indices that remain marked. This is sufficient because every composite below NN has a prime factor at most N1\sqrt{N-1}.

Theorem 3 (Algorithm correctness). SumOfPrimesBelow(N) returns the sum of all primes less than NN.

Proof. The sieve phase constructs an array AA satisfying Theorem 1. The final loop sums exactly those indices ii for which A[i]=trueA[i] = \mathrm{true}. By Theorem 2, that sum is precisely the sum of all primes less than NN. \square

Numerical Evaluation

Proposition 1. For N=2,000,000N = 2{,}000{,}000, the algorithm returns

142,913,828,922.142{,}913{,}828{,}922.

Proof. Running the sieve on [0,2,000,000)[0,2{,}000{,}000) identifies exactly 148,933148{,}933 primes in this interval; the largest of them is 1,999,9931{,}999{,}993. Summing all marked prime indices yields

142,913,828,922.142{,}913{,}828{,}922.

By Theorem 3, this is exactly the required sum. \square

Pseudocode

Algorithm: Summation of Primes Below a Bound
Require: An integer N ≥ 2.
Ensure: The sum of all primes less than N.
1: Build a sieve on {2, 3, ..., N - 1}.
2: Mark every composite beginning from p^2 for each prime base p.
3: Sum all indices that remain marked as prime.
4: Return the resulting total.

Complexity Analysis

Theorem 4 (Time complexity). The algorithm runs in O(NloglogN)O(N \log \log N) time.

Proof. The total number of marking operations satisfies

pNp primeNp2p+1NpNp prime1p.\sum_{\substack{p \le \sqrt{N} \\ p \text{ prime}}} \left\lfloor \frac{N-p^2}{p} \right\rfloor + 1 \le N \sum_{\substack{p \le N \\ p \text{ prime}}} \frac{1}{p}.

By Mertens’ second theorem,

px1p=lnlnx+M+O ⁣(1lnx),\sum_{p \le x} \frac{1}{p} = \ln \ln x + M + O\!\left(\frac{1}{\ln x}\right),

where MM is the Meissel-Mertens constant. Hence the sieve phase uses O(NloglogN)O(N \log \log N) time. The final summation pass is linear, so the overall complexity remains O(NloglogN)O(N \log \log N). \square

Theorem 5 (Space complexity). The algorithm uses O(N)O(N) space.

Proof. The Boolean array AA has NN entries. The running sum and loop variables contribute only O(1)O(1) additional space. For N=2,000,000N = 2{,}000{,}000, a 64-bit signed integer is sufficient for the accumulator because

142,913,828,922<263.142{,}913{,}828{,}922 < 2^{63}.

Thus the total space usage is O(N)O(N). \square

Answer

142913828922\boxed{142913828922}

Code

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

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

int main() {
    const int N = 2000000;
    vector<bool> sieve(N, true);
    sieve[0] = sieve[1] = false;
    for (int i = 2; (long long)i * i < N; i++)
        if (sieve[i])
            for (int j = i * i; j < N; j += i)
                sieve[j] = false;
    long long sum = 0;
    for (int i = 2; i < N; i++)
        if (sieve[i])
            sum += i;
    cout << sum << endl;
    return 0;
}