Search

Search papers and lessons

Prime Number Theorem

Level 3 · 11 min read

The Prime Number Theorem says that the number of primes up to x is roughly x divided by the natural log of x, and that the rough estimate gets better as x grows.

On this page

  • Colour = topic mix
  • Discrete math
  • Calculus
  • ReadReady to readNot yet

TL;DR

Primes look random, but counted in bulk they follow a clean rule. The Prime Number Theorem says that the number of primes up to xx is, for large xx, close to x/ln⁡xx / \ln x, where ln⁡\ln is the natural logarithm. The ratio of the true count to that estimate approaches 1 as xx grows, so the estimate becomes proportionally accurate even though the primes themselves never follow a pattern.

Builds on: Prime Number, Logarithm, Limit of a Function · Leads to: Primes in arithmetic progressions, the Bombieri-Vinogradov theorem

Where it's used

Generating cryptographic keys. The RSA cryptosystem, a form of public-key cryptography, needs two random primes that are each hundreds of digits long. Its security rests on how hard it is to factor the product of those two primes. The theorem tells you how common such primes are. Near a number NN, about one integer in ln⁡N\ln N is prime. For a 1024-bit number, ln⁡N\ln N is about 710, so roughly 1 in 355 odd numbers of that size is prime. A key generator therefore tests a few hundred random odd candidates on average before it finds one.

Estimating table and search sizes. Before running a sieve, a programmer can estimate how many primes a table will hold. The number of primes up to one million is 78,498. The estimate 106/ln⁡10610^6 / \ln 10^6 is about 72,400, so the first guess is off by about 8 percent. That is close enough to size a buffer, which is all the estimate is needed for.

Analysing the gaps between primes. Because primes near xx have density about 1/ln⁡x1 / \ln x, the typical gap between consecutive primes near xx is about ln⁡x\ln x. Number theorists use this average spacing as the baseline when they study how primes are spread across arithmetic progressions.

How it works

Start with one number, x=100x = 100, and see how the estimate compares to the true count. Everything else in the theorem is this procedure repeated at larger xx.

  1. Pick a limit. Let x=100x = 100. We want to know how many primes are at most 100.
  2. Count the primes. The primes up to 10 are 2, 3, 5, 7, which is 4 of them. Between 11 and 100 there are 21 more, so the count is 25. Write this as π(100)=25\pi(100) = 25. Check: the primes from 2 to 29 are 10 of them, and the primes from 31 to 97 are 15, so 10+15=2510 + 15 = 25.
  3. Compute the estimate. ln⁡100≈4.605\ln 100 \approx 4.605 (the natural logarithm), so x/ln⁡x=100/4.605≈21.7x / \ln x = 100 / 4.605 \approx 21.7. Check: here the estimate is below the count. At x=10x = 10 it was above, so at small sizes the sign of the error is not fixed.
  4. Compare. The ratio of the count to the estimate is 25/21.7≈1.1525 / 21.7 \approx 1.15. Check: a ratio near 1 means the estimate has the right size.
  5. Move to a larger xx. At x=106x = 10^6 the count is 78,498 and the estimate is about 72,400, a ratio near 1.08. At x=109x = 10^9 the count is 50,847,534 and the ratio is near 1.05. The ratio drifts toward 1 as xx grows. Check: each step up in size makes the relative error smaller, though slowly.
  6. Read off the rule. The count and the estimate are both growing, and their ratio tends to 1. This is the entire content of the theorem.

The plot shows the same comparison for small xx. The points are the counted primes, and the curve is x/ln⁡xx / \ln x.

20406080100051015202530xnumber of primes
  • x / ln x
  • pi(x), counted
The step-by-step count of primes up to x, against the curve x / ln x. At these small sizes the curve is close to the count; the ratio approaches 1 only slowly as x grows.

The math

Definition: Prime-counting function and the Prime Number Theorem

Let π(x)\pi(x) be the number of primes that are at most xx. The Prime Number Theorem says that

lim⁡x→∞π(x)x/ln⁡x=1,\lim_{x \to \infty} \frac{\pi(x)}{x / \ln x} = 1,

which is written π(x)∼x/ln⁡x\pi(x) \sim x / \ln x and read "π(x)\pi(x) is asymptotically x/ln⁡xx / \ln x".

Step 1 in symbols: the counting function

π(x)\pi(x) is a step function. It stays flat between primes and jumps by one at each prime. For x=100x = 100, π(100)=25\pi(100) = 25, which is the count from the walkthrough. The theorem does not describe the jumps one at a time. It describes the overall size of the staircase.

