Algorithm Search⌘K

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(n,q1,…,qm)(n, q_1, \ldots, q_m) of the page. The input is an integer n≥0n \ge 0 and, for n≥3n \ge 3, the list of the distinct primes q1,…,qmq_1, \ldots, q_m that divide n−1n - 1. Write n−1=q1e1⋯qmemn - 1 = q_1^{e_1} \cdots q_m^{e_m} with each ei≥1e_i \ge 1.

Lemma 1 (orders). Let n≥2n \ge 2, let aa be an integer with an−1≡1(modn)a^{n-1} \equiv 1 \pmod n, and let rr be the least t≥1t \ge 1 with at≡1(modn)a^t \equiv 1 \pmod n. Then gcd⁡(a,n)=1\gcd(a, n) = 1, r∣n−1r \mid n - 1, and r∣φ(n)r \mid \varphi(n).

Proof. a⋅an−2≡1a \cdot a^{n-2} \equiv 1, so aa is a unit modulo nn. Divide: n−1=cr+tn - 1 = cr + t with 0≤t<r0 \le t < r; then at=an−1(ar)−c≡1a^t = a^{n-1} (a^r)^{-c} \equiv 1, so t=0t = 0 by the choice of rr. The units modulo nn form a group of order φ(n)\varphi(n), and rr is the order of aa in it, so r∣φ(n)r \mid \varphi(n) by Lagrange's theorem. □\square

Lemma 2 (bases imply primality). Let n≥3n \ge 3. Suppose that for each ii there is an integer aia_i with ain−1≡1a_i^{n-1} \equiv 1 and ai(n−1)/qi≢1(modn)a_i^{(n-1)/q_i} \not\equiv 1 \pmod n. Then nn is prime.

Proof. Let rir_i be the order of aia_i. By Lemma 1, ri∣n−1r_i \mid n - 1. If qiei∤riq_i^{e_i} \nmid r_i, then, as ri∣n−1r_i \mid n - 1, the power of qiq_i in rir_i is at most ei−1e_i - 1, so ri∣(n−1)/qir_i \mid (n - 1)/q_i and ai(n−1)/qi≡1a_i^{(n-1)/q_i} \equiv 1, a contradiction. So qiei∣riq_i^{e_i} \mid r_i, and by Lemma 1, qiei∣φ(n)q_i^{e_i} \mid \varphi(n). The qieiq_i^{e_i} are pairwise coprime, so their product n−1n - 1 divides φ(n)\varphi(n). Since 1≤φ(n)≤n−11 \le \varphi(n) \le n - 1, this forces φ(n)=n−1\varphi(n) = n - 1. Then each of 1,…,n−11, \ldots, n - 1 is coprime to nn, so nn has no divisor dd with 1<d<n1 < d < n, and nn is prime. □\square

Lemma 3 (primes have the bases). Let n≥3n \ge 3 be prime, and let gg be a primitive root modulo nn. Then gn−1≡1g^{n-1} \equiv 1 and g(n−1)/qi≢1(modn)g^{(n-1)/q_i} \not\equiv 1 \pmod n for each ii.

Proof. The group of units modulo a prime is cyclic of order n−1n - 1, so a primitive root gg exists, with order n−1n - 1. Then gn−1≡1g^{n-1} \equiv 1, and since 0<(n−1)/qi<n−10 < (n - 1)/q_i < n - 1, g(n−1)/qi≢1g^{(n-1)/q_i} \not\equiv 1. □\square

Lemma 4 (each search ends). Let n≥3n \ge 3. For each ii, the loop of lines L4 to L7 ends after at most p−1p - 1 bases, where pp is the smallest prime factor of nn if nn is composite, and the least primitive root gg of nn if nn is prime, with g≤n−1g \le n - 1.

Proof. The loop tries a=2,3,…a = 2, 3, \ldots and ends at line L5 or line L6. If nn is composite and the loop reaches a=pa = p, then p∣an−1p \mid a^{n-1}, so an−1≢1(modn)a^{n-1} \not\equiv 1 \pmod n (as p∤1p \nmid 1), and line L5 returns. If nn is prime and the loop reaches a=ga = g, 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 n−1n - 1. □\square

Theorem. For every valid input, LUCAS(n,q1,…,qm)(n, q_1, \ldots, q_m) ends, and it returns true if and only if nn is prime.

Proof. Termination. Line L1 ends the call for n<3n < 3. Otherwise the outer loop has mm passes, and each inner loop ends by Lemma 4.

The case n<3n < 3. Line L1 returns true for n=2n = 2, the only prime below 3, and false for n=0n = 0 and n=1n = 1.

False is right. Let n≥3n \ge 3 be prime. By Lemma 4, every base tried satisfies 2≤a≤g≤n−12 \le a \le g \le n - 1, so gcd⁡(a,n)=1\gcd(a, n) = 1, and an−1≡1(modn)a^{n-1} \equiv 1 \pmod n 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 nn or n<2n < 2.

True is right. Line L8 is reached only when, for each ii, line L6 exited the loop with some base aia_i. That base passed line L5, so ain−1≡1a_i^{n-1} \equiv 1, and line L6 gives ai(n−1)/qi≢1a_i^{(n-1)/q_i} \not\equiv 1. By Lemma 2, nn is prime.

Primes return true. If n≥3n \ge 3 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. ■\blacksquare

Back to Lucas primality test