Lucas-Lehmer Test

Theorem
Let $$q$$ be an odd prime.

Let $$\left \langle {L_n} \right \rangle$$ be a sequence in $\N$ defined by:
 * $$L_0 = 4, L_{n+1} = \left({L_n^2 - 2}\right) \bmod\, \left({2^q - 1}\right)$$.

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

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 \left({\bmod\, p^{e}}\right)$$.

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.

Then we note that $$\binom p k$$ is a multiple of $$p$$ except when $$k = 0$$ or $$k = p$$ from Binomial Coefficient of Prime.

We find that:

$$ $$

If $$p \ne 3$$, then from Fermat's Little Theorem we have $$3^{p-1} \equiv 1 \left({\bmod\, p}\right)$$.

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

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