Algorithm Search⌘K

Proofs: Segmented sieve

These are the human proofs of Segmented sieve, at full length.

The segmented sieve returns exactly the primes in [L, R] full

This proof shows: correctness.

For m≥2m \ge 2 let lpf(m)\mathrm{lpf}(m) be the least prime factor of mm. Recall that a composite mm satisfies lpf(m)2≤m\mathrm{lpf}(m)^2 \le m: writing m=qkm = q k with q=lpf(m)q = \mathrm{lpf}(m), every prime factor of k≥2k \ge 2 divides mm and so is at least qq, which gives k≥qk \ge q.

After line L1, L≥2L \ge 2. If L>RL > R, there is no integer in [max⁡(L,2),R][\max(L,2), R] and the empty answer is correct; this covers R<2R < 2. From now on 2≤L≤R2 \le L \le R. Line L2 is the sieve of Eratosthenes, so base is the increasing list of all primes p≤⌊R⌋p \le \lfloor \sqrt R \rfloor, that is, all primes with p2≤Rp^2 \le R.

The blocks. Line L3 sets lo=Llo = L. Each pass of the loop of line L4 handles the block [lo,hi][lo, hi] with hi=min⁡(lo+B−1,R)≥lohi = \min(lo + B - 1, R) \ge lo, since B≥1B \ge 1 and lo≤Rlo \le R, and line L11 sets the next lolo to hi+1hi + 1. So the blocks are [L,hi1],[hi1+1,hi2],…[L, hi_1], [hi_1 + 1, hi_2], \ldots, consecutive, disjoint and in increasing order, and each has at least one number. The loop stops at the first lo>Rlo > R; since lolo grows by at least 1 in each pass, it stops, and the last block ends at hi=Rhi = R. So the blocks cover [L,R][L, R] exactly.

One block. Fix a block [lo,hi][lo, hi] with 2≤lo≤hi≤R2 \le lo \le hi \le R. Line L6 sets every flag to true, and only line L9 changes a flag, always to false. We show that after line L9 is done, for lo≤m≤hilo \le m \le hi, the flag S[m−lo]S[m - lo] is false if and only if mm is composite.

Only composites are cleared. Line L9 clears m=s+tpm = s + tp for a base prime pp and an integer t≥0t \ge 0, where ss from line L8 is a multiple of pp with s≥p2s \ge p^2. So mm is a multiple of pp and m≥p2>pm \ge p^2 > p; such an mm has the proper divisor p>1p > 1 and is composite.

Every composite is cleared. Let mm in [lo,hi][lo, hi] be composite and q=lpf(m)q = \mathrm{lpf}(m). Then q2≤m≤Rq^2 \le m \le R, so qq is a base prime, and q2≤m≤hiq^2 \le m \le hi, so line L7 handles qq. Line L8 sets s=max⁡(q2,⌈lo/q⌉q)s = \max(q^2, \lceil lo / q \rceil q). Both terms are multiples of qq. The number ⌈lo/q⌉q\lceil lo/q \rceil q is the least multiple of qq that is at least lolo, so it is at most mm; also q2≤mq^2 \le m. Hence s≤ms \le m, and m−sm - s is a multiple of qq, as both are. So m=s+tqm = s + tq for some t≥0t \ge 0, and since m≤him \le hi, the loop of line L9 reaches mm and clears its flag.

Line L8 is well defined. ⌈lo/q⌉\lceil lo / q \rceil is computed in integers as ⌊(lo+q−1)/q⌋\lfloor (lo + q - 1)/q \rfloor; nothing else is needed.

So line L10 appends exactly the primes of [lo,hi][lo, hi], in increasing order of jj.

Termination of one block. Line L7 runs over a finite list, and line L9 advances mm by p≥2p \ge 2 until it passes hihi.

Conclusion. The blocks partition [L,R][L, R] in increasing order, and each block appends its primes in increasing order. So the list returned in line L12 is the increasing list of all primes pp with L≤p≤RL \le p \le R. ■\blacksquare

Cost of marking. For a base prime pp and a block of length ℓ\ell, line L9 runs at most ⌊ℓ/p⌋+1\lfloor \ell / p \rfloor + 1 times, since its values are distinct multiples of pp in an interval of length ℓ\ell. Summing over the blocks and the base primes gives the bound in the complexity section.

Back to Segmented sieve