Prime Counting Function Calculator (π(x))

Count how many primes lie below any number with the prime counting function π(x): exact sieve counts plus the x/ln(x) and logarithmic integral estimates.

Prime Counting Result

Counting the primes below a number

The prime counting function, written π(x), answers a simple question with a surprisingly deep answer: how many prime numbers are there up to x? For small numbers you can just count. π(10) = 4, because the primes up to 10 are 2, 3, 5, and 7. π(100) = 25. There is no neat closed formula that spits out the exact count, and computing it directly for very large x means sieving through enormous ranges of integers.

Two ways this calculator answers

For x up to a few million, it sieves out every prime and gives you the exact π(x). Past that, exact counting is too slow to run in a browser, so it falls back on the famous approximations and labels the result as an estimate.

The prime number theorem

π(x) ≈ x / ln(x)

This is one of the landmark results in mathematics, proved independently by Hadamard and de la Vallée Poussin in 1896. It says primes thin out at a predictable rate: near a number x, roughly one in every ln(x) integers is prime. At x = 1,000,000, ln(x) is about 13.8, so about one in fourteen numbers in that neighborhood is prime. The estimate always runs a little low, but the ratio of the true count to x/ln(x) creeps toward 1 as x grows.

A much closer estimate

π(x) ≈ Li(x) = ∫ from 2 to x of dt / ln(t)

The logarithmic integral, an idea Carl Friedrich Gauss had as a teenager, tracks π(x) far more tightly than x/ln(x). For x near a billion it is off by only a few thousand out of fifty million primes. This calculator computes Li(x) by numerical integration so you can see how good the fit is against the exact count.

Why anyone cares

Cryptography leans on the fact that primes are common enough to find by random search yet spread out enough that factoring is hard. Knowing roughly how many primes sit below a key size tells you how big the haystack is. The function also sits at the center of the Riemann hypothesis, which is, in effect, a precise statement about how closely Li(x) shadows π(x). A proof would pin down the error term in the prime number theorem once and for all.


Embed This Calculator

Copy the code below and paste it into your website or blog.
The calculator will work directly on your page.