Mersenne Number Lucas-Lehmer Test

Is 2^p - 1 prime? The Lucas-Lehmer test, step by step.

  • Exponents up to 11213
  • Trace for small p
  • Perfect number shown
A row of five stacks of soft brown clay discs on a small round wooden stool in daylight, the stacks of similar height with the last one shorter.

The guide

A prime exponent is not enough: how squaring settles whether 2^p - 1 is prime

A Mersenne number can only be prime when its exponent is, yet most prime exponents fail. One short recurrence, repeated p - 2 times on exact integers, decides each case.

  • 2047 = 23 x 89 the smallest Mersenne number with a prime exponent that is composite
  • p - 2 steps of squaring and subtracting 2, starting from 4
  • 23 exponents give a Mersenne prime from 2 up to 11,213
  • 3,376 digits in M(11213), the largest Mersenne prime this page can test
  • 1876 the year Lucas proved by hand that M(127) is prime
Read the full guide Fold the guide 7 min · 14 sources

Take 2 to the power 11 and subtract 1. You get 2047. Eleven is prime, and a quick guess says 2047 should be prime too. It is not: 2047 = 23 x 89. The Wikipedia article on Mersenne primes names it as the smallest counterexample to the idea that a prime exponent always gives a prime.

That small failure is the whole subject. Numbers of the form 2 to the p, minus 1, are called Mersenne numbers. Some are prime, many are not, and a short test tells them apart without ever looking for a factor.

What a Mersenne number is, and what the exponent must be

The article defines a Mersenne prime as a prime number that is one less than a power of two, written M(n) = 2^n - 1. They are named after Marin Mersenne, a French Minim friar who studied them in the early 17th century. The list of exponents that work begins 2, 3, 5, 7, 13, 17, 19 and 31, and the matching primes begin 3, 7, 31, 127, 8191, 131071, 524287 and 2147483647.

The exponent has to be prime, and the reason is an identity. For any whole numbers a and b, the article shows that 2^(ab) - 1 splits as (2^a - 1) times (1 + 2^a + 2^(2a) + and so on up to 2^((b-1)a)). So an exponent of 4 gives 15 = 3 x 5, a composite exponent always gives a composite number, and only prime exponents are worth testing.

The converse fails. The first four prime exponents, 2, 3, 5 and 7, all give primes. The fifth, 11, does not. Successes then thin out fast: up to 11,213 there are only 23 exponents that give a prime, while the primes up to 11,213 number 1,357. The page you are using tests each one exactly.

Mersenne himself got this wrong. MacTutor reports that in 1644 he claimed M(p) is prime for p = 2, 3, 5, 7, 13, 17, 19, 31, 67, 127 and 257 and composite for the other 44 primes below 257. Over the years that list was found to be wrong in five places: he included 67 and 257, which are composite, and left out 61, 89 and 107, which are prime.

The test that needs only squaring

The Lucas-Lehmer test is short enough to hold in your head. For an odd prime p, start with s = 4. Replace s by s^2 - 2, keeping only the remainder modulo M(p), and do this p - 2 times. The Wikipedia article states that M(p) is prime exactly when the last value is 0.

Here is the smallest case. For p = 3, M(3) = 7 and there is a single step: 4 x 4 - 2 = 14, and 14 mod 7 = 0. So 7 is prime. For p = 7 the page lists 4, 14, 67, 42, 111 and finally 0, so 127 is prime. For p = 11 there are nine steps, and the article gives the values 14, 194, 788, 701, 119, 1877, 240, 282 and 1736. The last is not 0, so 2047 is composite.

You can reproduce the 11 run in the tool. The step from 14 is 14 x 14 - 2 = 194. The step from 194 is 194 x 194 - 2 = 37,634, and 37,634 mod 2047 is 788. Nothing else is needed, and nothing is guessed. The article notes one honest limit: although 2047 has the factors 23 and 89, the test gives no hint what they are.

Two details are easy to get wrong, and a chatbot often does. The start value is 4, not 2: with 2, the step 2 x 2 - 2 returns 2 again, so the sequence never moves and every exponent would look composite. The number of steps is p - 2, not p - 1: after p - 2 steps a prime has reached 0, and one more step gives M(p) - 2, which is not 0. The self-check on the page runs both wrong rules on the first seven exponents above 2 and shows every one of them rejected.

Other start values do exist. The article lists 10 and 52, and the value 2/3 taken modulo M(p), and notes that Lucas used 3 for exponents that are 3 mod 4 when he proved M(127) prime. The page sticks to 4 because it works for every odd exponent.

Why it is fast

The remainder looks expensive but is not. The article explains that dividing by 2^n - 1 can be replaced by adding: take the low n bits of the number, add the bits above them, and repeat. Its own example reduces 916 modulo 2^5 - 1 = 31 in a few additions and gets 17. In practice each step is one squaring and a few additions, with no division at all.

That leaves the squaring. Schoolbook multiplication takes work that grows as p squared for a p-bit number, and since the loop runs about p times, the total grows as p cubed. The article says faster multiplication based on the Fourier transform brings the whole test down to roughly p squared times a small logarithmic factor. The article names Prime95, the program used by the Great Internet Mersenne Prime Search, as one of the most efficient practical implementations of that kind of multiplication.

