Summation of Primes
Compute sum_(p prime, p < 2,000,000) p.
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 is the following procedure:
- Initialize a Boolean array with every entry set to
true. - Set .
- For each integer from to , if then mark every multiple
as composite by setting the corresponding array entries to
false.
Theorem 1 (Sieve correctness). After the sieve terminates, if and only if is prime, for every integer .
Proof. We prove both directions.
Completeness. Let be composite. Then for some integers . Hence
Let be the smallest prime factor of . Then . During iteration , the sieve marks every multiple of starting at . Since is a multiple of and , the entry is marked false.
Soundness. Let be prime. The only numbers marked during iteration are multiples of . If , then because is prime. If , then the marking starts at , so is not marked during its own iteration. Therefore no sieve step ever changes from true to false.
The entries and are explicitly set to false, so the theorem follows.
Theorem 2 (Summation correctness). If
then
Proof. By Theorem 1, the set of indices with is exactly the set of primes less than . Summing over either description gives the same value.
Editorial
We use the Sieve of Eratosthenes to classify all integers below as prime or composite. After initializing the table, we traverse prime bases up to and mark their multiples starting at , then sum the indices that remain marked. This is sufficient because every composite below has a prime factor at most .
Theorem 3 (Algorithm correctness). SumOfPrimesBelow(N) returns the sum of all primes less than .
Proof. The sieve phase constructs an array satisfying Theorem 1. The final loop sums exactly those indices for which . By Theorem 2, that sum is precisely the sum of all primes less than .
Numerical Evaluation
Proposition 1. For , the algorithm returns
Proof. Running the sieve on identifies exactly primes in this interval; the largest of them is . Summing all marked prime indices yields
By Theorem 3, this is exactly the required sum.
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 time.
Proof. The total number of marking operations satisfies
By Mertens’ second theorem,
where is the Meissel-Mertens constant. Hence the sieve phase uses time. The final summation pass is linear, so the overall complexity remains .
Theorem 5 (Space complexity). The algorithm uses space.
Proof. The Boolean array has entries. The running sum and loop variables contribute only additional space. For , a 64-bit signed integer is sufficient for the accumulator because
Thus the total space usage is .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
def sieve_sum(limit):
is_prime = bytearray(b'\x01') * limit
is_prime[0] = is_prime[1] = 0
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
is_prime[i*i::i] = bytearray(len(is_prime[i*i::i]))
return sum(i for i in range(2, limit) if is_prime[i])
print(sieve_sum(2_000_000))