The Lucas-Lehmer Test consists of a recursive algorithm performed p-1 times, where p is a prime exponent of the Mersenne Number to be tested, 2p-1
The algorithm is defined as: S1=4, S2 = (S1)2 - 2, ..., SP-1 = (Sp-2)2 - 2
When the last result in the sequence is congruent to zero Mod(2p-1), 2p-1 is a prime number.
Here is an example:
p=7, 2p-1 = 127
S1=4, S2=14, S3=194, S4=37634, S5=1416317954, S6=2005956546822746114
As one can see the numbers become quite large quickly. However, using modulo 127 on each result, the algorithm is simplified:
S1=4, S2=14, S3=67, S4=42, S5=-16, S6=0
Another interesting result is that Sp-2 = ±2(p+1)/2 Mod(2p-1)
There are several ways to generate the sequence of numbers for the Lucas-Lehmer Test. They do not seem to be efficient for large values of p, however they are interesting and may lead to a faster method for computing the last result.
A Binet formula for each result exists:
Sk = (2 + sqrt(3))2(k-1) + (2 - sqrt(3))2(k-1)
Example: k=1, S1 = (2 + sqrt(3))2(1-1) + (2 - sqrt(3))2(1-1)
S1 = (2 + sqrt(3)) + (2 - sqrt(3)) = 4
Example: k=3, S3 = (2 + sqrt(3))2(3-1) + (2 - sqrt(3))2(3-1)
S3 = (2 + sqrt(3))4 + (2 - sqrt(3))4
S3 = (4 + 4sqrt(3)+3)2 + (4 - 4sqrt(3)+3)2
S3 = (7 + 4sqrt(3))2 + (7 - 4sqrt(3))2
S3 = (49 + 56sqrt(3)+48) + (49 - 56sqrt(3)+48)
S3 = 194
Substituting 2=a, sqrt(3)=b, Sk = (a + b)2(k-1) + (a - b)2(k-1)
S1 = 2a = 4
S2 = 2(a2 + b2) = 14
S3 = 2(a4 + 6a2b2 + b4) = 194
S4 = 2(a8 + 28a6b2+ 70a4b4+ 28a2b6 + b8) = 37634
Now to simplify this further we employ Pascal's Triangle. Pascal's Triangle is an easy way to compute the coefficients of the binomial expansion of (a + b)n. The first and last coefficient are always 1. The intermidiate coefficients are generated by adding the two coefficients in the previous row.
| 1 | 1 | (a + b)1 | |||||||||||||||||
| 1 | 2 | 1 | (a + b)2 | ||||||||||||||||
| 1 | 3 | 3 | 1 | (a + b)3 | |||||||||||||||
| 1 | 4 | 6 | 4 | 1 | (a + b)4 | ||||||||||||||
| 1 | 5 | 10 | 10 | 5 | 1 | (a + b)5 | |||||||||||||
| 1 | 6 | 15 | 20 | 15 | 6 | 1 | (a + b)6 | ||||||||||||
| 1 | 7 | 21 | 35 | 35 | 21 | 7 | 1 | (a + b)7 | |||||||||||
| 1 | 8 | 28 | 56 | 70 | 56 | 28 | 8 | 1 | (a + b)8 | = a8b0 + 8a7b1 + 28a6b2 + 56a5b3 + 70a4b4 + 56a3b5 + 28a2b6 + 8a1b7 + a0b8 |
Here's another interesting way to use Pascal's Trinagle. Each row represents the number eleven in any base taken to a power. Lets do base 10 as an example, row 2 is 112 or 121, next row is 113, 1331, etc. However, we have to do a little manipulating when the number in a cell is greater than the base. For example: row 8, 1 8 28 56 70 56 28 8 1. Here we have to use a way to get the correct digits. We divide-up and leave the moduloas the digit.
| Step 1: | 8 | 1 | |||||||
| Step 2: 28 mod(10) | 8 | 8 | 1 | ||||||
| Step 3: 20/10 added to next cell | 58 | 8 | 8 | 1 | |||||
| Step 4: 58 mod(10) | 8 | 8 | 8 | 1 | |||||
| Step 5: 50/10 added to next cell | 75 | 8 | 8 | 8 | 1 | ||||
| Step 6: 75 mod(10) | 5 | 8 | 8 | 8 | 1 | ||||
| Step 7: 70/10 added to next cell | 63 | 5 | 8 | 8 | 8 | 1 | |||
| Step 8: 63 mod(10) | 3 | 5 | 8 | 8 | 8 | 1 | |||
| Step 9: 60/10 added to next cell | 34 | 3 | 5 | 8 | 8 | 8 | 1 | ||
| Step 10: 34 mod(10) | 4 | 3 | 5 | 8 | 8 | 8 | 1 | ||
| Step 11: 30/10 added to next cell | 11 | 4 | 3 | 5 | 8 | 8 | 8 | 1 | |
| Step 12: 11 mod(10) | 1 | 4 | 3 | 5 | 8 | 8 | 8 | 1 | |
| Step 13: 10/10 added to next cell | 2 | 1 | 4 | 3 | 5 | 8 | 8 | 8 | 1 |
| Step 1: | 1 | ||||||||||||
| Step 2: 8 mod(2) | 0 | 1 | |||||||||||
| Step 3: 8/2 added to next cell | 32 | 0 | 1 | ||||||||||
| Step 4: 32 mod(2) | 0 | 0 | 1 | ||||||||||
| Step 5: 32/2 added to next cell | 72 | 0 | 0 | 1 | |||||||||
| Step 6: 72 mod(2) | 0 | 0 | 0 | 1 | |||||||||
| Step 7: 72/2 added to next cell | 106 | 0 | 0 | 0 | 1 | ||||||||
| Step 8: 106 mod(2) | 0 | 0 | 0 | 0 | 1 | ||||||||
| Step 9: 106/2 added to next cell | 109 | 0 | 0 | 0 | 0 | 1 | |||||||
| Step 10: 109 mod(2) | 1 | 0 | 0 | 0 | 0 | 1 | |||||||
| Step 11: 108/2 added to next cell | 82 | 1 | 0 | 0 | 0 | 0 | 1 | ||||||
| Step 12: 82 mod(2) | 0 | 1 | 0 | 0 | 0 | 0 | 1 | ||||||
| Step 13: 82/2 added to next cell | 49 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | |||||
| Step 14: 49 mod(2) | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | |||||
| Step 15: 48/2 added to next cell | 25 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | ||||
| Step 16: 25 mod(2) | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | ||||
| Step 17: 24/2 added to next cell | 12 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | |||
| Step 18: 12 mod(2) | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | |||
| Step 19: 12/2 added to next cell | 6 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | ||
| Step 20: 6 mod(2) | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | ||
| Step 21: 6/2 added to next cell | 3 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | |
| Step 22: 3 mod(2) | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | |
| Step 23: 2/2 added to next cell | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 |