Sub topic 9
How Common Are Primes? The Prime Number Theorem
This chapter assumes you know what a prime is, can read the sieve of Eratosthenes from the earlier chapters, and are comfortable with natural logarithms (ln, base e). The goal is to estimate how many primes lie below n without counting them. That estimate also explains how much work the sieve does, which the next chapter analyzes.
Counting primes with π(n)
Mathematicians write π(n) for the number of primes less than or equal to n. The symbol has nothing to do with 3.14159. Here are some true values:
| n | π(n) | n / ln n | ratio π(n) / (n / ln n) |
|---|---|---|---|
| 10 | 4 | 4.3 | 0.92 |
| 100 | 25 | 21.7 | 1.15 |
| 1,000 | 168 | 144.8 | 1.16 |
| 10,000 | 1,229 | 1,085.7 | 1.13 |
| 100,000 | 9,592 | 8,685.9 | 1.10 |
| 1,000,000 | 78,498 | 72,382.4 | 1.08 |
Primes thin out as n grows. A quarter of the numbers up to 100 are prime, but fewer than 8% of the numbers up to a million are.
The theorem
The prime number theorem says:
π(n) ~ n / ln n
The symbol ~ means the ratio of the two sides tends to 1 as n goes to infinity. It does not mean the difference goes to zero. The table shows the ratio falling slowly toward 1. Even at a million it is still 8% off.
An equivalent way to read the theorem: near a large number n, a randomly chosen integer is prime with probability about 1 / ln n. The average gap between consecutive primes near n is therefore about ln n. Near one million, ln n ≈ 13.8, so roughly one number in 14 is prime.
Checking the estimate in code
The script below counts primes with a sieve and compares the count with n / ln n.
import math
def sieve(n):
if n < 2:
return []
is_prime = [True] * (n + 1)
is_prime[0] = is_prime[1] = False
p = 2
while p * p <= n:
if is_prime[p]:
for m in range(p * p, n + 1, p):
is_prime[m] = False
p += 1
return [i for i, flag in enumerate(is_prime) if flag]
for exp in range(1, 7):
n = 10 ** exp
actual = len(sieve(n))
estimate = n / math.log(n)
print(f"n=10^{exp} pi(n)={actual:>6} n/ln n={estimate:>9.1f} ratio={actual / estimate:.3f}")The last line printed is n=10^6 pi(n)= 78498 n/ln n= 72382.4 ratio=1.084.
A better approximation
The estimate n / ln n always comes out too low for n ≥ 17. The logarithmic integral, li(n) = ∫₂ⁿ dt / ln t (some books start the integral at 0), adds up the local density 1 / ln t and is much closer. For n = 10⁶, li(n) gives about 78,627, only 0.2% above the true 78,498. If you need to size an array for the primes below n, a safe rule is to allocate int(1.26 * n / math.log(n)) + 1 slots. For n ≥ 17, that is a proven upper bound.
Common mistakes
- Using log base 10. The theorem uses the natural logarithm. In Python that is
math.log(n), notmath.log10(n). - Reading
~as "close in absolute terms". The gap π(n) − n / ln n actually grows without limit. Only the ratio approaches 1. - Expecting regular spacing. ln n is only the average gap. Twin primes such as 101 and 103 sit next to long gaps with no primes.
Why this matters for the sieve
The sieve crosses out multiples of each prime p, which costs about n / p steps for that p. To total that work you need to know how the primes are spread out, and the prime number theorem tells you. The next chapter uses it to show that the sum of 1 / p over primes up to n grows like ln ln n.
Exercise
Use n / ln n to estimate how many primes lie below 10,000,000. The true value is 664,579. By what percentage does the estimate fall short?
Show answer
ln(10^7) ≈ 16.118, so the estimate is 10,000,000 / 16.118 ≈ 620,421. That is 664,579 − 620,421 = 44,158 too few, about 6.6% below the true count.
Exercise
You pick a random integer near 10^12. Roughly what is the chance it is prime, and about how many consecutive integers would you expect to test before finding a prime?
Show answer
ln(10^12) = 12 × ln 10 ≈ 27.6, so the probability is about 1/27.6 ≈ 3.6%. On average you would test about 28 consecutive integers, or about 14 if you skip even numbers.