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 , and be integers. For an iteration , write , and , 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 passes. So the algorithm stops, at line L2, L5 or L6.
Lemma 1 (the size of ). , and .
Proof. by the choice of , so . Since is the smallest integer with , we have , so , and because both sides are integers. Finally , so .
Lemma 2 (a returned value is a proper factor). Suppose line L5 runs at iteration , so and with an integer . Then satisfies and .
Proof. From Lemma 1, , because . So , and . Then
so divides . Here .
The value divides by definition. If , then with , which is impossible. If , then is coprime to , and since divides , it divides . But , which is again impossible. So .
Corollary 3 (primes and failures). If is prime, the algorithm returns . For every , the output is or a proper factor of .
Proof. The algorithm returns only at lines L2 and L6, with the value , or at line L5. By Lemma 2, a return at line L5 gives a proper factor of . A prime has no proper factor, so for a prime line L5 never runs, and the output is .
Lemma 4 (when an iteration succeeds). Suppose with integers of the same parity, , and . Then, if the loop reaches iteration , it stops there at line L5 with and .
Proof. Put and , integers because and have the same parity. Then , so and . Also
so , and . Then is a square, and the square root found at line L4 is . The range test of line L2 passes because . So the algorithm reaches line L5 at iteration .
Lemma 4 explains the example of the main page: with , so iteration gives and .
Theorem 5 (the full factorization). Let , let , and suppose that trial division by every with divides out all such factors from , with the part left at the end. Then is , a prime, the square of a prime, or a product of two distinct primes. Splitting by a primality test, a square test, OLF and, if OLF fails, trial division up to , gives the prime factorization of .
Proof. The trial division loop stops either because or because . Every prime factor of is larger than every that was tried, since those were divided out.
If the loop stopped because , then every prime factor of is at least , so has at most one prime factor counted with multiplicity: is or a prime.
If it stopped because , then every prime factor of is larger than . If had three prime factors counted with multiplicity, their product would be larger than , a contradiction. So is , a prime , a square , or a product of two distinct primes.
A primality test settles the cases and . The case is a perfect square, and the integer square root gives . In the case , is composite and not a square; OLF returns a proper factor or by Corollary 3. A proper factor of is or , so is the factorization into two primes. If OLF fails, trial division by odd numbers from the last finds the smaller prime , since is composite. In each case the factors found, together with those removed by trial division, are primes whose product is .