Mathematics · Algorithms · October 26, 2026

Mersenne Primes, Lucas–Lehmer & Euclid–Euler Perfect Numbers

For over two millennia, mathematicians have been fascinated by prime numbers of the special form:

Mp = 2p − 1

Named after the 17th-century French Minim friar Marin Mersenne, who published a famous (though imperfect) list of them in his 1644 Cogitata Physico-Mathematica, these numbers hold nearly every historical world record for the largest known prime. But their significance extends far deeper into number theory: they are intimately connected to ancient geometry, perfect numbers, and one of the fastest deterministic primality tests ever conceived.

Why Must the Exponent Be Prime?

A simple algebraic identity reveals why we only ever test prime exponents p. If an integer exponent n is composite, say n = a × b with a, b > 1, then 2n − 1 factors algebraically:

2ab − 1 = (2a − 1)(2a(b−1) + 2a(b−2) + … + 2a + 1)

For example, 24 − 1 = 15 = 3 × 5, and 26 − 1 = 63 = 7 × 9. Therefore, p being prime is a strict prerequisite for 2p − 1 to be prime.

However, p being prime is not sufficient. In 1536, Hudalrichus Regius proved that p = 11 fails:

M11 = 211 − 1 = 2047 = 23 × 89

As p grows, Mersenne numbers become composite far more often than they are prime.

The Euclid–Euler Theorem: Perfect Numbers

Around 300 BCE, in Book IX, Proposition 36 of Elements, Euclid proved an extraordinary relationship: if the Mersenne number 2p − 1 is prime, then:

N = 2p−1(2p − 1)

is a perfect number—an integer equal to the exact sum of its proper positive divisors (σ(N) = 2N).

The ancient Greeks knew the first four perfect numbers:

Two millennia later, in the 18th century, Leonhard Euler proved the converse: every even perfect number must be of Euclid's form. This established a 1-to-1 correspondence between Mersenne primes and even perfect numbers. (Whether any odd perfect number exists remains one of the oldest unsolved problems in all of mathematics).

The Lucas–Lehmer Deterministic Test

General-purpose primality tests for arbitrary numbers (such as Miller–Rabin or AKS) require significant computational overhead. For Mersenne numbers, however, French mathematician Édouard Lucas discovered in 1876 a remarkably elegant recurrence, later simplified and proven by Derrick Henry Lehmer in 1930:

Lucas–Lehmer Sequence:
s0 = 4
si = (si−1² − 2) mod Mp

The Theorem: For any odd prime p, the Mersenne number Mp = 2p − 1 is prime if and only if:

sp−2 ≡ 0   (mod Mp)

Consider p = 5 (M5 = 31):

Since s5−2 = s3 = 0, M5 = 31 is proven prime in just 3 squarings!

In 1876, Édouard Lucas computed this test entirely by hand for p = 127:

M127 = 170,141,183,460,469,231,731,687,303,715,884,105,727

This 39-digit monster stood as the largest prime ever discovered by manual calculation for all of human history.

The Secret to Speed: Division-Free Modular Reduction

Why is the Lucas–Lehmer test so fast on computers?

In standard modular arithmetic, calculating X mod M requires multi-precision division, which is costly (O(n²) bit complexity). But when the modulus is a Mersenne number Mp = 2p − 1, binary arithmetic behaves like clockwork:

2p ≡ 1   (mod 2p − 1)

Because 2p is congruent to 1, any number X written in base 2p:

X = A × 2p + B   ⇒   X ≡ A + B   (mod 2p − 1)

Here, B is simply the lowest p bits of X, and A is X bit-shifted right by p bits! In code:

// Division-free Mersenne modular reduction:
let rem = (sq >> p) + (sq & ((1 << p) - 1));
if rem >= mp { rem -= mp; }

No integer division is ever required! A single squaring, a bitwise shift, a bitwise mask, and a conditional addition suffice for each iteration. In our pure Rust backend (crypto-lab), this allows our engine to verify Lucas's 39-digit prime M127 in just 77 microseconds, and Robinson's 157-digit M521 in 1.2 milliseconds.

Interactive Lab Tools

You can test any prime exponent up to p = 5000 and view the step-by-step recurrence trace or enumerate the sequence of known Mersenne primes in our live web tools:

𝔐 Explore Mersenne Primes in The Lab ❯

Further Reading & Code