I've always wondered why the Lucas-Lehmer Primality Test works. After studying it, I came up with an explanation. I hope you can help me confirm and complete it.
A Mersenne prime is a number of the form Mp = 2p – 1, when Mp and p are primes.
The basic Lucas-Lehmer Test routine for evaluating a number Mp is as follows:
a = 4
M = 2p – 1
repeat p – 2 times:
a = ( a2 – 2 ) mod M
if a = 0 then M is a Mersenne prime, otherwise it is not.
To optimize the process, the module calculation is performed at each repetition. This prevents the value of a turns into an excessively large number.
If we didn't use the modulus, the equation Ap+1 = ( Ap2 – 2 ) would generate Sequence A (OEIS A003010):
A0 = 4
A1 = 14
A2 = 194
A3 = 37634
A4 = 1416317954
A5 = 2005956546822746114
To evaluate the Mersenne prime M5 = 31 = 25 – 1, the repetition is done 3 times (= 5 – 2), which corresponds to A3 = 37634 which is divisible by 31.
The interesting point is that we can also use An – 2 to carry out the evaluation. In this case, A5 – 2 is divisible by 31.
Carry out the computation with A3 instead of A5 is more computationally efficient, as it involves much smaller values.
Justification of why, when An-2 is divisible by Mn, An – 2 is also:
For a Mersenne prime Mn, according to Lucas-Lehmer, we have that:
An-2 is a multiple of Mn
An-1 = An-22 – 2
An = An-12 – 2
Consequently,
An = (An-22 – 2)2 – 2 = An-24 – 4 . An-22 + 2
And since An-2 is a multiple of Mn let's replace An-24 – 4 . An-22 by k . Mn:
An = k . Mn + 2
Or
An – 2 = k . Mn, a multiple of Mn.
In modular arithmetic this is represented as An – 2 ≡ 0 (mod Mn).
So, if A3 is divisible by 31, then A5 – 2 is too.
Another example:
The Mersenne prime M3 = 23 – 1 = 7 is a divisor of A1 = 14 (where, 1 = 3 – 2).
And M3 is also a divisor of A3 – 2 = 37632.
Now we will see the origin of the Sequence A property.
There is another sequence, with a special property, which is generated by equation Bm = 4 . Bm-1 – Bm-2, Sequence B (OEIS A003500):
B1 = 4 = A0
B2 = 14 = A1
B3 = 52
B4 = 194 = A2
B5 = 724
B6 = 2702
B7 = 10084
B8 = 37634 = A3
B9 = 140452
B10 = 524174
B11 = 1956244
...
B31 = 537494436773078404
B32 = 2005956546822746114 = A5
Note that the values in sequence A are a subset of sequence B, as indicated. Matching with A occurs when the subscript m, of Bm, has the value of a power of 2 (1, 2, 4, 8, etc.). Therefore, A3 has the same value as B8, because 23=8.
Quick summary
Prime numbers p divides the corresponding values of Bp minus 4, that is
Bp – 4 ≡ 0 (mod p).
For example, 31 is a divisor of (B31 – 4) and 7 is a divisor of (B7 – 4).
Mersenne primes furthermore divide the value of Bp+1 minus 2:
Bp+1 – 2 ≡ 0 (mod p).
In this case, 31 also divides (B32 – 2). And 7 divides (B8 – 2).
So, for all Mersenne primes, Bp+1 are powers of 2 and have the same values as An, in which 2n = p + 1.
That is, 31 divides (B32 – 2) which has the same value as (A5 – 2), where 5 = log2(32).
And, if An – 2 ≡ 0 (mod Mn), we have that An-2 ≡ 0 (mod Mn), as we saw previously, which is the Lucas-Lehmer Test.
The Test works because primes p divide the respective Bp values minus 4.
Some questions and comments:
Although I have not found a proof that prime numbers divide their respective values of sequence B minus 4, I have verified that this is true up to 1000000. Does anyone know of a proof of this property?
Note that there are composite numbers that also divide the respective values of sequence B minus 4, for example, 10, 209, 230, etc. (OEIS A335673).
In addition, the prime m divides (Bm – 4) and also divides another value, either (Bm-2 – 4) or (Bm+2 – 4). Some primes and the Mersenne primes divide (Bm – 4) and also divide (Bm+2 – 4). For example, M3 = 7, in addition to dividing B7 – 4 (=10080), divides B9 – 4 (=140448). Other primes divide (Bm – 4) and (Bm-2 – 4), for instance, prime 11, which divides (B11 – 4) and (B9 – 4).
I have not found a demonstration of why this occurs, but the fact, proven by applying the Lucas-Lehmer Test, is that the Mersenne primes divide (Bm – 4) and (Bm+2 – 4).
Therefore, knowing that
Bm+2 = 4 . Bm+1 – Bm and also that
Bm – 4 ≡ 0 (mod M) and
Bm+2 – 4 ≡ 0 (mod M) we can calculate that
Bm+1 – 2 ≡ 0 (mod M).
And, as Mersenne prime Bm+1 is the same as An then
An – 2 ≡ 0 (mod M) and, consequently,
An-2 ≡ 0 (mod M), which is the Lucas-Lehmer Test.