Algorithm Search⌘K

Proofs: Shanks' square forms factorization

These are the human proofs of Shanks' square forms factorization, at full length.

Correctness of Shanks' square forms factorization full

This proof shows: correctness, termination.

Let D=kND = kN, not a perfect square, so D\sqrt D is irrational, and let a=⌊D⌋a = \lfloor \sqrt D \rfloor. For integers PP and Q>0Q > 0, the number x=(P+D)/Qx = (P + \sqrt D)/Q has conjugate xˉ=(P−D)/Q\bar x = (P - \sqrt D)/Q. Call xx reduced if x>1x > 1 and −1<xˉ<0-1 < \bar x < 0.

Lemma 1 (the step). Let x=(P+D)/Qx = (P + \sqrt D)/Q be reduced with integers PP, Q>0Q > 0 and Q∣D−P2Q \mid D - P^2, let Q′Q' be the integer with QQ′=D−P2QQ' = D - P^2, and let b=⌊(a+P)/Q⌋b = \lfloor (a + P)/Q \rfloor, P+=bQ−PP^+ = bQ - P, Q+=Q′+b(P−P+)Q^+ = Q' + b(P - P^+). Then b=⌊x⌋≥1b = \lfloor x \rfloor \ge 1, QQ+=D−(P+)2QQ^+ = D - (P^+)^2, and x+=(P++D)/Q+x^+ = (P^+ + \sqrt D)/Q^+ is reduced, with x+=1/(x−b)x^+ = 1/(x - b). Also 0<P<D0 < P < \sqrt D and 0<Q<2D0 < Q < 2\sqrt D.

Proof. From x>1>−xˉx > 1 > -\bar x we get x+xˉ=2P/Q>0x + \bar x = 2P/Q > 0, so P>0P > 0; from xˉ<0\bar x < 0, P<DP < \sqrt D. From x>1x > 1, Q<P+D<2DQ < P + \sqrt D < 2\sqrt D. Since Q≥1Q \ge 1 is an integer and ⌊P+D⌋=P+a\lfloor P + \sqrt D \rfloor = P + a, we have ⌊x⌋=⌊(P+D)/Q⌋=⌊(P+a)/Q⌋=b\lfloor x \rfloor = \lfloor (P + \sqrt D)/Q \rfloor = \lfloor (P + a)/Q \rfloor = b (the floor of y/Qy/Q equals the floor of ⌊y⌋/Q\lfloor y \rfloor / Q for a positive integer QQ). As x>1x > 1, b≥1b \ge 1.

Next, (P+)2−P2=(P+−P)(P++P)=(P+−P)bQ(P^+)^2 - P^2 = (P^+ - P)(P^+ + P) = (P^+ - P) bQ, so D−(P+)2=QQ′−bQ(P+−P)=Q(Q′+b(P−P+))=QQ+D - (P^+)^2 = QQ' - bQ(P^+ - P) = Q(Q' + b(P - P^+)) = QQ^+.

Now x−b=(P−bQ+D)/Q=(D−P+)/Qx - b = (P - bQ + \sqrt D)/Q = (\sqrt D - P^+)/Q, and 0<x−b<10 < x - b < 1 because xx is irrational. Since DD is not a square, D−(P+)2=QQ+≠0D - (P^+)^2 = QQ^+ \ne 0, so Q+≠0Q^+ \ne 0, and x+=1/(x−b)=Q(D+P+)/(D−(P+)2)=(P++D)/Q+x^+ = 1/(x - b) = Q(\sqrt D + P^+)/(D - (P^+)^2) = (P^+ + \sqrt D)/Q^+. From 0<x−b<10 < x - b < 1, x+>1x^+ > 1. The conjugate map is a field automorphism of Q(D)\mathbb Q(\sqrt D) that fixes the integers, so xˉ+=1/(xˉ−b)\bar x^+ = 1/(\bar x - b). Since xˉ∈(−1,0)\bar x \in (-1, 0) and b≥1b \ge 1, xˉ−b<−1\bar x - b < -1, so xˉ+∈(−1,0)\bar x^+ \in (-1, 0). Then x+−xˉ+=2D/Q+x^+ - \bar x^+ = 2\sqrt D / Q^+ is positive, because x+>1>0>xˉ+x^+ > 1 > 0 > \bar x^+, so Q+>0Q^+ > 0. Hence x+x^+ is reduced. □\square

Lemma 2 (the forward walk). Line L2 sets P=aP = a, Q′=1Q' = 1, Q=D−a2Q = D - a^2. Throughout lines L3 and L4, Q>0Q > 0, Q∣D−P2Q \mid D - P^2 with QQ′=D−P2QQ' = D - P^2, and (P+D)/Q(P + \sqrt D)/Q is reduced.

