Inputs
Any integer.
π(x) = #{p ≤ x : p prime}, and π(x) = 0 for x < 2. The count is exact: it is computed by Lehmer's method without listing the primes up to x. Computation runs locally in your browser.
Result
π(x) = —
—
- Method
- —
- Prime list up to
- —
- Direct π table up to
- —
- Elapsed
- —
Lehmer's ingredients for this x
Prime number theorem comparison
Approximations, not exact values. The exact count is shown above; the two formulas below only estimate it. Li(x) = ∫₂ˣ dt / ln t (the offset logarithmic integral, starting at 2 — not the principal-value li(x) that starts at 0).
| Value | Error vs. π(x) | Relative error | |
|---|---|---|---|
| Exact π(x) | — | — | — |
| x / ln x (approximation) | — | — | — |
| Li(x) (approximation) | — | — | — |
To list the primes themselves rather than only count them, use Primes in a Range.
How it works
The prime-counting function π(x) counts the primes p ≤ x. For small x the calculator builds a prime table up to x and reads the answer off it. For larger x it uses Lehmer's algorithm, which combines a partial sieve function φ(x, a) with recursive prime counts, so π(x) is obtained without generating every prime below x. Only what the algorithm needs is generated: the primes up to √x, and a bit-packed π lookup table (up to about x^(2/3), at most 2 × 10⁸) that answers small π(y) instantly.
Write p₁ = 2, p₂ = 3, … and let
Then Lehmer's formula is
with every quotient rounded down. φ is evaluated with a lookup table for the first seven primes and the recurrence φ(x, a) = φ(x, a−1) − φ(⌊x/p_a⌋, a−1) beyond that. Everything is exact integer arithmetic. The values π(x/p_i) that fall above the lookup table are counted for i > c in one pass of a segmented sieve up to about x^(2/3), and recursively by the same formula otherwise. Long computations are shared between several background workers, whose partial sums are added at the end. Requests whose predicted work or memory exceeds the calculator's browser limits (currently roughly x ≳ 7 × 10¹⁴) are refused rather than started.