Algorithm Search⌘K

Proofs: Lucas-Lehmer test

These are the human proofs of Lucas-Lehmer test, at full length.

The Lucas-Lehmer test returns true exactly when 2^p - 1 is prime full

This proof shows: correctness, termination.

We use the pseudocode LUCAS-LEHMER(p)(p) of the page, for an integer p≥2p \ge 2, with Mp=2p−1M_p = 2^p - 1. Define the integers s0=4s_0 = 4 and si+1=si2−2s_{i+1} = s_i^2 - 2.

Termination. The loop of line L4 runs p−2p - 2 times, and each pass does one squaring and one reduction. So the call ends.

Lemma 1 (the loop computes sp−2s_{p-2}). For p≥3p \ge 3, after the ii-th pass of line L5, s≡si(modMp)s \equiv s_i \pmod{M_p}. So line L6 returns true if and only if Mp∣sp−2M_p \mid s_{p-2}.

Proof. Before the loop s=4=s0s = 4 = s_0, and 4<Mp4 < M_p as p≥3p \ge 3. If s≡sis \equiv s_i, then s2−2≡si2−2=si+1s^2 - 2 \equiv s_i^2 - 2 = s_{i+1}, and line L5 keeps the residue. After p−2p - 2 passes, ss is the residue of sp−2s_{p-2} in [0,Mp)[0, M_p), which is 0 if and only if Mp∣sp−2M_p \mid s_{p-2}. □\square

The ring. For an odd prime qq, let Rq=Zq[x]/(x2−3)R_q = \mathbb{Z}_q[x]/(x^2 - 3), and write 3\sqrt 3 for the class of xx. Its elements are a+b3a + b\sqrt 3 with a,b∈Zqa, b \in \mathbb{Z}_q, so ∣Rq∣=q2|R_q| = q^2, and the units of RqR_q number at most q2−1q^2 - 1, since 0 is not a unit. Put ω=2+3\omega = 2 + \sqrt 3 and ωˉ=2−3\bar\omega = 2 - \sqrt 3. Then ωωˉ=4−3=1\omega \bar\omega = 4 - 3 = 1, so both are units and ωˉ=ω−1\bar\omega = \omega^{-1}.

Lemma 2 (closed form). In RqR_q, the image of sis_i is ω2i+ωˉ2i\omega^{2^i} + \bar\omega^{2^i} for each i≥0i \ge 0.

Proof. For i=0i = 0, ω+ωˉ=4\omega + \bar\omega = 4. If the claim holds for ii, then si+1=si2−2=ω2i+1+2(ωωˉ)2i+ωˉ2i+1−2=ω2i+1+ωˉ2i+1s_{i+1} = s_i^2 - 2 = \omega^{2^{i+1}} + 2(\omega\bar\omega)^{2^i} + \bar\omega^{2^{i+1}} - 2 = \omega^{2^{i+1}} + \bar\omega^{2^{i+1}}. □\square

Lemma 3 (if Mp∣sp−2M_p \mid s_{p-2}, then MpM_p is prime). Let p≥3p \ge 3 and Mp∣sp−2M_p \mid s_{p-2}. Then MpM_p is prime.

Proof. Suppose MpM_p is composite, and let qq be its smallest prime factor. Then q2≤Mpq^2 \le M_p, and qq is odd since MpM_p is odd. As q∣sp−2q \mid s_{p-2}, Lemma 2 gives ω2p−2+ωˉ2p−2=0\omega^{2^{p-2}} + \bar\omega^{2^{p-2}} = 0 in RqR_q. Multiplying by ω2p−2\omega^{2^{p-2}} and using ωωˉ=1\omega\bar\omega = 1 gives ω2p−1+1=0\omega^{2^{p-1}} + 1 = 0, so ω2p−1=−1\omega^{2^{p-1}} = -1 and ω2p=1\omega^{2^p} = 1. Since qq is odd, −1≠1-1 \ne 1 in RqR_q, so the order of ω\omega in the unit group divides 2p2^p but not 2p−12^{p-1}, and it is 2p2^p. The order of an element is at most the size of the group, so 2p≤q2−1≤Mp−1=2p−22^p \le q^2 - 1 \le M_p - 1 = 2^p - 2, which is false. □\square

Lemma 4 (residue facts). Let p≥3p \ge 3 and let q=Mpq = M_p be prime. Then pp is odd, q≡7(mod8)q \equiv 7 \pmod 8 and q≡1(mod3)q \equiv 1 \pmod 3. Hence 2 is a quadratic residue modulo qq and 3 is not.

