Lucas–Lehmer primality test

From Wikipedia, the free encyclopedia
Revision as of 12:01, 1 June 2025 by imported>Cerniagigante (The test: Fixed grammar)
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:Short description Script error: No such module "about".

In mathematics, the Lucas–Lehmer test (LLT) is a primality test for Mersenne numbers. The test was originally developed by Édouard Lucas in 1878[1] and subsequently proved by Derrick Henry Lehmer in 1930.

The test

The Lucas–Lehmer test works as follows. Let Mp = 2p − 1 be the Mersenne number to test with p an odd prime. The primality of p can be efficiently checked with a simple algorithm like trial division since p is exponentially smaller than Mp. Define a sequence {si} for all i ≥ 0 by

si={4if i=0;si122otherwise.

The first few terms of this sequence are 4, 14, 194, 37634, ... (sequence A003010 in the OEIS). Then Mp is prime if and only if

sp20(modMp).

The number sp − 2 mod Mp is called the Lucas–Lehmer residue of p. (Some authors equivalently set s1 = 4 and test sp−1 mod Mp). In pseudocode, the test might be written as

// Determine if Mp = 2p − 1 is prime for p > 2
Lucas–Lehmer(p)
    var s = 4
    var M = 2p − 1
    repeat p − 2 times:
        s = ((s × s) − 2) mod M
    if s == 0 return PRIME else return COMPOSITE

Performing the mod M at each iteration ensures that all intermediate results are at most p bits (otherwise the number of bits would double each iteration). The same strategy is used in modular exponentiation.

Alternative starting values

Starting values s0 other than 4 are possible, for instance 10, 52, and others (sequence A018844 in the OEIS).[2] The Lucas–Lehmer residue calculated with these alternative starting values will still be zero if Mp is a Mersenne prime. However, the terms of the sequence will be different and a non-zero Lucas-Lehmer residue for non-prime Mp will have a different numerical value from the non-zero value calculated when s0 = 4.

It is also possible to use the starting value (2 mod Mp)(3 mod Mp)−1, usually denoted by 2/3 for short.[2] This starting value equals (2p + 1) /3, the Wagstaff number with exponent p.

Starting values like 4, 10, and 2/3 are universal, that is, they are valid for all (or nearly all) p. There are infinitely many additional universal starting values.[2] However, some other starting values are only valid for a subset of all possible p, for example s0 = 3 can be used if p = 3 (mod 4).[3] This starting value was often used where suitable in the era of hand computation, including by Lucas in proving M127 prime.[4] The first few terms of the sequence are 3, 7, 47, ... (sequence A001566 in the OEIS).

Sign of penultimate term

If sp−2 = 0 mod Mp then the penultimate term is sp−3 = ± 2(p+1)/2 mod Mp. The sign of this penultimate term is called the Lehmer symbol ϵ(s0p).

In 2000 S.Y. Gebre-Egziabher proved that for the starting value 2/3 and for p ≠ 5 the sign is:

ϵ(23, p)=(1)p12

That is, ϵ(2/3, p) = +1 if p = 1 (mod 4) and p ≠ 5.[2]

The same author also proved Woltman's conjecture[5] that the Lehmer symbols for starting values 4 and 10 when p is not 2 or 5 are related by:

ϵ(10, p)=ϵ(4, p) × (1)(p+1)(p+3)8

That is, ϵ(4, p) × ϵ(10, p) = 1 if p = 5 or 7 (mod 8) and p ≠ 2, 5.[2]

OEIS sequence A123271 shows ϵ(4, p) for each Mersenne prime Mp.

Time complexity

In the algorithm as written above, there are two expensive operations during each iteration: the multiplication s × s, and the mod M operation. The mod M operation can be made particularly efficient on standard binary computers by observing that

k(kmod2n)+k/2n(mod2n1).

This says that the least significant n bits of k plus the remaining bits of k are equivalent to k modulo 2n−1. This equivalence can be used repeatedly until at most n bits remain. In this way, the remainder after dividing k by the Mersenne number 2n−1 is computed without using division. For example,

916 mod 25−1 = 11100101002 mod 25−1
= ((916 mod 25) + int(916 ÷ 25)) mod 25−1
= (101002 + 111002) mod 25−1
= 1100002 mod 25−1
= (100002 + 12) mod 25−1
= 100012 mod 25−1
= 100012
= 17.

Moreover, since s × s will never exceed M2 < 22p, this simple technique converges in at most 1 p-bit addition (and possibly a carry from the pth bit to the 1st bit), which can be done in linear time. This algorithm has a small exceptional case. It will produce 2n−1 for a multiple of the modulus rather than the correct value of 0. However, this case is easy to detect and correct.

With the modulus out of the way, the asymptotic complexity of the algorithm only depends on the multiplication algorithm used to square s at each step. The simple "grade-school" algorithm for multiplication requires O(p2) bit-level or word-level operations to square a p-bit number. Since this happens O(p) times, the total time complexity is O(p3). A more efficient multiplication algorithm is the Schönhage–Strassen algorithm, which is based on the Fast Fourier transform. It only requires O(p log p log log p) time to square a p-bit number. This reduces the complexity to O(p2 log p log log p) or Õ(p2).[6] An even more efficient multiplication algorithm, Fürer's algorithm, only needs plogp 2O(log*p) time to multiply two p-bit numbers.

By comparison, the most efficient randomized primality test for general integers, the Miller–Rabin primality test, requires O(k n2 log n log log n) bit operations using FFT multiplication for an n-digit number, where k is the number of iterations and is related to the error rate. For constant k, this is in the same complexity class as the Lucas-Lehmer test, and is similarly fast in practice.

The most efficient deterministic primality test for any n-digit number, the AKS primality test, requires Õ(n6) bit operations in its best known variant and is extremely slow even for relatively small values.

Examples

The Mersenne number M3 = 23−1 = 7 is prime. The Lucas–Lehmer test verifies this as follows. Initially s is set to 4 and then is updated 3−2 = 1 time:

  • s ← ((4 × 4) − 2) mod 7 = 0.

Since the final value of s is 0, the conclusion is that M3 is prime.

On the other hand, M11 = 2047 = 23 × 89 is not prime. Again, s is set to 4 but is now updated 11−2 = 9 times:

  • s ← ((4 × 4) − 2) mod 2047 = 14
  • s ← ((14 × 14) − 2) mod 2047 = 194
  • s ← ((194 × 194) − 2) mod 2047 = 788
  • s ← ((788 × 788) − 2) mod 2047 = 701
  • s ← ((701 × 701) − 2) mod 2047 = 119
  • s ← ((119 × 119) − 2) mod 2047 = 1877
  • s ← ((1877 × 1877) − 2) mod 2047 = 240
  • s ← ((240 × 240) − 2) mod 2047 = 282
  • s ← ((282 × 282) − 2) mod 2047 = 1736

Since the final value of s is not 0, the conclusion is that M11 = 2047 is not prime. Although M11 = 2047 has nontrivial factors, the Lucas–Lehmer test gives no indication about what they might be.

Proof of correctness

The proof of correctness for this test presented here is simpler than the original proof given by Lehmer. Recall the definition

si={4if i=0;si122otherwise.

The goal is to show that Mp is prime iff sp20(modMp).

The sequence si is a recurrence relation with a closed-form solution. Let ω=2+3 and ω¯=23. It then follows by induction that si=ω2i+ω¯2i for all i:

s0=ω20+ω¯20=(2+3)+(23)=4

and

sn=sn122=(ω2n1+ω¯2n1)22=ω2n+ω¯2n+2(ωω¯)2n12=ω2n+ω¯2n.

The last step uses ωω¯=(2+3)(23)=1. This closed form is used in both the proof of sufficiency and the proof of necessity.

Sufficiency

The goal is to show that sp20(modMp) implies that Mp is prime. What follows is a straightforward proof exploiting elementary group theory given by J. W. Bruce[7] as related by Jason Wojciechowski.[8]

Suppose sp20(modMp). Then

ω2p2+ω¯2p2=kMp

for some integer k, so

ω2p2=kMpω¯2p2.

Multiplying by ω2p2 gives

(ω2p2)2=kMpω2p2(ωω¯)2p2.

Thus,

ω2p1=kMpω2p21.(1)

For a contradiction, suppose Mp is composite, and let q be the smallest prime factor of Mp. Mersenne numbers are odd, so q > 2. Let q be the integers modulo q, and let X={a+b3a,bq}. Multiplication in X is defined as

(a+3b)(c+3d)=[(ac+3bd)modq]+3[(ad+bc)modq].[note 1]

Clearly, this multiplication is closed, i.e. the product of numbers from X is itself in X. The size of X is denoted by |X|.

Since q > 2, it follows that ω and ω¯ are in X.[note 2] The subset of elements in X with inverses forms a group, which is denoted by X* and has size |X*|. One element in X that does not have an inverse is 0, so |X*||X|1=q21.

Now Mp0(modq) and ωX, so

kMpω2p2=0

in X. Then by equation (1),

ω2p1=1

in X, and squaring both sides gives

ω2p=1.

Thus ω lies in X* and has inverse ω2p1. Furthermore, the order of ω divides 2p. However ω2p11, so the order does not divide 2p1. Thus, the order is exactly 2p.

The order of an element is at most the order (size) of the group, so

2p|X*|q21<q2.

But q is the smallest prime factor of the composite Mp, so

q2Mp=2p1.

This yields the contradiction 2p<2p1. Therefore, Mp is prime.

Necessity

In the other direction, the goal is to show that the primality of Mp implies sp20(modMp). The following simplified proof is by Öystein J. Rödseth.[9]

Since 2p17(mod12) for odd p>1, it follows from properties of the Legendre symbol that (3|Mp)=1. This means that 3 is a quadratic nonresidue modulo Mp. By Euler's criterion, this is equivalent to

3Mp121(modMp).

In contrast, 2 is a quadratic residue modulo Mp since 2p1(modMp) and so 22p+1=(2p+12)2(modMp). This time, Euler's criterion gives

2Mp121(modMp).

Combining these two equivalence relations yields

24Mp12(2Mp12)3(3Mp12)(1)3(1)1(modMp).

Let σ=23, and define X as before as the ring X={a+b3a,bMp}. Then in the ring X, it follows that

(6+σ)Mp=6Mp+(2Mp)(3Mp)=6+2(3Mp12)3=6+2(1)3=6σ,

where the first equality uses the Binomial Theorem in a finite field, which is

(x+y)MpxMp+yMp(modMp),

and the second equality uses Fermat's little theorem, which is

aMpa(modMp)

for any integer a. The value of σ was chosen so that ω=(6+σ)224. Consequently, this can be used to compute ωMp+12 in the ring X as

ωMp+12=(6+σ)Mp+124Mp+12=(6+σ)(6+σ)Mp2424Mp12=(6+σ)(6σ)24=1.

All that remains is to multiply both sides of this equation by ω¯Mp+14 and use ωω¯=1, which gives

ωMp+12ω¯Mp+14=ω¯Mp+14ωMp+14+ω¯Mp+14=0ω2p1+14+ω¯2p1+14=0ω2p2+ω¯2p2=0sp2=0.

Since sp2 is 0 in X, it is also 0 modulo Mp.

Applications

The Lucas–Lehmer test was the main primality test used by the Great Internet Mersenne Prime Search (GIMPS) to locate large primes until 2021. This search has been successful in locating many of the largest primes known to date.[10] In 2021, GIMPS switched to an even faster variant of Fermat's primality test for locating probable Mersenne primes, and only uses Lucas–Lehmer to verify these probable primes as actual primes.

The test is considered valuable because it can provably test a large set of very large numbers for primality within an affordable amount of time. In contrast, the equivalently fast Pépin's test for any Fermat number can only be used on a much smaller set of very large numbers before reaching computational limits.

See also

Notes

  1. Formally, let q=/q and X=q[T]/T23.
  2. Formally, ω+T23 and ω¯+T23 are in X. By abuse of language ω and ω¯ are identified with their images in X under the natural ring homomorphism from [3] to X which sends 3 to T.

References

Template:Reflist

  • Script error: No such module "citation/CS1".

External links

Template:Number theoretic algorithms

  1. Script error: No such module "Citation/CS1".
  2. a b c d e Template:Cite thesis
  3. Script error: No such module "Citation/CS1".
  4. Script error: No such module "citation/CS1".
  5. Script error: No such module "citation/CS1".
  6. Script error: No such module "citation/CS1".
  7. Script error: No such module "Citation/CS1".
  8. Jason Wojciechowski. Mersenne Primes, An Introduction and Overview. 2003.
  9. Script error: No such module "Citation/CS1".
  10. The "Top Ten" Record Primes, The Prime Pages