Thought Toys · Chance & inference · Exhibit 63

The gaps are chaos. The count is law.

No formula tells you how far the next prime is from this one — it could be 2 away, it could be 50. And yet the running count of primes below x tracks the smooth curve x/ln(x) closer and closer as x grows, with no exceptions. Drag x from a thousand to a million and watch both truths at once.

Gaps between the 30 primes just above x average gap ln(x)

π(x): x/ln(x): ratio:

your turn — drag x toward a million

What you're seeing

Primes thin out as numbers grow, but not smoothly. Walk along the number line near some large x and the gaps between consecutive primes jump around with no visible rhythm: a gap of 2 (a twin prime pair) can sit two doors down from a gap of 50 with nothing in between to warn you which is coming. Nobody has a formula that predicts the next one — the top chart is the actual gaps near your chosen x, and they scatter well above and below their own average with no sign of settling down, however far out you drag the dial.

And yet zoom out to the count — how many primes exist below x, written π(x) — and a law appears with no exceptions: π(x) divided by x/ln(x) drifts toward exactly 1 as x grows. It's a slow drift, honestly — even at a million it's still about 8% high, a reminder that "the limit is 1" is a statement about forever, not about being close by a million. Compare that to the naive guess that "half of all whole numbers are prime": that ratio doesn't just miss, it gets worse the farther out you go. One law converges. The other doesn't. The same primes produce both stories, depending only on whether you ask about the next one or about all of them — local unpredictability underneath global regularity.

The rule, exactly. The prime number theorem (Hadamard and de la Vallée Poussin, 1896): limx→∞ π(x) / (x / ln x) = 1 where π(x) counts primes ≤ x. Verified in node (improve/verify/63-prime-number-theorem.js): the sieve behind every number on this page is cross-checked against independent trial division on 3,000 random integers up to 1,000,000 (zero mismatches); π(x)/(x/ln x) measurably closes in on 1 from 1.16 at x=1,000 to 1.08 at x=1,000,000, while the naive π(x)/(x/2) guess falls from 0.34 to 0.16 over the same range — negative control: getting worse, not better. Separately, the 300 primes surrounding x=500,000 have gaps averaging 12.98 (ln(500,000)=13.12, matching the law) but ranging from 2 to 52 with a coefficient of variation of 0.71 — nowhere near the near-zero variation a genuinely regular, evenly-spaced control sequence shows at the same average spacing — proof the local unpredictability is real, not an illusion of the chart.

Also in Chance & inference: The Galton board →

All 16 in Chance & inference
  1. 05The Galton board
  2. 06The Monty Hall problem
  3. 09Bayes' theorem
  4. 13Buffon's needle
  5. 14The central limit theorem
  6. 28Simpson's paradox
  7. 30Markov chains
  8. 31Averages that never settle
  9. 35The birthday paradox
  10. 38The drunkard's walk
  11. 39Zipf's law
  12. 40Benford's law
  13. 42The coupon collector's problem
  14. 48The wisdom of crowds
  15. 52Genetic drift
  16. 63The gaps are chaos. The count is law. — you are here

← the cabinet · Thought Toys — a cabinet of explorable explanations. Exhibit 63.