How the Lucas-Lehmer test checks a Mersenne number

Short answer: The Lucas-Lehmer test decides whether a Mersenne number M = 2^p − 1 is prime, for an odd prime p. It sets s = 4 and repeats s = s² − 2, modulo M, p−2 times. M is prime exactly when the final s is 0 modulo M. The test is deterministic. Each step is a squaring of a p-bit number, so the cost is O(p) squarings, which is practical for the exponents used in the record Mersenne searches and irrelevant to a general integer.


What the problem actually asks

A Mersenne number has a rigid shape, one less than a power of two. Most primality tests ignore that shape. Lucas-Lehmer uses it. The sequence s_{i+1} = s_i² − 2 is the trace of a recurring squaring in the ring Z[√3], and the theorem says the (p−2)-th term is divisible by M exactly when M is prime, provided p itself is prime.

If p is composite, 2^p − 1 is also composite, with a factorisation that follows the factors of p. The test is never asked that question. The caller checks that p is prime first, which for the size of p in a Mersenne search is a much smaller problem.


What the algorithm guarantees

For odd prime p, the test returns a correct prime-or-composite answer for 2^p − 1. The number of loop iterations is exactly p−2. Implementing one fewer or one more iteration tests a different statement and is wrong. Modular reduction modulo 2^p − 1 is a shift-and-add, because 2^p ≡ 1, which is why these numbers are friendlier than a random integer of the same size.

The test says nothing about a general odd integer. A number that is merely close to a power of two does not inherit the theorem. Miller-Rabin is the general test. Lucas-Lehmer is the Mersenne test.


The working steps

  1. Confirm that p is an odd prime. If it is not, do not run the test. If p is composite, 2^p − 1 is composite for a different reason.
  2. Set M = 2^p − 1 and s = 4.
  3. Repeat p−2 times: replace s by (s² − 2) mod M. The modulo uses the shift reduction for this particular M.
  4. If the final s is 0, M is prime. Otherwise M is composite.
  5. Do not stop early because s became 0 in the middle of the loop. The theorem looks at the final term.

The theorem is for Mersenne numbers with prime exponents. It is not a general-purpose Lucas test, of which there are several with different parameters, and it is not a factorisation. A composite result does not name a factor. The Great Internet Mersenne Prime Search uses this test, with an irrational-base check of the last digits, on exponents that have already passed trial factorisation.


Approaches that look simpler and fail

Approach Why it fails when the loop count is p instead of p minus 2
Iterate p times from s = 4 The theorem’s index is p−2. An off-by-one accepts composites or rejects primes.
Run the test for a composite exponent The hypothesis is an odd prime exponent. A composite exponent makes 2^p − 1 composite, and the sequence test is not the argument.
Use the same recurrence for a general integer n The modular ring and the theorem are about 2^p − 1. A general n needs Miller-Rabin or another general test.
Treat a final zero as a factorisation Zero means M divides the final term, which the theorem converts into primality. It is not a factor.

General primality is Miller-Rabin. A deterministic polynomial-time test for arbitrary integers is AKS. Lucas-Lehmer is the Mersenne primality test.


A working example

This Python fragment starts at s = 4 and iterates s = s² − 2 modulo 2^p − 1, exactly p−2 times. For p = 5 the Mersenne number is 31 and the final residue is 0, so 31 is prime.

def lucas_lehmer(p):
    mersenne = (1 << p) - 1
    s = 4
    for _ in range(p - 2):
        s = (s * s - 2) % mersenne
    return s == 0

assert lucas_lehmer(5) and lucas_lehmer(7) and not lucas_lehmer(11)
print(0 if lucas_lehmer(5) else 1)

Where it is used

The Great Internet Mersenne Prime Search uses the Lucas-Lehmer test on Mersenne numbers 2^p − 1. The test is a squaring loop, not a general primality test. A general integer goes to Miller-Rabin.


When you can drop this algorithm

Drop Lucas-Lehmer for any integer that is not one less than a power of two with prime exponent. Keep it for exactly those numbers, and iterate p−2 times.


Why this stays published

We publish this so a search for the Lucas-Lehmer test returns the recurrence s² − 2 and the p−2 iterations that decide a Mersenne prime. The Lucas-Lehmer test proves a Mersenne number 2^p − 1 prime or composite by iterating s = s² − 2 modulo that number, exactly p−2 times, and checking for zero.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *