How Many Primes? The Prime Number Theorem
Cryptography needs primes with hundreds of digits. Are there enough of them, and how long will a random search take? The prime number theorem answers both: near , roughly one number in is prime.
The counting function
Let be the number of primes . (Here is a function name, not 3.14…) The prime number theorem (Hadamard and de la Vallée Poussin, 1896) states
A much better approximation is the logarithmic integral (offset form), which says the "probability" that a number near is prime is .
import numpy as np
N = 10**7
is_p = np.ones(N + 1, dtype=bool); is_p[:2] = False
for p in range(2, int(N**0.5) + 1):
if is_p[p]:
is_p[p * p :: p] = False
counts = np.cumsum(is_p)
def li(x, steps=200000):
t = np.linspace(2, x, steps)
y = 1 / np.log(t)
return float(np.sum((y[1:] + y[:-1]) / 2 * np.diff(t)))
for x in (10**3, 10**4, 10**5, 10**6, 10**7):
print(f"x={x:>9} pi={int(counts[x]):>7} x/ln x={x / np.log(x):>10.1f} li={li(x):>10.1f}")x= 1000 pi= 168 x/ln x= 144.8 li= 176.6
x= 10000 pi= 1229 x/ln x= 1085.7 li= 1245.1
x= 100000 pi= 9592 x/ln x= 8685.9 li= 9628.8
x= 1000000 pi= 78498 x/ln x= 72382.4 li= 78627.8
x= 10000000 pi= 664579 x/ln x= 620420.7 li= 664942.1
undercounts by about 6–15 % in this range, while is off by a few hundred at most. The error of is governed by the Riemann hypothesis, the most famous open problem in mathematics.
Density of primes of a given size
Among -bit numbers (those in ), the fraction that are prime is about . For that is about , and restricting to odd candidates doubles the rate to about .
import math
for k in (64, 256, 512, 1024, 2048):
density = 1 / (k * math.log(2))
print(f"{k:5}-bit: about 1 in {1 / density:7.1f} numbers is prime; 1 in {1 / (2 * density):6.1f} odd numbers") 64-bit: about 1 in 44.4 numbers is prime; 1 in 22.2 odd numbers
256-bit: about 1 in 177.4 numbers is prime; 1 in 88.7 odd numbers
512-bit: about 1 in 354.9 numbers is prime; 1 in 177.4 odd numbers
1024-bit: about 1 in 709.8 numbers is prime; 1 in 354.9 odd numbers
2048-bit: about 1 in 1419.6 numbers is prime; 1 in 709.8 odd numbers
So a random search for a 1024-bit prime tests a few hundred odd candidates on average, each with a fast probabilistic test: well under a second.
Primes in arithmetic progressions
Dirichlet's theorem: if , there are infinitely many primes , and they are evenly spread over the allowed residues. For instance about half the odd primes are and half .
from sympy import primerange
from collections import Counter
ps = list(primerange(3, 10**6))
print(Counter(p % 4 for p in ps))
print(sorted(Counter(p % 10 for p in ps).items()))Counter({3: 39322, 1: 39175})
[(1, 19617), (3, 19665), (5, 1), (7, 19621), (9, 19593)]
In software
- Key generators use the density estimate to size their search loops and to budget time; OpenSSL first sieves out candidates divisible by small primes, which raises the hit rate further.
- The theorem also guarantees there are vastly more primes than could ever be stored: there are roughly primes of 1024 bits, so two independent key generators will practically never pick the same one, unless their randomness is broken.
Exercises
Exercise 1: Estimate
Estimate how many primes there are below using .
Answer
About .
Solution
, and . The true value is , about 4 % more.
Exercise 2: Average gap
What is the average gap between consecutive primes near ?
Answer
About .
Solution
If the density near is , primes are spaced apart on average: .
Exercise 3: Count a window (code)
Count the primes in and compare with .
Answer
Both are close to 4800.
Solution
import math
from sympy import primerange
L = 10**9
print(len(list(primerange(L, L + 10**5))), round(10**5 / math.log(L), 1))4832 4825.5
Test yourself
The prime number theorem says is asymptotic to…
Roughly what fraction of 1024-bit odd numbers are prime?
- 1 in 2
- 1 in 355
- 1 in
- 1 in 1024
Dirichlet's theorem guarantees infinitely many primes when…
- is prime
- is prime