Lucas-Lehmer Test


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.
11   (a + b)1
121 (a + b)2
1331 (a + b)3
14641 (a + b)4
15101051 (a + b)5
1615201561 (a + b)6
172135352171 (a + b)7
18285670562881(a + b)8 = a8b0 + 8a7b1 + 28a6b2 + 56a5b3 + 70a4b4 + 56a3b5 + 28a2b6 + 8a1b7 + a0b8

Since the Binet formula is raised to a power of 2, these are the only rows of Pascal's Trinagle we are interested in, 1,2,4,8,16,32, etc. Plus, we are only interested in every-other coefficient. Now we have another way of computing the last result in the algorithm for the Lucas-Lehmer Test. S4, above, is equal to, 2 times the binomial expansion of every-other term.
The coefficients can also be computed using the Combination of n things taken r at a time: C(n,r) = n!/((n-r)!(r!)). For (a + b)8 the coefficients are C(8,8), C(8,7), C(8,6), C(8,5), C(8,4), C(8,3), C(8,2), C(8,1), C(8,0).


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:81
Step 2: 28 mod(10)881
Step 3: 20/10 added to next cell58881
Step 4: 58 mod(10)8881
Step 5: 50/10 added to next cell758881
Step 6: 75 mod(10)58881
Step 7: 70/10 added to next cell6358881
Step 8: 63 mod(10)358881
Step 9: 60/10 added to next cell34358881
Step 10: 34 mod(10)4358881
Step 11: 30/10 added to next cell114358881
Step 12: 11 mod(10)14358881
Step 13: 10/10 added to next cell214358881
We're finished 118 = 214358881

This works for any base where the representation of the desired number is 11. In binary that would be a 3 in base 10. The same process is applied. However, we would Mod(2) and divide by 2. So here it is for base 2:
Step 1:1
Step 2: 8 mod(2)01
Step 3: 8/2 added to next cell3201
Step 4: 32 mod(2)001
Step 5: 32/2 added to next cell72001
Step 6: 72 mod(2)0001
Step 7: 72/2 added to next cell1060001
Step 8: 106 mod(2)00001
Step 9: 106/2 added to next cell10900001
Step 10: 109 mod(2)100001
Step 11: 108/2 added to next cell82100001
Step 12: 82 mod(2)0100001
Step 13: 82/2 added to next cell490100001
Step 14: 49 mod(2)10100001
Step 15: 48/2 added to next cell2510100001
Step 16: 25 mod(2)110100001
Step 17: 24/2 added to next cell12110100001
Step 18: 12 mod(2)0110100001
Step 19: 12/2 added to next cell60110100001
Step 20: 6 mod(2)00110100001
Step 21: 6/2 added to next cell300110100001
Step 22: 3 mod(2)100110100001
Step 23: 2/2 added to next cell1100110100001


© copyright 1996-2006