Sub topic 10
Why the Sieve Runs in O(n log log n) Time
This chapter assumes you have read the sieve of Eratosthenes chapter and know why each pass starts at p squared. It also assumes you can read Big-O notation and know the prime number theorem's estimate that roughly n / ln n primes lie below n. Here we count the sieve's work, find where the unusual log log n factor comes from, and look at its memory cost.
Counting the Crossings
Almost all of the sieve's time goes to one line: is_prime[m] = False. To get the running time, count how many times that line runs.
The pass for a prime p starts at p² and steps by p up to n. It crosses out about (n − p²) / p + 1 numbers, which is at most n / p. Passes happen only for primes p ≤ √n. The total work is therefore bounded by:
- n/2 + n/3 + n/5 + n/7 + ... = n × (sum of 1/p over primes p ≤ √n)
So the real question is how fast the sum of prime reciprocals grows.
Where log log n Comes From
Mertens' second theorem says the sum of 1/p over primes p ≤ x is about ln ln x + 0.2615. That is the source of the bound. The sum diverges, but extremely slowly, because primes thin out at the rate the prime number theorem describes.
Substituting x = √n gives ln ln √n = ln ln n − ln 2. The constant drops out in Big-O, so the crossing-out work is O(n log log n). The remaining costs are smaller or the same order:
| Part of the sieve | Cost |
|---|---|
| Allocating and initializing the array | O(n) |
| Outer loop over p up to √n | O(√n) |
| Crossing out multiples | O(n log log n) |
| Collecting the primes at the end | O(n) |
For any n that fits in memory, ln ln n is below 4, so in practice the sieve behaves almost linearly.
A Small Example
For n = 100, the passes run for p = 2, 3, 5, 7:
The total is 104 crossings for 100 numbers. The pass for 2 does nearly half the work, and each later pass does less.
Measuring It
The program below counts crossings and divides by n. If the analysis is right, the ratio grows like ln ln n: very slowly.
import math
def count_crossings(n):
is_prime = [True] * (n + 1)
is_prime[0] = is_prime[1] = False
ops = 0
p = 2
while p * p <= n:
if is_prime[p]:
for m in range(p * p, n + 1, p):
is_prime[m] = False
ops += 1
p += 1
return ops
for n in (10**3, 10**4, 10**5, 10**6):
ops = count_crossings(n)
print(n, ops, round(ops / n, 3), round(math.log(math.log(n)), 3))When you run it, the third column (crossings per number) rises only a little each time n grows tenfold, and it follows the fourth column (ln ln n) at a roughly constant offset.
Memory Cost
The sieve keeps one flag per number from 0 to n, so it uses O(n) memory. Memory usually runs out before time does. A Python list stores an 8-byte pointer per entry, so n = 10⁹ needs about 8 GB. A bytearray cuts that to 1 GB, and a bit array to about 125 MB. Later chapters cover odd-only and bit-packed sieves and the segmented sieve, which removes the O(n) memory requirement.
Comparison with Trial Division
Testing every number up to n by trial division costs up to √k steps per number k, or O(n√n) in total. For n = 10⁶ that is up to about 10⁹ steps, against roughly 2–3 million crossings for the sieve. When you need all primes up to n, the sieve wins by orders of magnitude.
Exercise
If the outer loop ran p all the way to n instead of stopping at √n (still skipping composites), would the Big-O time change? Why or why not?
Show answer
No. Primes above √n have p² > n, so their passes cross out nothing. Each one costs O(1) for the check, which adds O(n) in total. The bound stays O(n log log n), but the program does pointless loop iterations.
Exercise
Estimate the crossings for n = 10^8 using n × (ln ln n − ln 2 + 0.2615), and explain why the true count is somewhat lower.
Show answer
ln(10^8) ≈ 18.42, and ln 18.42 ≈ 2.91, so the estimate is 10^8 × (2.91 − 0.69 + 0.26) ≈ 2.5 × 10^8. The true count is lower because each pass starts at p² rather than p, so it skips about p of its n/p multiples.