Proofs: Lucas primality test
These are the human proofs of Lucas primality test, at full length.
The Lucas test returns true exactly when n is prime full
This proof shows: correctness, termination.
We use the pseudocode LUCAS of the page. The input is an integer and, for , the list of the distinct primes that divide . Write with each .
Lemma 1 (orders). Let , let be an integer with , and let be the least with . Then , , and .
Proof. , so is a unit modulo . Divide: with ; then , so by the choice of . The units modulo form a group of order , and is the order of in it, so by Lagrange's theorem.
Lemma 2 (bases imply primality). Let . Suppose that for each there is an integer with and . Then is prime.
Proof. Let be the order of . By Lemma 1, . If , then, as , the power of in is at most , so and , a contradiction. So , and by Lemma 1, . The are pairwise coprime, so their product divides . Since , this forces . Then each of is coprime to , so has no divisor with , and is prime.
Lemma 3 (primes have the bases). Let be prime, and let be a primitive root modulo . Then and for each .
Proof. The group of units modulo a prime is cyclic of order , so a primitive root exists, with order . Then , and since , .
Lemma 4 (each search ends). Let . For each , the loop of lines L4 to L7 ends after at most bases, where is the smallest prime factor of if is composite, and the least primitive root of if is prime, with .
Proof. The loop tries and ends at line L5 or line L6. If is composite and the loop reaches , then , so (as ), and line L5 returns. If is prime and the loop reaches , then by Lemma 3 line L5 does not fire and line L6 exits the loop. In both cases the loop ends no later than at that base, which is at most .
Theorem. For every valid input, LUCAS ends, and it returns true if and only if is prime.
Proof. Termination. Line L1 ends the call for . Otherwise the outer loop has passes, and each inner loop ends by Lemma 4.
The case . Line L1 returns true for , the only prime below 3, and false for and .
False is right. Let be prime. By Lemma 4, every base tried satisfies , so , and by Fermat's little theorem. So line L5 never returns false for a prime, and line L1 does not either. Hence false is returned only for composite or .
True is right. Line L8 is reached only when, for each , line L6 exited the loop with some base . That base passed line L5, so , and line L6 gives . By Lemma 2, is prime.
Primes return true. If is prime, no base ever fails line L5, and by Lemma 4 each search exits through line L6. So the call reaches line L8 and returns true.