Step 4 in symbols: the ratio tends to 1

The ratio in step 4 is π(x)/(x/ln⁡x)\pi(x) / (x / \ln x). At x=100x = 100 it is 1.15. The theorem says this ratio approaches 1 as xx goes to infinity, and the limit is what makes "approximately" precise. The limit notation is defined in Limit of a Function.

Why the estimate has the form x/ln⁡xx / \ln x

The proof is hard, but the form of the answer is easy to see from the density idea. Near a large number tt, the chance that tt is prime is about 1/ln⁡t1 / \ln t. Adding up that chance across all tt from 2 to xx (a definite integral) gives the estimate

π(x)≈∫2xdtln⁡t.\pi(x) \approx \int_2^x \frac{dt}{\ln t}.

Now evaluate the integral by parts. Let u=1/ln⁡tu = 1 / \ln t and dv=dtdv = dt, so du=−dtt(ln⁡t)2du = -\frac{dt}{t (\ln t)^2} (the Derivative of 1/ln⁡t1 / \ln t) and v=tv = t. Then

∫2xdtln⁡t=[tln⁡t]2x+∫2xdt(ln⁡t)2=xln⁡x−2ln⁡2+∫2xdt(ln⁡t)2.\int_2^x \frac{dt}{\ln t} = \left[\frac{t}{\ln t}\right]_2^x + \int_2^x \frac{dt}{(\ln t)^2} = \frac{x}{\ln x} - \frac{2}{\ln 2} + \int_2^x \frac{dt}{(\ln t)^2}.

The last integral is of smaller order than x/ln⁡xx / \ln x (see Big-O Notation), since it behaves like x/(ln⁡x)2x / (\ln x)^2, so the main term of the integral is x/ln⁡xx / \ln x. This is why the density heuristic and the theorem agree. The argument that turns the heuristic into a proof is the hard part, and it uses complex analysis.

Worked examples

Example
Example 1: the estimate at x = 10 (tiny)

Take x=10x = 10. The primes at most 10 are 2, 3, 5 and 7.

  1. The count is π(10)=4\pi(10) = 4.
  2. ln⁡10≈2.303\ln 10 \approx 2.303.
  3. The estimate is 10/2.303≈4.3410 / 2.303 \approx 4.34.
  4. The ratio is 4/4.34≈0.924 / 4.34 \approx 0.92.
  5. Interpretation: at this tiny size the estimate is slightly too large. The theorem is an asymptotic statement, so it says nothing about the sign of the error at small xx.
Example
Example 2: the estimate at x = 1000 (standard)

Take x=1000x = 1000. The number of primes up to 1000 is 168.

  1. Use ln⁡1000=3ln⁡10≈3×2.303=6.908\ln 1000 = 3 \ln 10 \approx 3 \times 2.303 = 6.908.
  2. The estimate is 1000/6.908≈144.81000 / 6.908 \approx 144.8.
  3. The ratio is 168/144.8≈1.16168 / 144.8 \approx 1.16.
  4. Interpretation: at x=1000x = 1000 the estimate is about 14 percent too small, and the ratio is near 1. At x=10x = 10 the ratio was about 0.92, so the ratio is not monotone; the theorem only promises that it settles toward 1 as xx goes to infinity.
Example
Example 3: the density of primes in RSA key generation (realistic)

A key generator needs a random odd number N≈21024N \approx 2^{1024} that is prime. The density result says a random integer near NN is prime with probability about 1/ln⁡N1 / \ln N, and among odd numbers the probability is about 2/ln⁡N2 / \ln N.

  1. ln⁡N=1024ln⁡2≈1024×0.693=710\ln N = 1024 \ln 2 \approx 1024 \times 0.693 = 710.
  2. The probability that a random odd candidate is prime is about 2/710≈0.00282 / 710 \approx 0.0028.
  3. The reciprocal is 710/2=355710 / 2 = 355, so one odd candidate in about 355 is prime.
  4. Interpretation: on average the generator tests about 355 random odd candidates before it finds a prime. Real generators first discard candidates divisible by small primes, which cuts the work, but the density sets the baseline.

Try it: a random odd number near 25122^{512}. About how many odd candidates should you test on average? Answer: ln⁡N=512ln⁡2≈354.9\ln N = 512 \ln 2 \approx 354.9, so the probability is about 2/354.9≈1/1772 / 354.9 \approx 1/177. You test about 177 odd candidates on average.

Common mistakes and tradeoffs