On this page the arithmetic is exact. The MDN page on BigInt says its values represent integers too high or too low for the ordinary number type, and its examples use 9007199254740991, the previous largest safe integer. That value happens to be 2^53 - 1, and OEIS entry A001348, which lists 2^p - 1 for prime p, includes it. It is a Mersenne number with a prime exponent, and it is composite: 6361 x 69431 x 20394401. For p = 11,213, the largest Mersenne exponent the page can reach, M(p) has 3,376 digits and the test takes 11,211 steps, which the browser finishes in a fraction of a second.

The test runs inside a web worker. MDN describes a worker as a script that runs in a background thread separate from the main thread, so the page stays responsive and the Cancel button works.

Looking for factors, and what comes of a prime

When the test says composite, you may still want a factor. The Mersenne prime article proves that any prime q dividing M(p) satisfies q = 1 modulo 2p. That means the only candidates are 2p + 1, 4p + 1, 6p + 1 and so on, which is a short list to try. The page also keeps only candidates that are 1 or 7 modulo 8. That filter is derived, not quoted: since 2^p = 1 modulo q, the number 2 is a square modulo q, which forces q to be 1 or 7 modulo 8. An independent computation on the factors of ten Mersenne numbers agrees, and the page checks every result by dividing.

For p = 11 the first candidate that works is 23 = 2 x 1 x 11 + 1. For p = 23 it is 47, for p = 29 it is 233 and for p = 37 it is 223. Each of those is the smallest prime factor, and the article gives 23 divides M(11) and 47 divides M(23) as examples of a prime p with 2p + 1 also prime.

The search has an edge. M(67) is composite, and Lucas showed so in 1876 without finding a factor. No factor turned up until Frank Nelson Cole’s talk in 1903, in which, according to the article, he multiplied 193707721 by 761838257287 on a blackboard and got 2^67 - 1. That smaller factor is 2 x 1,445,580 x 67 + 1, far past the 200,000 steps this page tries, so for p = 67 the page says plainly that it found no factor and that this does not make M(67) prime.

When the test says prime, a second result follows. The article on perfect numbers records that Euclid showed around 300 BC that when 2^p - 1 is prime, 2^(p-1) times 2^p - 1 is perfect, meaning it equals the sum of its proper divisors. The Euclid-Euler article adds that Euler proved the converse for even numbers, so each Mersenne prime gives exactly one even perfect number and each even perfect number comes from one.

For p = 2 that number is 6, for p = 3 it is 28, and for p = 31 it is 2^30 x 2147483647 = 2305843008139952128. OEIS entry A000396 lists it as the eighth. It is still unknown whether any odd perfect number exists.

What the page can and cannot say

The Wikipedia article on Mersenne primes says that 52 Mersenne primes are known and that the largest, 2^136279841 - 1, was found on October 12, 2024. Since 1997, according to the article, every new one has come from the Great Internet Mersenne Prime Search, a volunteer project founded in 1996 by George Woltman, who also wrote its Prime95 program. Treat those counts as dated: the OEIS entry for the exponents, A000043, says that 48 terms were confirmed in order as of January 31, 2025, and sends readers to the project for the latest.

This page stops at p = 12,000. Within that range it lists every Mersenne exponent, 23 of them, from 2 to 11,213, along with the digit count of each prime and the perfect number that goes with it. The typed list is confirmed by the page itself: it can run the test on every prime exponent up to a limit you choose and compare the results with the list.

It does not hunt for record primes, which needs huge hardware and weeks of computing for each exponent. It does not factor a large composite M(p) completely, and it does not use other primality tests. It does one thing: it runs the Lucas-Lehmer test on exact integers and reports what comes out.

In 1876 Edouard Lucas proved M(127) prime by hand, after testing it for 19 years, and the Wikipedia article on him adds that it stayed the largest known prime for three quarters of a century. A modern browser runs the same test in a fraction of a second.

Sources

Every source consulted for this guide. Links checked November 10, 2026.

  1. Lucas-Lehmer primality test - Wikipedia Wikipedia (CC BY-SA 4.0)
  2. Mersenne prime - Wikipedia Wikipedia (CC BY-SA 4.0)
  3. Perfect number - Wikipedia Wikipedia (CC BY-SA 4.0)
  4. Euclid-Euler theorem - Wikipedia Wikipedia (CC BY-SA 4.0)
  5. Great Internet Mersenne Prime Search - Wikipedia Wikipedia (CC BY-SA 4.0)
  6. Marin Mersenne - Wikipedia Wikipedia (CC BY-SA 4.0)
  7. Edouard Lucas - Wikipedia Wikipedia (CC BY-SA 4.0)
  8. Marin Mersenne (1588 - 1648) - Biography - MacTutor History of Mathematics MacTutor History of Mathematics, University of St Andrews (CC BY-SA 4.0)
  9. A000043 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
  10. A000668 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
  11. A000396 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
  12. A001348 - OEIS The On-Line Encyclopedia of Integer Sequences (CC BY-SA 4.0)
  13. BigInt - JavaScript | MDN MDN Web Docs (CC BY-SA 2.5 or later)
  14. Web Workers API - Web APIs | MDN MDN Web Docs (CC BY-SA 2.5 or later)