Explore How Many Primes There Are: The Prime Number Theorem

Try adjusting the parameters yourself:

There Are 25 Primes Below 100. How Many Below 10,000?

Let's count: there are 25 primes from 1 to 100, or one quarter of the numbers. Remember, 1 is neither prime nor composite.

From 1 to 1,000, there are 168 primes, or 16.8%; from 1 to 10,000, there are 1,229, or just 12.29%.

As the numbers get larger, primes seem to become sparser. What if we extend the range to a million or a hundred million? Will primes keep becoming less frequent, or eventually disappear?

That is exactly what mathematicians want to know: How many primes are there up to x? Can we find a formula to estimate the count?

Range
Number of primes
Proportion
Below 100
25
25%
Below 1,000
168
16.8%
Below 10,000
1,229
12.29%

Count Them: The Prime Staircase

We write the number of primes no greater than x as π(x), read “pi of x.” It counts the primes from 1 through x.

For example, π(10) = 4, because the primes no greater than 10 are 2, 3, 5, and 7.

Starting at 1 and moving to the right, step up one level whenever you meet a prime. Between primes, the staircase stays flat. This creates the prime staircase shown in the graph.

Primes appear at irregular intervals, so the steps are uneven too. Zoom out, though, and the staircase looks smoother, like a gently rising curve.

Mathematicians ask: Can a simple formula approximate this irregular-looking staircase?

A Fifteen-Year-Old's Conjecture

Around 1792, Gauss, then only fifteen and later known as the “Prince of Mathematicians,” was already studying the distribution of primes. After observing prime tables for years, he began to notice a hidden pattern.

The pattern can be understood this way: near x, roughly 1 in every ln x integers is prime. Here ln x is the natural logarithm, which grows slowly as x increases.

For example, ln 100 is about 4.6; ln 1,000 is about 6.9; and ln 10,000 is about 9.2.

This means that near 100, primes appear on average about every 4.6 numbers; near 1,000, the average gap grows to about 6.9. This is an average pattern, not a fixed spacing.

Primes do become sparser, and the natural logarithm seems to describe how quickly. Extending this local density pattern over the whole interval, Gauss arrived at an estimate: π(x) ≈ x ÷ ln x. The orange curve shows this estimate.

Two Estimates: Which Is More Accurate?

The orange curve follows the prime staircase, but stays a little below it. As x grows, it tracks the overall trend more closely, though a noticeable error remains.

Why? x/ln x uses the prime density near x and roughly treats it as the average density across the whole interval. But prime density is not constant from 2 to x: primes become sparser farther along.

For a finer estimate, we should account for the density at each location. If the prime density near t is about 1/ln t, adding these local densities from 2 to x gives:

Li(x) = ∫₂ˣ dt / ln t

This function is called the logarithmic integral, shown by the blue curve. Instead of using only the density near the endpoint, it accounts for the changing density throughout the interval.

The blue curve follows the staircase more closely than the orange one. By the time we reach hundreds of thousands or a million, it is very close to the actual prime count.

How Large Is the Gap?

Switch to the “Error” view. It shows the actual prime count minus the estimate. A positive error means the estimate is too low; a negative one means it is too high.

At one million, there are 78,498 primes. The estimates are:

Estimate: x/ln x; Result: about 72,382; Error: about +6,116, an underestimate of more than six thousand.

Estimate: Li(x); Result: about 78,627; Error: about −129, an overestimate of about 130.

The difference is clear: x/ln x underestimates by about 7.8%, while Li(x) overestimates by only about 0.16%. The logarithmic integral is a more refined estimate.

Here is another interesting observation: over the range shown, the blue curve stays just above the actual prime staircase. It may look as if it always overestimates, but that is not true forever. We will see a surprising mathematical result later.

Method
Estimate
Difference from actual
Actual count
78,498
-
(x/ln x)
72,382
7.8% low
(Li(x))
78,627
0.16% high

The Prime Number Theorem: A Ratio Tending to 1

Switch to the “Ratio” view. It shows the actual prime count divided by an estimate. If an estimate were exact, this ratio would be 1.

The blue curve quickly approaches 1. The orange curve approaches 1 too, but at one million the actual count is still about 8% higher than x/ln x predicts.

Mathematicians eventually proved that as x tends to infinity, both ratios below tend to 1:

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

