On this page
A Mersenne number has the form 2^p − 1. Most are not prime, but the few that are include most of the largest primes ever found. The test that sorts them fits on one line, and you can run it by hand for small p. Here it is, with two worked examples and the reason it works on huge numbers.
What is the Lucas–Lehmer test?
Start with s₀ = 4 and build each next term by squaring and subtracting 2: s(k+1) = s(k)² − 2. The first terms are 4, 14, 194 and 37634, as listed in the Wikipedia article on the Lucas–Lehmer primality test.
The test: for an odd prime p, M = 2^p − 1 is prime if and only if s(p−2) is divisible by M (Wikipedia).
The test was developed by Édouard Lucas in 1878 and proved by Derrick Henry Lehmer in 1930 (Wikipedia).
Why must p be prime? If p = ab, then 2^a − 1 divides 2^p − 1: for example, 63 = 2^6 − 1 is divisible by 3 and 7. A prime p is necessary, but not enough.
How do you do the Lucas–Lehmer test by hand?
Reduce modulo M after every step, so the numbers stay small enough for paper.
Example 1: p = 5, M = 31. You need s(p−2) = s₃.
s₀ = 4
s₁ = 4² − 2 = 14
s₂ = 14² − 2 = 194, and 194 − 6 × 31 = 8, so s₂ ≡ 8
s₃ = 8² − 2 = 62 = 2 × 31, so s₃ ≡ 0
The remainder is 0, so 31 is prime.
Example 2: p = 11, M = 2047. Now you need s₉. Keep each remainder between 0 and 2046.
s₀ = 4, s₁ = 14, s₂ = 194
s₃ = 194² − 2 = 37634 = 18 × 2047 + 788, so s₃ ≡ 788
s₄ = 788² − 2 = 620942, which leaves 701
s₅ = 701² − 2 = 491399, which leaves 119
s₆ = 119² − 2 = 14159, which leaves 1877
s₇: 1877 ≡ −170, and 170² − 2 = 28898, which leaves 240
s₈ = 240² − 2 = 57598, which leaves 282
s₉ = 282² − 2 = 79522 = 38 × 2047 + 1736, so s₉ ≡ 1736
The remainder is 1736, not 0, so 2047 is composite even though 11 is prime: 23 × 89 = 2047. The test proved that without finding either factor.
Why is the Lucas–Lehmer test so fast?
Testing 2^p − 1 takes exactly p − 2 squarings, each followed by one reduction modulo M (Wikipedia). Trial division would try primes up to √M, and for a number with millions of digits that square root itself has millions of digits.
With Schönhage–Strassen multiplication for the squarings, the whole test runs in roughly p² log p log log p steps (Wikipedia). That grows like a power of the number of digits, not like the size of the number.
Reducing modulo 2^p − 1 is also cheap in binary: since 2^p ≡ 1, you cut the number into blocks of p bits and add them, with no division at all.
Why are the largest known primes Mersenne primes?
The largest known prime is 2^136,279,841 − 1, with 41,024,320 digits, discovered on 12 October 2024 by GIMPS and Luke Durant, according to the GIMPS list of known Mersenne primes. It is the 52nd known Mersenne prime, and GIMPS marks that ranking as provisional.
The Prime Pages give the reason: the largest numbers are proved prime using the factorisation of N + 1 or N − 1, and for a Mersenne number N + 1 is simply a power of two (The Largest Known Primes). On that page's list of the ten largest known primes, seven are Mersenne primes; the record is almost always a Mersenne, not always.
In 2021, GIMPS switched to an easy-to-check variant of Fermat's test for finding probable Mersenne primes and now uses Lucas–Lehmer to confirm them (Wikipedia). To see why a Fermat-style check alone can be fooled, read what Carmichael numbers are and the 561 trap in Fermat's test, then compare it with how the Miller–Rabin primality test works, worked by hand.
How should you practise it?
Run p = 3 (M = 7) and p = 7 (M = 127) yourself; both should end at 0.
Run p = 11 again without looking, and check that you reach 1736.
Explain in one sentence why p = 4 or p = 9 needs no test at all.
Read Lagrange's theorem on group order; the proof of Lucas–Lehmer uses the same idea of the order of an element.
If modular arithmetic or orders still feel shaky, the Fermi Concept Library covers every concept in mathematics, so you can fill the gap before the next step. For more practice, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



