Lucas-Lehmer Test

Theorem
Let $q$ be an odd prime.

Let $\left\langle {L_n} \right\rangle_{n \in \N}$ be the recursive sequence in $\Z / \left({2^q - 1}\right) \Z$ defined by:


 * $L_0 = 4, L_{n+1} = L_n^2 - 2 \pmod{2^q - 1}$

Then $2^q - 1$ is prime $L_{q-2} = 0 \pmod {2^q - 1}$.

Proof
Consider the sequences:
 * $U_0 = 0, U_1 = 1, U_{n+1} = 4 U_n - U_{n-1}$
 * $V_0 = 2, V_1 = 4, V_{n+1} = 4 V_n - V_{n-1}$

The following equations can be proved by induction:

Now, let $p$ be prime and $e \ge 1$.

Suppose $U_n \equiv 0 \pmod {p^e}$.

Then $U_n = b p^e$ for some $b$.

Let $U_{n+1} = a$.

By the recurrence relation and $(4)$, we have:

Similarly:

In general:

Taking $k = p$, we get:

Expanding $\left({2 \pm \sqrt 3}\right)^n$ by the Binomial Theorem, we find that $(2)$ and $(3)$ give us:

Let us set $n = p$ where $p$ is an odd prime.

From Binomial Coefficient of Prime, $\dbinom p k$ is a multiple of $p$ except when $k = 0$ or $k = p$.

We find that:

If $p \ne 3$, then from Fermat's Little Theorem:
 * $3^{p - 1} \equiv 1 \pmod p$

Hence:
 * $\left({3^{\frac {p - 1} 2} - 1}\right) \times \left({3^{\frac {p - 1} 2} + 1}\right) \equiv 0 \pmod p$
 * $3^{\frac {p - 1} 2} \equiv \pm 1 \pmod p$

When $U_p \equiv -1 \pmod p$, we have:
 * $U_{p + 1} = 4 U_p - U_{p - 1} = 4 U_p + V_p - U_{p + 1} \equiv -U_{p + 1} \pmod p$

Hence $U_{p + 1} \equiv 0 \pmod p$

When $U_p \equiv +1 \pmod p$, we have:
 * $U_{p - 1} = 4 U_p - U_{p + 1} = 4 U_p - V_p - U_{p-1} \equiv -U_{p - 1} \pmod p$

Hence $U_{p - 1} \equiv 0 \pmod p$

Thus we have shown that:
 * $(6) \quad \forall p \in \mathbb P: \exists \epsilon \left({p}\right): U_{p + \epsilon \left({p}\right)} \equiv 0 \pmod p$

where $\epsilon \left({p}\right)$ is an integer such that $\left|{\epsilon \left({p}\right)}\right| \le 1$.

Now, let $N \in \N$.

Let $m \in \N$ such that $m \left({N}\right)$ is the smallest positive integer such that:
 * $U_{m \left({N}\right)} \equiv 0 \pmod N$

Let $a \equiv U_{m + 1} \pmod N$.

Then $a \perp N$ because:
 * $\gcd \left\{{U_n, U_{n+1}}\right\} = 1$

Hence the sequence:
 * $U_m, U_{m + 1}, U_{m + 2}, \ldots$

is congruent modulo $N$ to $a U_0, a U_1, a U_2, \ldots$.

Then we have:
 * $(7) \quad U_n \equiv 0 \pmod N \iff n = k m \left({N}\right)$

for some integral $k$.

(This number $m \left({N}\right)$ is called the rank of apparition of $N$ in the sequence.)

Now, we have defined the sequence $\left \langle {L_n} \right \rangle$ as:
 * $L_0 = 4, L_{n+1} = \left({L_n^2 - 2}\right) \pmod {\left({2^q - 1}\right)}$

By induction it follows that:
 * $L_n \equiv V_{2^n} \pmod {\left({2^q - 1}\right)}$

We have the identity:
 * $2 U_{n+1} = 4 U_n + V_n$

So any common factor of $U_n$ and $V_n$ must divide $U_n$ and $2 U_{n+1}$.