It is tempting to think π(x)\pi(x) equals x/ln⁡xx / \ln x exactly. In fact the ratio only tends to 1, and at sizes people actually compute the error is real. At x=106x = 10^6 the estimate is about 8 percent low. A better estimate uses the logarithmic integral li⁡(x)\operatorname{li}(x), which is much closer at the same sizes. The simple formula is for orders of magnitude, not for exact answers.

It is tempting to think the theorem means primes are evenly spaced. In fact primes near xx are spaced on average by about ln⁡x\ln x, and the gaps vary a great deal. Some gaps are much larger than average, and some pairs of primes are close. The theorem describes the average density, not a regular pattern.

It is tempting to think the theorem guarantees a prime in every interval of a given length. In fact the theorem concerns counts over long ranges. It does not promise a prime in any particular short interval, and short-interval questions need different tools.

It is tempting to mix up the logarithm bases. In fact the theorem uses the natural logarithm, ln⁡\ln. Using log⁡10\log_{10} changes the estimate by a constant factor of about 2.3, which matters for sizing.

Use the estimate when you want the order of magnitude of a count, a density, or a typical gap, and when you do not need exact counts. Do not use it when you need an exact count; compute it by sieving or use a table. The theorem is also the wrong tool for a single number: it talks about large-scale averages, so it cannot tell you whether a particular integer is prime.

History

Prime tables were compiled by hand and by machine for centuries. In the 1790s the teenage Carl Friedrich Gauss noticed from tables that primes thin out roughly like 1/ln⁡x1 / \ln x near xx. Adrien-Marie Legendre published a related estimate in 1798 and a sharper form later. Pafnuty Chebyshev proved in the 1850s that π(x)\pi(x) is, up to constant factors, of the size x/ln⁡xx / \ln x, though he could not show that the ratio tends to 1. Bernhard Riemann's 1859 paper connected the question to the zeta function, which set the agenda for the next decades.

In 1896 Jacques Hadamard and Charles Jean de la Vallée Poussin independently proved the full theorem, using complex analysis and the properties of the zeta function that Riemann had identified. Their proof was a landmark because it turned a statement about tables into a precise limit. It caught on because the estimate is simple, and it still matches counts well at large sizes.

Recap

  • The prime-counting function π(x)\pi(x) counts the primes at most xx.
  • The Prime Number Theorem says π(x)∼x/ln⁡x\pi(x) \sim x / \ln x: the ratio of the true count to x/ln⁡xx / \ln x tends to 1.
  • The form x/ln⁡xx / \ln x comes from the density of primes near xx, which is about 1/ln⁡x1 / \ln x.
  • The estimate is accurate in relative terms but not exact at small sizes, and it describes averages, not individual primes.
  • Read next: Primes in arithmetic progressions for the theorem's extension to residue classes, and the Bombieri-Vinogradov theorem for an averaged form of that result.

Check yourself

QuestionAs xx grows, what does the ratio π(x)/(x/ln⁡x)\pi(x) / (x / \ln x) approach, and what does that say about the primes?Show answer

The ratio approaches 1. So the count of primes up to xx is asymptotically x/ln⁡xx / \ln x, which gives the average density of primes near xx as about 1/ln⁡x1 / \ln x. The primes themselves are not evenly spaced; the theorem is about their average behaviour.

QuestionEstimate π(100)\pi(100) with x/ln⁡xx / \ln x, using ln⁡100≈4.605\ln 100 \approx 4.605. Give the ratio of the true count to the estimate to two decimal places.Show answer

The estimate is 100/4.605≈21.7100 / 4.605 \approx 21.7. The true count is π(100)=25\pi(100) = 25. The ratio is 25/21.7≈1.1525 / 21.7 \approx 1.15.

QuestionWhy does the Prime Number Theorem not imply that the gaps between consecutive primes are all about the same size?Show answer

The theorem gives the average density of primes, which is about 1/ln⁡x1 / \ln x near xx, so the average gap is about ln⁡x\ln x. An average says nothing about how individual gaps are distributed. Some gaps are much larger than the average, and some pairs of primes are much closer, so the gaps vary a great deal.

QuestionA key generator needs a random odd number of about 2048 bits that is prime. Roughly how many odd candidates should it expect to test before finding one, without sieving?Show answer

Take ln⁡N=2048ln⁡2≈1420\ln N = 2048 \ln 2 \approx 1420. The probability that an odd candidate is prime is about 2/14202 / 1420. The expected number of candidates is about 1420/2=7101420 / 2 = 710. The generator should expect to test around 710 odd candidates before finding a prime, and sieving out small factors makes this faster in practice.

Sources