TExcelTAD
Home › Number Theory & Cryptography › How Many Primes? The Prime Number Theorem

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 xx, roughly one number in ln⁡x\ln x is prime.

The counting function

Let π(x)\pi(x) be the number of primes p≤xp \le x. (Here π\pi is a function name, not 3.14…) The prime number theorem (Hadamard and de la Vallée Poussin, 1896) states

π(x)∼xln⁡x,that is,lim⁡x→∞π(x)x/ln⁡x=1\pi(x) \sim \frac{x}{\ln x}, \qquad \text{that is,} \qquad \lim_{x \to \infty} \frac{\pi(x)}{x / \ln x} = 1

A much better approximation is the logarithmic integral li⁡(x)=∫2xdtln⁡t\operatorname{li}(x) = \int_2^x \frac{dt}{\ln t} (offset form), which says the "probability" that a number near tt is prime is 1/ln⁡t1/\ln t.

python · try it yourself
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

x/ln⁡xx / \ln x undercounts by about 6–15 % in this range, while li⁡(x)\operatorname{li}(x) is off by a few hundred at most. The error of li⁡(x)\operatorname{li}(x) is governed by the Riemann hypothesis, the most famous open problem in mathematics.

Density of primes of a given size

Among kk-bit numbers (those in [2k−1,2k)[2^{k-1}, 2^k)), the fraction that are prime is about 1/ln⁡2k=1/(kln⁡2)1/\ln 2^k = 1/(k \ln 2). For k=1024k = 1024 that is about 1/7101/710, and restricting to odd candidates doubles the rate to about 1/3551/355.

python · try it yourself
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 gcd⁡(a,n)=1\gcd(a, n) = 1, there are infinitely many primes p≡a(modn)p \equiv a \pmod{n}, and they are evenly spread over the φ(n)\varphi(n) allowed residues. For instance about half the odd primes are ≡1(mod4)\equiv 1 \pmod 4 and half ≡3(mod4)\equiv 3 \pmod 4.

python · try it yourself
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 210132^{1013} 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 101210^{12} using x/ln⁡xx / \ln x.

Answer

About 3.6⋅10103.6 \cdot 10^{10}.

Solution

ln⁡1012=12ln⁡10≈27.63\ln 10^{12} = 12 \ln 10 \approx 27.63, and 1012/27.63≈3.62⋅101010^{12} / 27.63 \approx 3.62 \cdot 10^{10}. The true value is π(1012)=37 607 912 018\pi(10^{12}) = 37\,607\,912\,018, about 4 % more.

Exercise 2: Average gap

What is the average gap between consecutive primes near 1010010^{100}?

Answer

About ln⁡10100≈230\ln 10^{100} \approx 230.

Solution

If the density near xx is 1/ln⁡x1/\ln x, primes are spaced ln⁡x\ln x apart on average: 100ln⁡10≈230.3100 \ln 10 \approx 230.3.

Exercise 3: Count a window (code)

Count the primes in [109,109+105)[10^9, 10^9 + 10^5) and compare with 105/ln⁡10910^5 / \ln 10^9.

Answer

Both are close to 4800.

Solution
python · try it yourself
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 π(x)\pi(x) is asymptotic to…

  1. x\sqrt{x}
  2. x/ln⁡xx / \ln x
  3. ln⁡x\ln x
  4. x/2x / 2

Roughly what fraction of 1024-bit odd numbers are prime?

  1. 1 in 2
  2. 1 in 355
  3. 1 in 25122^{512}
  4. 1 in 1024

Dirichlet's theorem guarantees infinitely many primes p≡a(modn)p \equiv a \pmod{n} when…

  1. aa is prime
  2. gcd⁡(a,n)=1\gcd(a, n) = 1
  3. nn is prime
  4. a<n/2a < n/2