As $U_n \perp U_{n+1}$, this implies that $\gcd \left\{{U_n, V_n}\right\} \le 2$.

So $U_n$ and $V_n$ have no odd factor in common.

So, if $L_{q-2} = 0$:

Now, if $m = m \left({2^q - 1}\right)$ is the rank of apparition of $2^q - 1$, it must be a divisor of $2^{q - 1}$ but not of $2^{q - 2}$. So $m = 2^{q - 1}$.

Now we prove that $n = 2^q - 1$ must therefore be prime.

Let the prime decomposition of $n$ be $p_1^{e_1} \ldots p_r^{e_r}$.

All primes $p_j$ are greater than $3$ because $n$ is odd and congruent to $\left({-1}\right)^q - 1 = -2 \pmod 3$.

From $(5), (6), (7)$ we know that $U_t \equiv 0 \pmod {2^q - 1}$, where:
 * $t = \operatorname {lcm} \left\{{p_1^{e_1-1} \left({p_1 + \epsilon_1}\right), \ldots, p_r^{e_r-1} \left({p_r + \epsilon_r}\right)}\right\}$

where each $\epsilon_j = \pm 1$.

It follows that $t$ is a multiple of $m = 2^{q-1}$.

Let $\displaystyle n_0 = \prod_{j \mathop = 1}^r p_j^{e_j - 1} \left({p_j + \epsilon_j}\right)$.

We have:
 * $\displaystyle n_0 \le \prod_{j \mathop = 1}^r p_j^{e_j - 1} \left({p_j + \frac {p_j} 5}\right) = \left({\frac 6 5}\right)^r n$

Also, because $p_j + \epsilon_j$ is even, $t \le \frac {n_0} {2^{r-1}}$, because a factor of $2$ is lost every time the LCM of two even numbers is taken.

Combining these results, we have:
 * $m \le t \le 2 \left({\frac 3 5}\right)^r n \le 4 \left({\frac 3 5}\right)^r m < 3 m$

Hence $r \le 2$ and $t = m$ or $t = 2 m$, a power of $2$.

Therefore $e_1 = 1$ and $e_r = 1$.

If $n$ is not prime, we must have:
 * $n = 2^q - 1 = \left({2^k + 1}\right) \left({2^l - 1}\right)$

where $\left({2^k + 1}\right)$ and $\left({2^l - 1}\right)$ are prime.

When $q$ is odd, that last factorization is obviously impossible, so $n$ is prime.

Conversely, suppose $n = 2^q - 1$ is prime.

We need to show that $V_{2^{q-2}} \equiv 0 \pmod n$.

All we need to do is show:
 * $V_{2^{q-1}} \equiv -2 \pmod n$

because:
 * $V_{2^{q-1}} = \left({V_{2^{q-2}}}\right)^2 - 2$

Now:

Since $n$ is an odd prime, the binomial coefficient:
 * $\dbinom {n + 1} {2 k} = \dbinom n {2 k} + \binom n {2 k - 1}$

is divisible by $n$ except when $2k = 0$ and $2k = n+1$, from Binomial Coefficient of Prime.

Hence:
 * $2^{\frac{n-1} 2} V_{2^{q-1}} \equiv 1 + 3^{\frac {n+1} 2} \pmod n$.

Here:
 * $2 \equiv \left({2^{\frac {q + 1} 2} }\right)^2$

so by Fermat's Little Theorem:
 * $2^{\frac {n - 1} 2} \equiv \left({2^{\frac {q + 1} 2} }\right)^{n-1} \equiv i$

Finally, by the Law of Quadratic Reciprocity:
 * $3^{\frac{n - 1} 2} \equiv -1$

since $n \bmod 3 = 1$ and $n \bmod 4 = 3$.

This means:
 * $V_{2^{q - 1}} \equiv -2$

Hence:
 * $V_{2^{q-2}} \equiv 0$

as required.

Note
This calculation is particularly suited to binary digital computers, since calculation $\pmod {2^q - 1}$ is very convenient.

Thus we have a relatively quick way to determine the primality of Mersenne numbers.