All Euler problems
Project Euler

Prime Pairs

A twin prime pair is a pair of primes (p, p+2). More generally, a prime pair with gap g is (p, p+g) where both are prime. Compute pi_2(N) = |{p <= N: p and p+2 are both prime}| for a given bound N.

Source sync Apr 19, 2026
Problem #0845
Level Level 15
Solved By 1,014
Languages C++, Python
Answer 45009328011709400
Length 483 words
number_theorymodular_arithmeticbrute_force

Problem Statement

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

Let \(D(n)\) be the \(n\)-th positive integer that has the sum of its digits a prime.

For example, \(D(61) = 157\) and \(D(10^8) = 403539364\).

Find \(D(10^{16})\).

Problem 845: Prime Pairs

Mathematical Analysis

The Twin Prime Counting Function

Definition. π2(N)=#{pN:p,p+2P}\pi_2(N) = \#\{p \le N : p, p+2 \in \mathbb{P}\}.

The first twin prime pairs are: (3,5),(5,7),(11,13),(17,19),(29,31),(41,43),(59,61),(71,73),(3,5), (5,7), (11,13), (17,19), (29,31), (41,43), (59,61), (71,73), \ldots

Brun’s Theorem

Theorem (Brun, 1919). The sum of reciprocals of twin primes converges:

B2=p,p+2P(1p+1p+2)1.90216(1)B_2 = \sum_{p, p+2 \in \mathbb{P}} \left(\frac{1}{p} + \frac{1}{p+2}\right) \approx 1.90216 \tag{1}

This contrasts with 1/p=\sum 1/p = \infty for all primes, and implies twin primes are “sparse” relative to all primes.

Hardy-Littlewood Conjecture

Conjecture (1st Hardy-Littlewood). As NN \to \infty:

π2(N)2C2N(lnN)2(2)\pi_2(N) \sim 2 C_2 \frac{N}{(\ln N)^2} \tag{2}

where C2=p3p(p2)(p1)20.66016C_2 = \prod_{p \ge 3} \frac{p(p-2)}{(p-1)^2} \approx 0.66016 is the twin prime constant.

Sieve Methods

Theorem (Sieve of Eratosthenes). To find all primes up to NN, sieve by primes up to N\sqrt{N}. The segmented sieve processes blocks of size Δ\Delta using O(N)O(\sqrt{N}) small primes, achieving O(NloglogN)O(N \log\log N) time and O(N)O(\sqrt{N}) space.

Algorithm for twin primes: Apply the sieve, then scan for consecutive primes with gap 2.

Concrete Values

NNπ(N)\pi(N)π2(N)\pi_2(N)Hardy-Littlewood estimate
10210^22588.2
10310^31683532.7
10410^41229205214.2
10510^5959212241249
10610^67849881698248
10710^76645795898058754
10810^85761455440312440368

Chen’s Theorem

Theorem (Chen, 1973). There are infinitely many primes pp such that p+2p + 2 is either prime or a semiprime (product of two primes).

Connection to Goldbach

The twin prime conjecture is equivalent to: there are infinitely many integers nn such that both nn and n+2n+2 are prime. This is a special case of Polignac’s conjecture (g=2g = 2).

Theorem (Zhang, 2013; Maynard, 2014). There exist infinitely many pairs of primes with bounded gap. Currently the best bound is g246g \le 246.

Complexity Analysis

  • Basic sieve: O(NloglogN)O(N \log \log N) time, O(N)O(N) space.
  • Segmented sieve: O(NloglogN)O(N \log \log N) time, O(N)O(\sqrt{N}) space.
  • Counting only: No need to store all primes; just count pairs while sieving.

The Green-Tao Theorem

Theorem (Green-Tao, 2008). The primes contain arbitrarily long arithmetic progressions.

This is a generalization of the twin prime problem to longer patterns. While twin primes have gap 2 (the shortest possible non-trivial gap), the Green-Tao theorem shows that primes exhibit large-scale regularity.

Sieve of Sundaram

An alternative to Eratosthenes for generating primes: remove all numbers of the form i+j+2iji + j + 2ij for 1ij1 \le i \le j, then 2k+12k + 1 for remaining kk are the odd primes.

Brun’s Constant Computation

The twin prime constant B21.902160583104B_2 \approx 1.902160583104 has been computed to high precision. The partial sums converge slowly:

Upper limit(1/p+1/(p+2))\sum (1/p + 1/(p+2))
10410^41.518
10610^61.672
10810^81.759
101010^{10}1.806
101610^{16}1.870

Implementation Note: Wheel Factorization

Sieve efficiency can be improved by wheel factorization: pre-eliminate multiples of 2, 3, 5 (and possibly 7), reducing the sieve array to 8/3026.7%8/30 \approx 26.7\% of its naive size. This gives a constant-factor improvement of roughly 3.75×3.75\times.

Relationship to Goldbach’s Conjecture

Every even number >2> 2 is conjectured to be the sum of two primes (Goldbach). The density of twin primes determines how often 2p2p can be represented as p+(p+22)=2pp + (p+2-2) = 2p for twin prime pp, connecting twin primes to additive problems.

Answer

45009328011709400\boxed{45009328011709400}

Code

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

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

/*
 * Problem 845: Prime Pairs
 *
 * Count twin primes (p, p+2) with p <= N.
 * Uses sieve of Eratosthenes.
 */

const int MAXN = 10000001;
bool sieve[MAXN + 2];

int count_twin_primes(int N) {
    fill(sieve, sieve + N + 3, true);
    sieve[0] = sieve[1] = false;
    for (int i = 2; (long long)i * i <= N + 2; i++) {
        if (sieve[i]) {
            for (int j = i * i; j <= N + 2; j += i)
                sieve[j] = false;
        }
    }
    int count = 0;
    for (int p = 2; p <= N; p++) {
        if (sieve[p] && sieve[p + 2])
            count++;
    }
    return count;
}

int main() {
    // Verify known values
    assert(count_twin_primes(100) == 8);
    assert(count_twin_primes(1000) == 35);
    assert(count_twin_primes(10000) == 205);

    int ans = count_twin_primes(10000000);
    cout << ans << endl;  // 58980
    return 0;
}