Algorithm Search⌘K

Proofs: Hart's one line factoring algorithm

These are the human proofs of Hart's one line factoring algorithm, at full length.

Correctness of Hart's one line factoring algorithm full

This proof shows: correctness, termination.

Let N≥2N \ge 2, M≥1M \ge 1 and K≥1K \ge 1 be integers. For an iteration ii, write x=MNix = MNi, s=⌈x⌉s = \lceil \sqrt x \rceil and m=s2−xm = s^2 - x, as in lines L2 and L3.

Termination. Each pass of the loop of line L1 does a fixed number of operations, and there are at most KK passes. So the algorithm stops, at line L2, L5 or L6.

Lemma 1 (the size of mm). 0≤m≤2s−20 \le m \le 2s - 2, and s≥2s \ge 2.

Proof. s2≥xs^2 \ge x by the choice of ss, so m≥0m \ge 0. Since ss is the smallest integer with s2≥xs^2 \ge x, we have (s−1)2<x(s - 1)^2 < x, so m=s2−x<s2−(s−1)2=2s−1m = s^2 - x < s^2 - (s - 1)^2 = 2s - 1, and m≤2s−2m \le 2s - 2 because both sides are integers. Finally x≥2x \ge 2, so s≥2s \ge 2. □\square

Lemma 2 (a returned value is a proper factor). Suppose line L5 runs at iteration ii, so 2s−1<N2s - 1 < N and m=t2m = t^2 with an integer t≥0t \ge 0. Then g=gcd⁡(N,s−t)g = \gcd(N, s - t) satisfies 1<g<N1 < g < N and g∣Ng \mid N.

Proof. From Lemma 1, t2=m≤2s−2<s2t^2 = m \le 2s - 2 < s^2, because s2−2s+2=(s−1)2+1>0s^2 - 2s + 2 = (s - 1)^2 + 1 > 0. So 0≤t<s0 \le t < s, and t≤s−1t \le s - 1. Then

(s−t)(s+t)=s2−t2=s2−m=x=MNi,(s - t)(s + t) = s^2 - t^2 = s^2 - m = x = MNi,

so NN divides (s−t)(s+t)(s - t)(s + t). Here 1≤s−t≤s+t≤2s−1<N1 \le s - t \le s + t \le 2s - 1 < N.

The value gg divides NN by definition. If g=Ng = N, then N∣s−tN \mid s - t with 0<s−t<N0 < s - t < N, which is impossible. If g=1g = 1, then NN is coprime to s−ts - t, and since NN divides (s−t)(s+t)(s - t)(s + t), it divides s+ts + t. But 0<s+t<N0 < s + t < N, which is again impossible. So 1<g<N1 < g < N. □\square

Corollary 3 (primes and failures). If NN is prime, the algorithm returns 11. For every NN, the output is 11 or a proper factor of NN.

Proof. The algorithm returns only at lines L2 and L6, with the value 11, or at line L5. By Lemma 2, a return at line L5 gives a proper factor of NN. A prime has no proper factor, so for a prime NN line L5 never runs, and the output is 11. □\square

Lemma 4 (when an iteration succeeds). Suppose MNi=ABMNi = AB with integers 1≤A<B1 \le A < B of the same parity, (B−A)2<2(\sqrt B - \sqrt A)^2 < 2, and A+B<NA + B < N. Then, if the loop reaches iteration ii, it stops there at line L5 with s=(A+B)/2s = (A + B)/2 and t=(B−A)/2t = (B - A)/2.

Proof. Put a=(A+B)/2a = (A + B)/2 and b=(B−A)/2b = (B - A)/2, integers because AA and BB have the same parity. Then a2−b2=AB=xa^2 - b^2 = AB = x, so a2≥xa^2 \ge x and a≥xa \ge \sqrt x. Also

a−x=A+B2−AB=(B−A)22<1,a - \sqrt x = \frac{A + B}{2} - \sqrt{AB} = \frac{(\sqrt B - \sqrt A)^2}{2} < 1,

so a−1<x≤aa - 1 < \sqrt x \le a, and a=⌈x⌉=sa = \lceil \sqrt x \rceil = s. Then m=s2−x=b2m = s^2 - x = b^2 is a square, and the square root found at line L4 is t=bt = b. The range test of line L2 passes because 2s−1=A+B−1<N2s - 1 = A + B - 1 < N. So the algorithm reaches line L5 at iteration ii. □\square

Lemma 4 explains the example of the main page: 8⋅200633=1252⋅12828 \cdot 200633 = 1252 \cdot 1282 with (1282−1252)2≈0.18(\sqrt{1282} - \sqrt{1252})^2 \approx 0.18, so iteration 88 gives s=1267s = 1267 and t=15t = 15.

Theorem 5 (the full factorization). Let N≥2N \ge 2, let c=⌊N1/3⌋+1c = \lfloor N^{1/3} \rfloor + 1, and suppose that trial division by every d≤cd \le c with d2≤Rd^2 \le R divides out all such factors from NN, with RR the part left at the end. Then RR is 11, a prime, the square of a prime, or a product of two distinct primes. Splitting RR by a primality test, a square test, OLF and, if OLF fails, trial division up to R\sqrt R, gives the prime factorization of NN.

Proof. The trial division loop stops either because d>cd > c or because d2>Rd^2 > R. Every prime factor of RR is larger than every dd that was tried, since those were divided out.

If the loop stopped because d2>Rd^2 > R, then every prime factor of RR is at least d>Rd > \sqrt R, so RR has at most one prime factor counted with multiplicity: RR is 11 or a prime.

If it stopped because d>cd > c, then every prime factor of RR is larger than c>N1/3≥R1/3c > N^{1/3} \ge R^{1/3}. If RR had three prime factors counted with multiplicity, their product would be larger than RR, a contradiction. So RR is 11, a prime pp, a square p2p^2, or a product pqpq of two distinct primes.

A primality test settles the cases 11 and pp. The case p2p^2 is a perfect square, and the integer square root gives pp. In the case pqpq, RR is composite and not a square; OLF returns a proper factor gg or 11 by Corollary 3. A proper factor of pqpq is pp or qq, so R=g⋅(R/g)R = g \cdot (R/g) is the factorization into two primes. If OLF fails, trial division by odd numbers from the last dd finds the smaller prime p≤Rp \le \sqrt R, since RR is composite. In each case the factors found, together with those removed by trial division, are primes whose product is NN. ■\blacksquare

Back to Hart's one line factoring algorithm