Sub topic 8
Starting Each Pass at p Squared
This chapter assumes you already know the basic sieve of Eratosthenes from the previous chapter: you keep a list of flags, and for each prime p you cross out its multiples. You should also be comfortable with Python loops and range with a step. The question here is narrower. When you reach prime p, why can you start crossing out at p * p and not at 2 * p?
The claim
When the sieve reaches a prime p, every multiple k * p with k < p has already been crossed out. The first multiple that might still be unmarked is p * p.
Why smaller multiples are already gone
Take any multiple k * p where 2 <= k < p. The number k is at least 2, so it has a prime factor q. Since q <= k < p, the prime q is smaller than p. The sieve handles primes in increasing order, so the pass for q already ran. That pass crossed out every multiple of q from q * q up to n. The number k * p is a multiple of q, and it is at least q * q because both k and p are at least q. So the pass for q crossed it out.
Here is the case p = 5 in detail. Its multiples below 25 are 10, 15, and 20.
| Multiple of 5 | k | Smallest prime factor of k | Already crossed out in pass |
|---|---|---|---|
| 10 | 2 | 2 | p = 2 |
| 15 | 3 | 3 | p = 3 |
| 20 | 4 | 2 | p = 2 |
| 25 | 5 | 5 | p = 5 (first new mark) |
The same fact gives the outer loop bound
If every pass starts at p * p, a prime with p * p > n has no multiples to cross out within the range. Its pass would do nothing. That is why the outer loop can run while p * p <= n and stop there. The flags for every number above the square root are already final when the loop stops. Starting at p * p and stopping at the square root of n come from the same argument.
Measuring the savings
The function below counts how many crossing-out writes each starting point makes. Both versions use the same outer bound, so the only difference is where each pass begins.
def count_marks(n, start_at_square):
is_prime = [True] * (n + 1)
is_prime[0] = is_prime[1] = False
marks = 0
p = 2
while p * p <= n:
if is_prime[p]:
start = p * p if start_at_square else 2 * p
for m in range(start, n + 1, p):
is_prime[m] = False
marks += 1
p += 1
return marks
print(count_marks(30, False), count_marks(30, True)) # 28 24
print(count_marks(100, False), count_marks(100, True)) # 113 104Each prime p skips exactly p - 2 redundant writes. For n = 100 the primes are 2, 3, 5, and 7, which gives 0 + 1 + 3 + 5 = 9 fewer writes. The constant-factor gain is modest. The more important payoff is the square root bound on the outer loop, which you just saw follows from the same argument.
Common errors
- Starting at
p * pwithout checkingis_prime[p]. Ifpis composite, its multiples are already crossed out. The output is still correct, but the pass wastes work. Always guard the inner loop with the primality check. - Using
range(p * p, n, p). The stop value ofrangeis exclusive, so this never marksnitself. Usen + 1. - Overflow in fixed-width languages. In C or Java,
p * pcan overflow a 32-bitintwhenpis above 46340. Python integers never overflow, but if you port the code, use a 64-bit type or comparep <= n / p. - Starting the inner loop at
p. This crosses outpitself and marks every prime as composite.
Exercise
When the sieve reaches p = 7, which multiples of 7 below 49 does it skip, and which earlier pass crossed out each one?
Show answer
It skips 14, 21, 28, 35, and 42. 14, 28, and 42 were crossed out in the pass for 2. 21 was crossed out in the pass for 3, and 35 in the pass for 5.
Exercise
A student changes the inner loop to start at p * p + p. For n = 30, which number is wrongly reported as prime?
Show answer
Both 25 (5 5) and 9 (3 3) are skipped, and so is 4 (2 2). No other pass crosses out 4, 9, or 25, so all three are reported as prime. In general, every p p is left unmarked.