Sub topic 3
How Common Are Primes? The Prime Number Theorem
This chapter assumes you know the basic sieve of Eratosthenes from the previous chapter, can read Python, and know introductory Big-O notation.
How Common Are Primes?
The prime-counting function π(n) is the number of primes up to n. The prime number theorem says that π(n) ≈ n / ln n. Put another way, a random number near n is prime with probability about 1 / ln n.
| n | π(n) | n / ln n |
|---|---|---|
| 1,000 | 168 | 145 |
| 10,000 | 1,229 | 1,086 |
| 1,000,000 | 78,498 | 72,382 |
The estimate runs a little low, but the ratio between π(n) and n / ln n tends to 1 as n grows.
Why the Sieve Runs in O(n log log n) Time
Each prime p crosses out about n / p numbers. The total work is n multiplied by the sum of 1/p over all primes p ≤ n. Because primes thin out at a rate of about 1 / ln n, that sum grows like ln ln n, a result known as Mertens' theorem. The total is therefore O(n log log n) time. For any practical n this is close to linear. Memory is O(n), with one flag per number.
Saving Memory: Odd-Only and Bit-Packed Sieves
2 is the only even prime, so an odd-only sieve stores flags just for odd numbers. Index i stands for the number 2i + 1, which halves the memory. Bit-packing stores 8 flags in each byte instead of one flag per byte. Combining the two uses about n/16 bytes.
Sieving Large Ranges with a Segmented Sieve
To find primes in [lo, hi], first sieve the base primes up to √hi. Then cross out their multiples inside a buffer that covers only the range. Memory is O(√hi + (hi − lo)).
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]
def segmented_sieve(lo, hi):
base = sieve(math.isqrt(hi))
mark = [True] * (hi - lo + 1)
for p in base:
start = max(p * p, ((lo + p - 1) // p) * p)
for m in range(start, hi + 1, p):
mark[m - lo] = False
return [lo + i for i, flag in enumerate(mark) if flag and lo + i >= 2]
print(segmented_sieve(100, 130)) # [101, 103, 107, 109, 113, 127]Smallest Prime Factor Tables for Fast Factorization
The smallest-prime-factor (SPF) sieve records the smallest prime factor of each number instead of a true/false flag. To factorize any x ≤ n, divide x repeatedly by spf[x]. Each division at least halves x, so factorizing takes O(log x) steps.
def smallest_prime_factors(n):
spf = list(range(n + 1))
p = 2
while p * p <= n:
if spf[p] == p:
for m in range(p * p, n + 1, p):
if spf[m] == m:
spf[m] = p
p += 1
return spf
def factorize(x, spf):
factors = []
while x > 1:
factors.append(spf[x])
x //= spf[x]
return factors
spf = smallest_prime_factors(100)
print(factorize(84, spf)) # [2, 2, 3, 7]Exercise
In segmented_sieve(100, 130), where does crossing out start for p = 7, and why not at 49?
Show answer
It starts at 105, the first multiple of 7 that is at least 100. 49 lies below lo, so the code takes max(49, 105) = 105.
Exercise
Use the prime number theorem to estimate how many primes lie below 10^9.
Show answer
10^9 / ln(10^9) ≈ 10^9 / 20.7 ≈ 48.3 million. The true count is 50,847,534.