Proof. At the start, Q=D−a2≥1Q = D - a^2 \ge 1 because DD is not a square, and Q⋅1=D−a2Q \cdot 1 = D - a^2. The number x1=(a+D)/(D−a2)=1/(D−a)x_1 = (a + \sqrt D)/(D - a^2) = 1/(\sqrt D - a) is greater than 11 since 0<D−a<10 < \sqrt D - a < 1, and xˉ1=1/(−D−a)\bar x_1 = 1/(-\sqrt D - a) lies in (−1,0)(-1, 0) since D+a>1\sqrt D + a > 1. So x1x_1 is reduced. Each pass of line L3 is the step of Lemma 1, with (Q′,Q)(Q', Q) replaced by (Q,Q+)(Q, Q^+) and QQ+=D−(P+)2QQ^+ = D - (P^+)^2, so the invariant is kept. □\square

Lemma 3 (the start of the second walk). Suppose line L5 runs with Q=r2Q = r^2. Then RR and S=(D−R2)/rS = (D - R^2)/r are integers, S>0S > 0, rS=D−R2rS = D - R^2, and y=(R+D)/Sy = (R + \sqrt D)/S is reduced.

Proof. By Lemma 2, D−P2=Q′Q=Q′r2D - P^2 = Q'Q = Q' r^2. Since R=br+P≡P(modr)R = br + P \equiv P \pmod r, D−R2≡D−P2≡0(modr)D - R^2 \equiv D - P^2 \equiv 0 \pmod r, so SS is an integer and rS=D−R2rS = D - R^2.

Let b=⌊(a−P)/r⌋b = \lfloor (a - P)/r \rfloor; since 0<P≤a0 < P \le a by Lemma 1, b≥0b \ge 0. Then R=br+PR = br + P is the largest integer ≤a\le a congruent to PP modulo rr, so a−r<R≤aa - r < R \le a, that is a−r+1≤R≤aa - r + 1 \le R \le a. Hence 0<R<D0 < R < \sqrt D, and D−R2>0D - R^2 > 0, so S>0S > 0.

Also r2=Q<2Dr^2 = Q < 2\sqrt D (Lemma 1), so r<2D≤Dr < \sqrt{2\sqrt D} \le \sqrt D when D≥4D \ge 4; for D<4D < 4, D∈{2,3}D \in \{2, 3\} and Qi∈{1,2}Q_i \in \{1, 2\}, and r=1<Dr = 1 < \sqrt D too. Now y>1y > 1 means S<R+DS < R + \sqrt D, that is (D−R)(D+R)/r<R+D(\sqrt D - R)(\sqrt D + R)/r < R + \sqrt D, that is D−R<r\sqrt D - R < r, which holds since R≥a−r+1>D−rR \ge a - r + 1 > \sqrt D - r. And yˉ=(R−D)/S<0\bar y = (R - \sqrt D)/S < 0 since R<DR < \sqrt D, while yˉ>−1\bar y > -1 means D−R<S=(D−R)(D+R)/r\sqrt D - R < S = (\sqrt D - R)(\sqrt D + R)/r, that is r<D+Rr < \sqrt D + R, which holds since r<Dr < \sqrt D. So yy is reduced. □\square

Lemma 4 (the second walk). Throughout lines L6 to L8, S>0S > 0, SS′=D−R2SS' = D - R^2, and (R+D)/S(R + \sqrt D)/S is reduced. If the repeat ends at line L8 with R+=RR^+ = R, then S∣2RS \mid 2R and S∣D−R2S \mid D - R^2, and for every odd prime ℓ\ell and every e≥1e \ge 1, ℓe∣S\ell^e \mid S implies ℓe∣D\ell^e \mid D.

Proof. The invariant holds at the start by Lemma 3 (with S′=rS' = r), and each pass of line L8 that does not leave the repeat is the step of Lemma 1, so it is kept. If R+=RR^+ = R, then bS−R=RbS - R = R, so 2R=bS2R = bS and S∣2RS \mid 2R; and S∣SS′=D−R2S \mid SS' = D - R^2. Let ℓ\ell be an odd prime with ℓe∣S\ell^e \mid S. From ℓe∣2R\ell^e \mid 2R and ℓ\ell odd, ℓe∣R\ell^e \mid R, so ℓe∣R2\ell^{e} \mid R^2; with ℓe∣D−R2\ell^e \mid D - R^2, we get ℓe∣D\ell^e \mid D. □\square

So the odd part of the final SS divides kNkN; when it is coprime to kk it divides NN, and since it also divides RR, it divides gcd⁡(N,R)\gcd(N, R). This explains why line L9 uses gcd⁡(N,R)\gcd(N, R); whether this gcd is proper is not decided by these lemmas.

Theorem. For every valid input, SQUFOF(N,k,I)(N, k, I) stops, and its result is either 11 or an integer gg with 1<g<N1 < g < N and g∣Ng \mid N. For a prime NN the result is 11.

Proof. Termination. Each pass of line L3 increases ii, so line L3 runs at most II times. Each run of lines L6 to L8 makes at most II passes. All divisions are by positive integers (Q>0Q > 0, r>0r > 0, S>0S > 0 by Lemmas 2 to 4) and are exact where the algorithm needs an integer (Lemmas 1 and 3), so no step fails. Hence the algorithm stops after at most I+I2I + I^2 passes of the inner lines.

Correctness. The only return other than line L11 is line L10, which returns g=gcd⁡(N,R)g = \gcd(N, R) only if 1<g<N1 < g < N. A gcd with NN divides NN. So the result is 11 or a proper divisor of NN. A prime NN has no proper divisor, so the test of line L10 always fails, and line L11 returns 11. ■\blacksquare

Back to Shanks' square forms factorization