π(x) ÷ Li(x) → 1

This is the Prime Number Theorem. Although primes seem chaotic, their overall count across a sufficiently large range can be accurately described by these simple functions.

One detail matters: a ratio tending to 1 does not mean the absolute difference between the actual value and the estimate tends to 0. The absolute gap can still grow with x; it simply becomes a smaller fraction of the total number of primes.

The Prime Number Theorem was independently proved in 1896 by the French mathematician Hadamard and the Belgian mathematician de la Vallée Poussin. One of their key tools was the Riemann zeta function.

How Is This Related to the Riemann Hypothesis?

The Prime Number Theorem tells us that Gauss's estimate is correct overall. But it does not tell us how far the actual prime staircase can stray from the smooth curve.

That is the deeper question addressed by the Riemann Hypothesis. Mathematicians have proved an important result: the Riemann Hypothesis holds if and only if the error between π(x) and Li(x) has an upper bound of order O(√x ln x).

More specifically, this means there is a constant C such that, for sufficiently large x:

|π(x) - Li(x)| ≤ C√x ln x

If the Riemann Hypothesis holds, the actual prime staircase may fluctuate around the smooth curve, but those deviations obey a precise mathematical bound. The Prime Number Theorem describes the overall trend; the Riemann Hypothesis further constrains the fluctuations around it.

These fluctuations are closely connected to the nontrivial zeros of the Riemann zeta function. Imagine many waves of different frequencies combining to shape the details of the prime count. Continue exploring these connections in Prime Staircase Woven by Zeros.

There is another surprising result: in 1914, mathematician Littlewood proved that the relative sizes of π(x) and Li(x) switch infinitely many times.

So although Li(x) stays above π(x) throughout the range we can easily see, far beyond this graph the prime staircase eventually overtakes the blue curve—and they switch places again and again. The first switch occurs far outside the range shown here.

The Riemann Hypothesis remains unproved.

Try It Yourself: Adjust the Graph

Click “Next” below the graph to switch between views. You can also change the parameters below and click “Try.” In addition to exploring the graph, try these experiments.

Experiment 1: Observe the prime staircase. Set the range to 100 and watch the staircase step up at each prime. Then increase the range to 1,000. Notice how the steps change. The gaps between primes are not fixed.

Experiment 2: Compare the estimates. Set the range to 1,000,000 and show both estimate curves. Compare the orange x/ln x and blue Li(x) curves with the actual staircase. Why does adding the prime densities at different locations give a more accurate result?

Experiment 3: Explore errors and ratios. Switch to “Error” to see whether the actual count minus the estimate is positive or negative. Then switch to “Ratio” and see whether the actual count divided by the estimate approaches 1. The absolute error can be large even when the ratio is very close to 1.

Range: The largest x to plot (20 to 1,000,000). A small value makes each step easy to see; one million reveals the overall trend.

View: 0 shows the prime count; 1 shows the error (actual minus estimate); 2 shows the ratio (actual divided by estimate).

Conjecture: 0 shows only Gauss's original x/ln x; 1 shows only the improved Li(x); 2 shows both for comparison. Both curves estimate the prime count.

Animation: 1 draws the graph gradually from left to right; 0 draws it all at once.

You can also drag horizontally with your mouse and zoom with the scroll wheel. Zoom in to see the step for each prime, or zoom out to follow the overall change in prime counts.

Continue Exploring Mathematics

The Prime Number Theorem shows that primes can look chaotic up close yet follow an exceptionally smooth pattern overall. From changing counts along the number line to zeros in the complex plane, mathematicians keep searching for order hidden behind numbers that seem random.

If you enjoy patterns generated by mathematical rules, explore other works such as Julia Set, Mandelbrot Set, and Newton Fractal.

You can also browse more fascinating mathematical figures, or read the Wikipedia article about the Prime Number Theorem.

Further Reading

  • Ulam Prime Spiral A geometric view: why do primes form diagonal lines?
  • Prime Number Theorem A statistical view: how does the number of primes grow?
  • Euler Product An algebraic view: why can primes build the sum of reciprocal powers of all integers?
  • Zeros at Work A complex analysis view: how do the zeros of the zeta function affect the distribution of primes?

Want to see these stunning mathematical visuals every time you open a new tab? Install the Chrome extension now and set "Science Wonder Box" as your new tab page.🚀

Learn More
↑