Algorithm Search⌘K

Proofs: Wheel factorization

These are the human proofs of Wheel factorization, at full length.

Correctness of wheel factorization full

This proof shows: correctness, termination.

Let N≥1N \ge 1, let p1<⋯<pkp_1 < \cdots < p_k be the first kk primes, W=p1⋯pkW = p_1 \cdots p_k (with W=1W = 1 for k=0k = 0), and let ss be the least integer greater than 11 with gcd⁡(s,W)=1\gcd(s, W) = 1.

Lemma 1 (the wheel lists the right numbers). The values that dd takes at line L5 are exactly the integers t≥st \ge s with gcd⁡(t,W)=1\gcd(t, W) = 1, in increasing order.

Proof. Since gcd⁡(t,W)=gcd⁡(t mod W,W)\gcd(t, W) = \gcd(t \bmod W, W), the set C={t≥s:gcd⁡(t,W)=1}C = \{t \ge s : \gcd(t, W) = 1\} is invariant under t↦t+Wt \mapsto t + W. Its elements in [s,s+W)[s, s + W) are r0=s<r1<⋯<rφ−1r_0 = s < r_1 < \cdots < r_{\varphi - 1}, where φ=φ(W)\varphi = \varphi(W). So the elements of CC in increasing order are r0,r1,…,rφ−1,r0+W,r1+W,…r_0, r_1, \dots, r_{\varphi - 1}, r_0 + W, r_1 + W, \dots, and the gap from each to the next is gj=rj+1−rjg_j = r_{j+1} - r_j for j<φ−1j < \varphi - 1 and gφ−1=r0+W−rφ−1g_{\varphi - 1} = r_0 + W - r_{\varphi - 1} at the end of a period. Line L4 sets d=r0d = r_0, i=0i = 0, and line L7 adds gig_i and advances ii modulo φ\varphi; by induction, after tt steps dd is the tt-th element of CC (counted from 00) and i=t mod φi = t \bmod \varphi. □\square

Lemma 2. Every prime qq that is not a wheel prime is in CC, and every prime less than ss is a wheel prime.

Proof. A prime qq that is not among p1,…,pkp_1, \dots, p_k does not divide WW, so gcd⁡(q,W)=1\gcd(q, W) = 1. As q>1q > 1, the minimality of ss gives q≥sq \ge s, so q∈Cq \in C. The second claim is the contrapositive. □\square

Invariant. Each time line L5 is reached: (a) N=(∏F)⋅mN = (\prod F) \cdot m; (b) FF is a list of primes in non-decreasing order, each less than dd; (c) no prime less than dd divides mm.

Initialization. After lines L1 to L3, FF holds each wheel prime as often as it divides NN, in increasing order, and m=N/∏Fm = N / \prod F, so (a) holds and mm has no wheel prime as a factor. Line L4 sets d=sd = s. Each wheel prime is less than ss (it divides WW, so it is not coprime to WW, and the primes are less than pk+1=sp_{k+1} = s when the basis is the first kk primes). So (b) holds. Every prime less than ss is a wheel prime by Lemma 2, so (c) holds.

Maintenance. Suppose the invariant holds and d2≤md^2 \le m. First, if d∣md \mid m, then dd is prime: d≥s≥2d \ge s \ge 2, and a prime factor qq of dd divides mm, so q≥dq \ge d by (c), which forces q=dq = d. The inner loop of line L6 divides mm by dd while possible and appends dd each time, which keeps (a), keeps the order in (b) because every earlier entry is less than dd, and leaves d∤md \nmid m. Line L7 moves dd to d′d', the next element of CC (Lemma 1). The entries of FF are at most d<d′d < d', so (b) holds. For (c), let q<d′q < d' be a prime. If qq is a wheel prime, it does not divide mm (it was removed in lines L2 and L3, and mm only shrinks by other factors). Otherwise q∈Cq \in C by Lemma 2, and q<d′q < d' means q≤dq \le d since no element of CC lies strictly between dd and d′d'. If q<dq < d, (c) before the step gives q∤mq \nmid m; if q=dq = d, line L6 removed it. Dividing mm never adds prime factors, so (c) holds for the new mm.

Termination. Each pass of the outer loop increases dd by a positive gap, and mm never grows. So after finitely many passes d2>md^2 > m. Each inner loop of line L6 ends, because d≥2d \ge 2 and mm is a positive integer that shrinks with each division.

Correctness. When the loop stops, d2>md^2 > m. If m>1m > 1 were composite, it would have a prime factor q≤m<dq \le \sqrt m < d, against (c). So m=1m = 1 or mm is a prime with m≥dm \ge d by (c); in the second case line L8 appends mm, which is at least every entry of FF. By (a), the product of the returned list is NN, and every entry is prime and in non-decreasing order. By the fundamental theorem of arithmetic this is the factorization of NN. ■\blacksquare

Back to Wheel factorization