Primes

Counting primes

How many primes are there up to a trillion? Listing them would take 37 billion numbers. Counting them without listing them takes about a second.

Legendre's formula

Let φ(x,a)\varphi(x, a) be the number of n≤xn \le x not divisible by any of the first aa primes. Removing the multiples of the aa-th prime from the numbers counted by φ(x,a−1)\varphi(x, a-1) gives the recurrence

φ(x,a)=φ(x,a−1)−φ ⁣(⌊xpa⌋,a−1),φ(x,0)=⌊x⌋, \varphi(x, a) = \varphi(x, a - 1) - \varphi\!\left(\left\lfloor \tfrac{x}{p_a} \right\rfloor, a - 1\right), \qquad \varphi(x, 0) = \lfloor x \rfloor,

which is inclusion and exclusion, organised. If a=π(x)a = \pi(\sqrt x), the numbers counted are 1 and the primes between x\sqrt x and xx, so π(x)=φ(x,a)+a−1\pi(x) = \varphi(x, a) + a - 1.

import { legendrePhi, primePi, meissel } from "@ratmath/numtheory";

legendrePhi(100, 3);     // 26n: the n ≤ 100 coprime to 30
legendrePhi(100, 4);     // 22n: 1 and the 21 primes from 11 to 97
22n + 4n - 1n;           // 25n = π(100)

Meissel's improvement

The recurrence has too many terms when aa is as large as π(x)\pi(\sqrt x). Meissel took a=π(x3)a = \pi(\sqrt[3]{x}) instead. Then φ(x,a)\varphi(x, a) counts 1, the primes between x3\sqrt[3]{x} and xx, and the products of exactly two such primes; no product of three fits below xx. Hence

π(x)=φ(x,a)+a−1−P2(x,a),P2(x,a)=∑x3<p≤x(π ⁣(xp)−π(p)+1). \pi(x) = \varphi(x, a) + a - 1 - P_2(x, a), \qquad P_2(x, a) = \sum_{\sqrt[3]{x} < p \le \sqrt{x}} \Big( \pi\!\left(\tfrac{x}{p}\right) - \pi(p) + 1 \Big).

The sum needs π\pi only up to x2/3x^{2/3}, which a sieve provides.

meissel(1000);
// { pi: 168n, y: 10n, a: 4n, phi: 228n, P2: 63n }:   228 + 4 − 1 − 63 = 168
meissel(10n ** 6n);
// { pi: 78498n, y: 100n, a: 25n, phi: 120760n, P2: 42286n }

primePi(10n ** 9n);      // 50847534n
primePi(10n ** 12n);     // 37607912018n, in about a second

primePi sieves below 10710^7 and uses this formula above, up to 101210^{12}.

What makes it fast

  • A table for the first primes. φ(x,6)\varphi(x, 6) is periodic in xx with period 30030=2⋅3⋯1330030 = 2 \cdot 3 \cdots 13, so it is a table lookup, and the recurrence starts at the seventh prime.
  • Stopping early. When x<pa2x < p_a^2, the numbers counted by φ(x,a)\varphi(x, a) are 1 and primes, so φ(x,a)=π(x)−a+1\varphi(x, a) = \pi(x) - a + 1, another lookup.
  • A compact sieve. The sieve up to x2/3x^{2/3} keeps one bit for each odd number and a running count for every 32 bits, so π(m)\pi(m) at any mm costs one addition and one bit count. At x=1012x = 10^{12} that is 6 megabytes.

Lehmer, and later Lagarias, Miller and Odlyzko, pushed the same idea further (more correction terms, and a sieve in place of the recurrence); the record computations of π(1027)\pi(10^{27}) and beyond descend from it. Compare the result with the estimates: π(1012)=37607912018\pi(10^{12}) = 37607912018, while x/ln⁡xx/\ln x gives 3.62×10103.62 \times 10^{10} and the logarithmic integral li⁡(x)\operatorname{li}(x) gives 3760795028137607950281, off by 38263.

Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs