Prime Gap Distribution
Let g(p) denote the gap to the next prime: g(p) = p' - p, where p' is the smallest prime exceeding p. Find sum_(p <= 10^7, p prime) g(p)^2.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A composition of
For example,
Let
For example,
Find
Problem 929: Prime Gap Distribution
Mathematical Foundation
Theorem 1 (Prime Number Theorem). The number of primes up to satisfies as . Equivalently, the -th prime satisfies .
Proof. (Sketch.) The PNT was proved independently by Hadamard and de la Vallee Poussin in 1896 using the non-vanishing of and contour integration applied to .
Theorem 2 (Average Gap). The average prime gap near is . More precisely, for :
Proof. The gaps telescope: . Dividing by and using PNT: .
Theorem 3 (Sum of Squared Gaps — Heuristic). Under the Hardy—Littlewood prime -tuples conjecture, the sum of squared gaps satisfies:
Proof. (Heuristic.) If gaps near follow an approximate exponential distribution with mean , then and the sum over primes gives . A rigorous proof would require the full strength of the Hardy—Littlewood conjectures.
Lemma 1 (Sieve Correctness). The sieve of Eratosthenes correctly identifies all primes up to in time.
Proof. A composite has a prime factor . The sieve marks as composite when processing . Conversely, a prime is never marked (it has no proper prime factor ). The time bound follows from (Mertens’ theorem).
Lemma 2 (Boundary Handling). To compute for all primes , it suffices to sieve up to .
Proof. By the Baker—Harman—Pintz theorem (2001), the gap for all primes . Hence for , the next prime . In practice, gaps are much smaller (the maximal gap below is ), so sieving to is more than sufficient.
Editorial
Compute the sum of squared prime gaps: S(N) = sum_{p <= N, p prime} (p’ - p)^2, where p’ is the next prime after p, for N = 10^7. We sieve primes up to N + buffer. We then collect primes up to N, plus the next prime after N. Finally, accumulate sum of squared gaps.
Pseudocode
Sieve primes up to N + buffer
Collect primes up to N, plus the next prime after N
Accumulate sum of squared gaps
Complexity Analysis
- Time: for the sieve. The gap accumulation is , dominated by the sieve.
- Space: for the sieve bit array. The prime list requires additional space.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/*
* Problem 929: Prime Gap Distribution
*
* Find sum_{p <= 10^7} g(p)^2 where g(p) = next_prime - p.
*
* Average gap ~ ln(p) ~ 16 near 10^7.
* Max gap below 10^7: ~154.
* Heuristic: sum g^2 ~ 2N ln N.
*
* Cramer's conjecture: g_n = O((ln p_n)^2).
*
* Algorithm: sieve primes to N+1000, iterate consecutive pairs.
* Complexity: O(N log log N) sieve.
*/
int main() {
const int N = 10000000;
const int LIM = N + 1000;
vector<bool> sieve(LIM + 1, true);
sieve[0] = sieve[1] = false;
for (int i = 2; (long long)i * i <= LIM; i++)
if (sieve[i])
for (int j = i * i; j <= LIM; j += i)
sieve[j] = false;
long long total = 0;
int prev = 2;
int max_gap = 0;
for (int i = 3; i <= LIM; i++) {
if (sieve[i]) {
if (prev <= N) {
long long g = i - prev;
total += g * g;
if (g > max_gap) max_gap = g;
}
prev = i;
}
}
cout << total << endl;
// cerr << "Max gap: " << max_gap << endl;
return 0;
}
"""
Problem 929: Prime Gap Distribution
Compute the sum of squared prime gaps: S(N) = sum_{p <= N, p prime} (p' - p)^2,
where p' is the next prime after p, for N = 10^7.
Key results:
- Prime gaps grow roughly as O(log p), but squared gaps amplify large gaps.
- The distribution of gaps is highly structured: even gaps dominate (Goldbach-related).
Methods:
- solve: Sieve of Eratosthenes + iterate consecutive primes, sum gap^2.
- solve_segmented: Same logic, but using bytearray sieve for memory efficiency.
- verify_small: Brute-force for small N to cross-check.
Complexity: O(N log log N) for sieve, O(pi(N)) for gap summation.
"""
from collections import Counter
def solve(N=10**7):
"""Sum of squared prime gaps for primes up to N."""
limit = N + 1000
sieve = bytearray(b'\x01') * (limit + 1)
sieve[0] = sieve[1] = 0
for i in range(2, int(limit**0.5) + 1):
if sieve[i]:
sieve[i*i::i] = bytearray(len(sieve[i*i::i]))
primes = [i for i in range(2, limit + 1) if sieve[i]]
total = 0
for i in range(len(primes) - 1):
if primes[i] > N:
break
gap = primes[i + 1] - primes[i]
total += gap * gap
return total
def verify_small(N):
"""Brute-force sum of squared gaps for small N."""
sieve = bytearray(b'\x01') * (N + 200)
sieve[0] = sieve[1] = 0
for i in range(2, int((N + 200)**0.5) + 1):
if sieve[i]:
sieve[i*i::i] = bytearray(len(sieve[i*i::i]))
primes = [i for i in range(2, N + 200) if sieve[i]]
total = 0
for i in range(len(primes) - 1):
if primes[i] > N:
break
total += (primes[i + 1] - primes[i]) ** 2
return total
def gap_statistics(N):
"""Return list of gaps and basic statistics for primes up to N."""
sieve = bytearray(b'\x01') * (N + 200)
sieve[0] = sieve[1] = 0
for i in range(2, int((N + 200)**0.5) + 1):
if sieve[i]:
sieve[i*i::i] = bytearray(len(sieve[i*i::i]))
primes = [i for i in range(2, N + 200) if sieve[i]]
gaps = []
for i in range(len(primes) - 1):
if primes[i] > N:
break
gaps.append(primes[i + 1] - primes[i])
return primes[:len(gaps)], gaps
# Verify small cases
# Primes up to 20: 2,3,5,7,11,13,17,19 -> gaps: 1,2,2,4,2,4,2
# Sum of squares: 1+4+4+16+4+16+4 = 49
# Primes up to 20: 2,3,5,7,11,13,17,19 -> gaps: 1,2,2,4,2,4,2,4
# Sum of squares: 1+4+4+16+4+16+4+16 = 65
assert verify_small(20) == 65, f"Expected 65, got {verify_small(20)}"
# Primes up to 10: 2,3,5,7 -> gaps: 1,2,2,4 -> 1+4+4+16 = 25
assert verify_small(10) == 25
# Compute answer
answer = solve()
print(answer)