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=kN, not a perfect square, so D is irrational, and let a=⌊D⌋. For integers P and Q>0, the number x=(P+D)/Q has conjugate xˉ=(P−D)/Q. Call xreduced if x>1 and −1<xˉ<0.
Lemma 1 (the step). Let x=(P+D)/Q be reduced with integers P, Q>0 and Q∣D−P2, let Q′ be the integer with QQ′=D−P2, and let b=⌊(a+P)/Q⌋, P+=bQ−P, Q+=Q′+b(P−P+). Then b=⌊x⌋≥1, QQ+=D−(P+)2, and x+=(P++D)/Q+ is reduced, with x+=1/(x−b). Also 0<P<D and 0<Q<2D.
Proof. From x>1>−xˉ we get x+xˉ=2P/Q>0, so P>0; from xˉ<0, P<D. From x>1, Q<P+D<2D. Since Q≥1 is an integer and ⌊P+D⌋=P+a, we have ⌊x⌋=⌊(P+D)/Q⌋=⌊(P+a)/Q⌋=b (the floor of y/Q equals the floor of ⌊y⌋/Q for a positive integer Q). As x>1, b≥1.
Next, (P+)2−P2=(P+−P)(P++P)=(P+−P)bQ, so D−(P+)2=QQ′−bQ(P+−P)=Q(Q′+b(P−P+))=QQ+.
Now x−b=(P−bQ+D)/Q=(D−P+)/Q, and 0<x−b<1 because x is irrational. Since D is not a square, D−(P+)2=QQ+=0, so Q+=0, and x+=1/(x−b)=Q(D+P+)/(D−(P+)2)=(P++D)/Q+. From 0<x−b<1, x+>1. The conjugate map is a field automorphism of Q(D) that fixes the integers, so xˉ+=1/(xˉ−b). Since xˉ∈(−1,0) and b≥1, xˉ−b<−1, so xˉ+∈(−1,0). Then x+−xˉ+=2D/Q+ is positive, because x+>1>0>xˉ+, so Q+>0. Hence x+ is reduced. □
Lemma 2 (the forward walk). Line L2 sets P=a, Q′=1, Q=D−a2. Throughout lines L3 and L4, Q>0, Q∣D−P2 with QQ′=D−P2, and (P+D)/Q is reduced.
Proof. At the start, Q=D−a2≥1 because D is not a square, and Q⋅1=D−a2. The number x1=(a+D)/(D−a2)=1/(D−a) is greater than 1 since 0<D−a<1, and xˉ1=1/(−D−a) lies in (−1,0) since D+a>1. So x1 is reduced. Each pass of line L3 is the step of Lemma 1, with (Q′,Q) replaced by (Q,Q+) and QQ+=D−(P+)2, so the invariant is kept. □
Lemma 3 (the start of the second walk). Suppose line L5 runs with Q=r2. Then R and S=(D−R2)/r are integers, S>0, rS=D−R2, and y=(R+D)/S is reduced.
Proof. By Lemma 2, D−P2=Q′Q=Q′r2. Since R=br+P≡P(modr), D−R2≡D−P2≡0(modr), so S is an integer and rS=D−R2.
Let b=⌊(a−P)/r⌋; since 0<P≤a by Lemma 1, b≥0. Then R=br+P is the largest integer ≤a congruent to P modulo r, so a−r<R≤a, that is a−r+1≤R≤a. Hence 0<R<D, and D−R2>0, so S>0.
Also r2=Q<2D (Lemma 1), so r<2D≤D when D≥4; for D<4, D∈{2,3} and Qi∈{1,2}, and r=1<D too. Now y>1 means S<R+D, that is (D−R)(D+R)/r<R+D, that is D−R<r, which holds since R≥a−r+1>D−r. And yˉ=(R−D)/S<0 since R<D, while yˉ>−1 means D−R<S=(D−R)(D+R)/r, that is r<D+R, which holds since r<D. So y is reduced. □
Lemma 4 (the second walk). Throughout lines L6 to L8, S>0, SS′=D−R2, and (R+D)/S is reduced. If the repeat ends at line L8 with R+=R, then S∣2R and S∣D−R2, and for every odd prime ℓ and every e≥1, ℓe∣S implies ℓe∣D.
Proof. The invariant holds at the start by Lemma 3 (with S′=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+=R, then bS−R=R, so 2R=bS and S∣2R; and S∣SS′=D−R2. Let ℓ be an odd prime with ℓe∣S. From ℓe∣2R and ℓ odd, ℓe∣R, so ℓe∣R2; with ℓe∣D−R2, we get ℓe∣D. □
So the odd part of the final S divides kN; when it is coprime to k it divides N, and since it also divides R, it divides gcd(N,R). This explains why line L9 uses gcd(N,R); whether this gcd is proper is not decided by these lemmas.
Theorem. For every valid input, SQUFOF(N,k,I) stops, and its result is either 1 or an integer g with 1<g<N and g∣N. For a prime N the result is 1.
Proof.Termination. Each pass of line L3 increases i, so line L3 runs at most I times. Each run of lines L6 to L8 makes at most I passes. All divisions are by positive integers (Q>0, r>0, S>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+I2 passes of the inner lines.
Correctness. The only return other than line L11 is line L10, which returns g=gcd(N,R) only if 1<g<N. A gcd with N divides N. So the result is 1 or a proper divisor of N. A prime N has no proper divisor, so the test of line L10 always fails, and line L11 returns 1. ■