These are the human proofs of Continued fraction factorization (CFRAC), at full length.
Correctness of continued fraction 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. Since 1 and D are linearly independent over Q, the map u+vD↦u−vD for rational u,v is a field automorphism of Q(D); it fixes the rationals.
Lemma 1 (the step). Let x=(P+D)/Q be reduced, with integers P, Q>0 and an integer Q′ such that QQ′=D−P2. Let b=⌊(a+P)/Q⌋, P+=bQ−P and Q+=Q′+b(P−P+). Then 0<P<D, 0<Q<2D, b=⌊x⌋≥1, QQ+=D−(P+)2, and x+=(P++D)/Q+ is reduced with x+=1/(x−b).
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. For a real y and a positive integer Q, ⌊y/Q⌋=⌊⌊y⌋/Q⌋, and ⌊P+D⌋=P+a, so ⌊x⌋=⌊(P+a)/Q⌋=b. 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=(D−P+)/Q, and 0<x−b<1 since x is irrational. As D is not a square, QQ+=D−(P+)2=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 gives xˉ+=1/(xˉ−b), and xˉ−b<−1 because xˉ<0 and b≥1, so −1<xˉ+<0. Then x+−xˉ+=2D/Q+>0, so Q+>0. Hence x+ is reduced. □
Lemma 2 (the expansion). Let P1=a, Q0=1, Q1=D−a2, and define bi, Pi+1, Qi+1 by the recurrence of line L5. Let xi=(Pi+D)/Qi. Then for every i≥1, xi is reduced, Qi−1Qi=D−Pi2, 0<Qi<2D, bi=⌊xi⌋ and xi+1=1/(xi−bi). Also D=a+1/x1.
Proof.Q1=D−a2≥1 since D is not a square, and Q0Q1=D−P12. Since 0<D−a<1, x1=(a+D)/(D−a2)=1/(D−a)>1, which also gives D=a+1/x1. And xˉ1=1/(−D−a) lies in (−1,0) because D+a>1. So x1 is reduced. Lemma 1 with (P,Q′,Q)=(Pi,Qi−1,Qi) gives all the claims for i and passes the hypotheses on to i+1. Induction on i completes the proof. □
Lemma 3 (convergents). Let A−1=1, A0=a, B−1=0, B0=1, and Ai=biAi−1+Ai−2, Bi=biBi−1+Bi−2 for i≥1. Then for every i≥1:
D=(Ai−1xi+Ai−2)/(Bi−1xi+Bi−2),
Ai−1Bi−2−Ai−2Bi−1=(−1)i,
Ai−12−DBi−12=(−1)iQi.
Proof. (1) For i=1 the right side is (ax1+1)/x1=a+1/x1=D by Lemma 2. If (1) holds for i, substitute xi=bi+1/xi+1 from Lemma 2. The numerator becomes Ai−1bi+Ai−2+Ai−1/xi+1=(Aixi+1+Ai−1)/xi+1, and in the same way the denominator becomes (Bixi+1+Bi−1)/xi+1; so (1) holds for i+1. The denominators are positive, since all Bj≥0, Bi−1≥1 for i≥1, and xi>0.
(2) For i=1: A0B−1−A−1B0=0−1=−1. And AiBi−1−Ai−1Bi=(biAi−1+Ai−2)Bi−1−Ai−1(biBi−1+Bi−2)=−(Ai−1Bi−2−Ai−2Bi−1), so the sign alternates.
(3) Multiply (1) out: xi(DBi−1−Ai−1)=Ai−2−DBi−2. Let M=Ai−12−DBi−12, a nonzero integer since D is irrational. Multiplying both sides by DBi−1+Ai−1 gives −Mxi=(Ai−2−DBi−2)(DBi−1+Ai−1)=U+D(Ai−2Bi−1−Ai−1Bi−2) with the integer U=Ai−1Ai−2−DBi−1Bi−2. By (2) this is U−(−1)iD. So xi=((−1)iD−U)/M. We also have xi=Pi/Qi+D/Qi. Since 1 and D are linearly independent over Q, the coefficients of D agree: 1/Qi=(−1)i/M, so M=(−1)iQi. □
Lemma 4 (the loop of lines L3 to L5). At each test of line L4, with step counter i, the variables hold P=Pi, Q=Qi, Q′=Qi−1 and A=Ai−1modN, A′=Ai−2modN. Every relation (A,v) in R satisfies A2≡∏p∈Fpvp(modN), where v is the exponent vector and p=−1 is included.
Proof. Line L2 sets P=a=P1, Q′=1=Q0, Q=Q1, A′=1=A−1, A=A0modN and i=1. Line L5 is the recurrence of Lemma 2 for P and Q, and it replaces (A′,A) by (A,(bA+A′)modN) with b=bi, which is (Ai−1,Ai) modulo N because reduction modulo N commutes with sums and products. Then i grows by one, so the invariant holds at the next test. Line L4 appends (A,v) only if (−1)iQi=∏p∈Fpvp. By Lemma 3, Ai−12=DBi−12+(−1)iQi, and N∣D, so A2≡Ai−12≡(−1)iQi(modN). □
Lemma 5 (the elimination). Let v1,…,vm be the exponent vectors mod 2, as bit masks. The elimination keeps for each pivot a pair (u,h) with u=⨁j∈hvj, where ⊕ is the sum mod 2. When row j reduces to 0, the subset h it carries is nonempty and ⨁l∈hvl=0. If m>∣F∣, at least m−∣F∣ subsets are found.
Proof. Row j starts with u=vj and h={j}. Each reduction adds a pivot pair (u′,h′) to (u,h) by ⊕ in both parts, which keeps u=⨁l∈hvl. The pivots stored before row j come from rows l<j, so their sets h′ do not contain j, and j stays in h; thus h=∅. A row either becomes a new pivot, with a leading bit that no other pivot has, or reduces to u=0. Each reduction step clears the leading bit of u and changes only lower bits, so the process ends. The pivots have distinct leading bits among the ∣F∣ positions, so there are at most ∣F∣ pivots, and at least m−∣F∣ rows reduce to 0. □
Theorem. CFRAC stops, and it returns either 1 or a proper factor g of N with 1<g<N. Every subset T tried at line L7 gives X2≡Y2(modN). When the loop ends with ∣R∣=∣F∣+E, at least E subsets are tried.
Proof.Termination. Each pass of the loop increases i, and the loop stops when i≥I, so it makes fewer than I passes. All divisions in line L5 are by Q=Qi>0 (Lemma 2). The smoothness test of line L4 divides a nonzero integer by primes and stops. Line L6 runs Lemma 5 on finitely many rows, and lines L7 and L8 run once per subset. So the algorithm stops.
Congruence of squares. Let T be a subset with ⨁j∈Tvj=0, and let e=∑j∈Tvj over the integers. Then every ep is even, including e−1. By Lemma 4, X2=∏j∈TAj2≡∏p∈Fpep=(−1)e−1∏p=−1(pep/2)2=Y2(modN), since (−1)e−1=1. The count of subsets follows from Lemma 5 with m=∣F∣+E.
Correctness. The only return other than line L9 is line L8, which returns g=gcd(X−Y,N) only if 1<g<N. A gcd with N divides N, so g is a proper factor. Otherwise line L9 returns 1. ■