Proof. If pp were even, 3=22−13 = 2^2 - 1 would divide 2p−12^p - 1, and q>3q > 3; so pp is odd. As p≥3p \ge 3, 2p≡0(mod8)2^p \equiv 0 \pmod 8, so q≡−1≡7(mod8)q \equiv -1 \equiv 7 \pmod 8. As pp is odd, 2p≡(−1)p=−1≡2(mod3)2^p \equiv (-1)^p = -1 \equiv 2 \pmod 3, so q≡1(mod3)q \equiv 1 \pmod 3. By the second supplement of quadratic reciprocity, 2 is a residue modulo qq because q≡±1(mod8)q \equiv \pm 1 \pmod 8. Since q≡3(mod4)q \equiv 3 \pmod 4 and 3≡3(mod4)3 \equiv 3 \pmod 4, quadratic reciprocity gives (3q)=−(q3)=−(13)=−1\left(\frac{3}{q}\right) = -\left(\frac{q}{3}\right) = -\left(\frac{1}{3}\right) = -1. □\square

Lemma 5 (if MpM_p is prime, then Mp∣sp−2M_p \mid s_{p-2}). Let p≥3p \ge 3 and let q=Mpq = M_p be prime. Then q∣sp−2q \mid s_{p-2}.

Proof. By Lemma 4, x2−3x^2 - 3 has no root modulo qq, so it is irreducible over Zq\mathbb{Z}_q and RqR_q is a field with q2q^2 elements. In it, u↦uqu \mapsto u^q is a ring homomorphism (the binomial coefficients (qj)\binom{q}{j} with 0<j<q0 < j < q vanish), it fixes Zq\mathbb{Z}_q by Fermat's little theorem, and (3)q=3(q−1)/23=−3(\sqrt 3)^q = 3^{(q-1)/2} \sqrt 3 = -\sqrt 3 by Euler's criterion and Lemma 4. So (a+b3)q=a−b3(a + b\sqrt 3)^q = a - b\sqrt 3.

Let τ=1+3\tau = 1 + \sqrt 3. Then τ2=4+23=2ω\tau^2 = 4 + 2\sqrt 3 = 2\omega, and τq+1=τ⋅τq=(1+3)(1−3)=−2\tau^{q+1} = \tau \cdot \tau^q = (1 + \sqrt 3)(1 - \sqrt 3) = -2. On the other hand, τq+1=(τ2)(q+1)/2=2(q+1)/2ω(q+1)/2\tau^{q+1} = (\tau^2)^{(q+1)/2} = 2^{(q+1)/2} \omega^{(q+1)/2}, and 2(q+1)/2=2⋅2(q−1)/2=22^{(q+1)/2} = 2 \cdot 2^{(q-1)/2} = 2 by Euler's criterion, since 2 is a residue. Hence 2ω(q+1)/2=−22\omega^{(q+1)/2} = -2, and as 22 is invertible, ω(q+1)/2=−1\omega^{(q+1)/2} = -1. Here (q+1)/2=2p−1(q + 1)/2 = 2^{p-1}. So ω2p−1=−1\omega^{2^{p-1}} = -1, and

sp−2=ω2p−2+ωˉ2p−2=ωˉ2p−2(ω2p−1+1)=0s_{p-2} = \omega^{2^{p-2}} + \bar\omega^{2^{p-2}} = \bar\omega^{2^{p-2}} \left(\omega^{2^{p-1}} + 1\right) = 0

in RqR_q, using ω2p−2=ωˉ2p−2ω2p−1\omega^{2^{p-2}} = \bar\omega^{2^{p-2}} \omega^{2^{p-1}}. As sp−2s_{p-2} is an integer whose image in RqR_q is 0, q∣sp−2q \mid s_{p-2}. □\square

Theorem. For each integer p≥2p \ge 2, LUCAS-LEHMER(p)(p) returns true if and only if MpM_p is prime.

Proof. For p=2p = 2, line L1 returns true, and M2=3M_2 = 3 is prime. For p≥3p \ge 3, by Lemma 1 the call returns true if and only if Mp∣sp−2M_p \mid s_{p-2}. By Lemma 3 this implies that MpM_p is prime, and by Lemma 5 it follows from it. ■\blacksquare

Remark. Lemma 3 does not use that pp is prime. So for a composite p≥4p \ge 4 the test returns false, which is right because MpM_p is then composite.

Back to Lucas-Lehmer test