The idea: count the primes up to x

Write π(x) for the number of primes that are at most x. π(10) = 4 (2, 3, 5, 7), π(30) = 10 and π(100) = 25. As a graph π(x) is a staircase: flat between two primes, one step up at each prime. The steps get further apart as x grows: the primes thin out. The question is how fast.

The staircase π(x) for x from 0 to 30: it steps up by 1 at the primes 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29 and reaches 10 at x = 30. An orange curve x / ln x runs below it and reaches 8.8 at x = 30. The flat stretch from 23 to 29 is marked as a gap of 6.
π(x) climbs by 1 at every prime; x / ln x is a smooth curve that runs a little below it.

The canvas draws the staircase in blue against the smooth guess x / ln x in orange, and below it one dot per prime p at the height of the gap to the next prime. Demo: the first ten primes, one by one walks from 2 to 29; Demo: sweep to 100 draws the whole staircase up to 100.

Thinning out: one number in ln x

Among the numbers near x, roughly 1 in ln x is prime, where ln is the natural logarithm. ln x grows very slowly, so the primes thin out very slowly too. Near 100 about 1 number in 4.6 is prime, near a million still 1 in 13.8. Every time x is multiplied by 10, the chance drops only by adding ln 10 ≈ 2.3 to the "1 in …".

xπ(x)x / ln xπ(x) ÷ (x / ln x)li(x)li(x) − π(x)x / π(x)
1002521.71.15130.15.14.00
1000168144.81.161177.69.65.95
10 00012291085.71.1321246.117.18.14
100 00095928685.91.1049629.837.810.43
1 000 00078 49872 382.41.08478 627.5129.512.74

The last column is the average gap between primes up to x. It grows like ln x − 1 (3.6, 5.9, 8.2, 10.5, 12.8). Demo: zoom out 100 → 10 000 sweeps three ranges in turn: from far away the steps of 1 shrink and the staircase looks like a smooth curve.

The prime number theorem

Legendre and the 15-year-old Gauss guessed it from prime tables around 1792–1798; Hadamard and de la Vallée Poussin proved it in 1896. The prime number theorem says

π(x) ÷ (x / ln x)  →  1      as x → ∞

It does not say that π(x) and x / ln x are close. The difference keeps growing (6000 at a million); only the ratio goes to 1, and it goes very slowly: 1.16 at 1000, 1.08 at a million, still 1.02 at 10²⁰. For every x ≥ 17 the staircase is above x / ln x.

li(x) is a much better guess

If a number t is prime with chance 1 / ln t, then the expected number of primes up to x is the sum of those chances, which is the area under 1 / ln t: the logarithmic integral

li(x) = ∫₀ˣ dt / ln t        li(1000) ≈ 177.6,   li(10⁶) ≈ 78 627.5

x / ln x uses the chance at the top end, 1 / ln x, for every number below x, which is too small; li(x) uses the right chance for each t. So li(x) is off by only 130 at a million where x / ln x is off by 6116 (Demo: up to a million, which turns on both curves; the curve menu under Options does the same). An integral is an area that grows with x: see the Fundamental Theorem of Calculus.

Bar chart of the relative error of the two guesses at x = 100, 1000, 10 000, 100 000 and 1 000 000. x / ln x is 13.1, 13.8, 11.7, 9.4 and 7.8 percent too low; li(x) is 20.5, 5.7, 1.4, 0.39 and 0.17 percent too high.
Both guesses get better in percent, but li(x) is far closer: 0.17 % off at a million against 7.8 %.

li(x) is above π(x) for every x anyone has computed, yet Littlewood proved in 1914 that the two cross infinitely often; the first crossing is somewhere below 10³¹⁷. How small the error li(x) − π(x) stays is exactly the Riemann hypothesis: about √x · ln x if it is true.

Gaps between primes

The gap after a prime p is the distance to the next prime. Since 1 number in ln p is prime, the gaps average about ln p (the dashed purple curve in the gap chart). Single gaps scatter widely around it:

  • Twin primes such as 11, 13 or 9929, 9931 have the smallest possible gap, 2. They keep appearing along the bottom row of dots; whether there are infinitely many is the twin prime conjecture (Zhang proved in 2013 that some gap below 70 million repeats for ever, now lowered to 246).
  • Record gaps are longer than every gap before them (red dots): 8 after 89, 14 after 113, 34 after 1327, 36 after 9551, 72 after 31 397, 114 after 492 113. They grow, but in every case checked they stay below (ln p)² (Cramér's conjecture).
  • Gaps can be as long as you like: n! + 2, n! + 3, …, n! + n are n − 1 composite numbers in a row.
The numbers from 80 to 130 on a line with the primes 83, 89, 97, 101, 103, 107, 109, 113 and 127 marked in blue. The stretch from 89 to 97 is a gap of 8, the twin primes 101 and 103 a gap of 2, and the stretch from 113 to 127 a record gap of 14, marked in red.
Gaps of 2 and a record gap of 14 sit side by side: the average ln p ≈ 4.7 hides big swings.

Demo: record gaps up to 10 000 walks through all eleven records up to 10 000; the Record gaps button does the same for any range.

The primes on this page come from the Sieve of Eratosthenes. Why they are the building blocks: Prime Factorization. The 1 / ln n density shows up as the fading of the dots on the Ulam spiral, and ln itself is on Exponentials and Logarithms.

What the page leaves out

The proof of the prime number theorem (through the zeros of the Riemann zeta function), Riemann's explicit formula and his better guess R(x), the error bounds, primes in arithmetic progressions (Dirichlet), Chebyshev's bias and the Skewes number, ranges beyond a million, and the conjectures on gaps (Cramér, Hardy–Littlewood k-tuples) beyond naming them.

On the mind map

This page on the Number Theory mind map · 19 pages in 5 branches · open it to see the whole map and test yourself in recall mode.