Mersenne Prime Calculator

Input

Mersenne number 2^p - 18,191
StatusPrime
Digits in M4
Lucas-Lehmer iterations11
How this was calculated
  1. The Mersenne number for p = 13 is M = 2^13 - 1 = 8,191, a number with 4 digits.
  2. Run Lucas-Lehmer: start s = 4 and repeat s = (s^2 - 2) mod M, 11 times. The final residue is 0, which proves M prime.
  3. Verdict: M is prime because the Lucas-Lehmer sequence ends at 0 after 11 iterations.
  4. A Mersenne number can only be prime when p itself is prime, which is why prime hunters test prime exponents - every Mersenne prime also yields an even perfect number, 2^(p-1) x (2^p - 1).

A Mersenne prime calculator tells you whether a number of the form M = 2^p - 1 is prime. Enter an exponent p and the tool reports the resulting Mersenne number and whether it is prime or composite. Because 2^a - 1 divides 2^p - 1 whenever a divides p, M can only be prime when p itself is prime, so the calculator first checks that p is prime and then tests the candidate. For example, 2^13 - 1 = 8,191 is prime, while 2^11 - 1 = 2,047 = 23 x 89 is composite even though 11 is prime. The tool also handles larger cases such as 2^31 - 1 = 2,147,483,647, the prime Euler famously proved by hand.

The method behind the answer is the Lucas-Lehmer test, the accepted standard for settling primality of Mersenne numbers with prime exponents. It starts with s = 4, repeats s = (s^2 - 2) mod M exactly p - 2 times, and M is prime if and only if the final residue is 0. That fixed, finite procedure is why the result is trusted rather than guessed. This page shows the formula, the step-by-step workings, worked examples, and the sources behind the method. It also notes the Euclid-Euler link: every Mersenne prime yields an even perfect number, 2^(p-1) x (2^p - 1).

Worked examples

p = 13

(13)

Mersenne number 2^p - 18,191
StatusPrime
Digits in M4
Lucas-Lehmer iterations11

p = 11

(11)

Mersenne number 2^p - 12,047
StatusComposite
Digits in M4
Lucas-Lehmer iterations9

p = 31

(31)

Mersenne number 2^p - 12,147,483,647
StatusPrime
Digits in M10
Lucas-Lehmer iterations29

Why the exponent must be prime

Before a mersenne prime calculator runs any heavy primality test, it performs a much cheaper check: is the exponent p itself prime? The reason is a divisibility rule. A Mersenne number has the form M = 2^p - 1, and it can only be prime when p itself is prime, because 2^a - 1 divides 2^p - 1 whenever a divides p. In other words, a composite exponent guarantees a composite Mersenne number. No amount of testing will turn it prime, so the exponent check comes first and saves the work that follows.

A concrete example makes the shortcut clear. Take p = 15. Because 3 divides 15, the number 2^3 - 1 (which is 7) must divide 2^15 - 1. Because 5 also divides 15, the number 2^5 - 1 (which is 31) must divide 2^15 - 1 as well. So 2^15 - 1 is divisible by both 7 and 31, and it cannot be prime. The same logic applies to every composite exponent: find any factor a of p, and 2^a - 1 becomes a factor of the Mersenne number. That single step rules out the candidate instantly.

Passing the exponent check is necessary but not sufficient. A prime exponent does not guarantee a prime Mersenne number. The classic counterexample is 2^11 - 1 = 2,047, which equals 23 x 89 and is composite even though 11 is prime. By contrast, 2^13 - 1 = 8,191 is prime. So once the exponent is confirmed prime, a genuine primality test is still required, and for Mersenne numbers the standard tool is the Lucas-Lehmer test.

The Lucas-Lehmer test settles primality for prime exponents with a simple repeated squaring. Start with s = 4, then repeat s = (s^2 - 2) mod M exactly p - 2 times. The number M is prime if and only if the final residue is 0. This is why the exponent-first design matters: the test is only defined for prime exponents, and running it on a composite exponent would waste effort on a number already known to be composite. The structure of the calculator mirrors the mathematics — filter the exponent, then test the Mersenne number.

The stakes are high because every Mersenne prime yields an even perfect number through the formula 2^(p-1) x (2^p - 1), the Euclid-Euler link between the two families. A famous case is 2^31 - 1 = 2,147,483,647, proved prime by Euler working by hand. That result stands as a reminder that the exponent check is the first and cheapest filter in a long chain of reasoning.

Quick rule: if p is composite, 2^p - 1 is composite too — because 2^a - 1 divides 2^p - 1 whenever a divides p. Check the exponent before you test the number.

The Lucas-Lehmer test

The Lucas-Lehmer test is the standard way a mersenne prime calculator decides whether a number of the form M = 2^p - 1 is prime. It applies only when the exponent p is itself prime, which is the first filter: 2^a - 1 divides 2^p - 1 whenever a divides p, so a composite exponent can never give a prime. Once p passes that check, the test gives a definite yes-or-no answer. Start with s = 4, then repeat the step s = (s^2 - 2) mod M exactly p - 2 times. M is prime if and only if the final residue is 0.

The p = 13 example shows the whole procedure. Here M = 2^13 - 1 = 8,191, and because p - 2 = 11, the iteration runs eleven times. Each step squares the current value, subtracts 2, and reduces the result modulo 8,191. After the eleventh step the residue is 0, so the test declares 8,191 prime — matching the known result that 2^13 - 1 is prime.

The same test convicts 2,047. For p = 11, M = 2^11 - 1 = 2,047, and p - 2 = 9, so nine iterations are required. The final residue here is not 0, and the verdict is composite. This is not a borderline case: 2,047 = 23 x 89. It is the cleanest illustration of why the exponent check alone is not enough — 11 is prime, yet 2^11 - 1 is not, and only the full Lucas-Lehmer iteration settles it.

The test scales to large exponents in principle, though the arithmetic grows with M. A famous early result is 2^31 - 1 = 2,147,483,647, which is prime; Euler proved it working by hand. Every Mersenne prime also yields an even perfect number through the Euclid-Euler link: 2^(p-1) x (2^p - 1). So a zero residue at the end of the Lucas-Lehmer test certifies two things at once — a Mersenne prime and its paired perfect number.

  • Exponent filter: M = 2^p - 1 can be prime only when p is prime, since 2^a - 1 divides 2^p - 1 whenever a divides p.
  • Iteration: start s = 4 and repeat s = (s^2 - 2) mod M exactly p - 2 times.
  • Verdict: M is prime if and only if the final residue is 0.
  • p = 13: M = 8,191, p - 2 = 11 iterations, final residue 0, so 8,191 is prime.
  • p = 11: M = 2,047, p - 2 = 9 iterations, final residue not 0, so 2,047 is composite (23 x 89).
Exponent pM = 2^p - 1Iterations (p - 2)Final residueVerdict
138,191110Prime
112,0479Not 0Composite (23 x 89)
312,147,483,64729Not givenPrime (proved by Euler by hand)
A zero final residue is the whole proof: on p = 13 the eleven iterations end at 0 and 8,191 is prime; on p = 11 the nine iterations do not, and 2,047 = 23 x 89 is composite.

Famous cases

The most useful lesson in Mersenne history is that a prime exponent is not enough. Take p = 11. Eleven is prime, so the basic rule allows 2^11 - 1 to be a candidate. But 2^11 - 1 = 2,047 = 23 x 89, which is composite. Anyone using a mersenne prime calculator will see this pattern again and again: the condition that p be prime is necessary, not sufficient. The reason is the factoring identity in the brief — 2^a - 1 divides 2^p - 1 whenever a divides p — so composite exponents are ruled out immediately, but prime exponents still have to be tested.

The contrast with p = 13 makes the point concrete. Here 2^13 - 1 = 8,191, and it is prime. Two adjacent primes, 11 and 13, give opposite answers, which is why no shortcut replaces an actual test. The Lucas-Lehmer test settles primality for prime exponents: start with s = 4, repeat s = (s^2 - 2) mod M exactly p - 2 times, and M is prime if and only if the final residue is 0. For p = 11 that procedure ends on a nonzero residue; for p = 13 it ends on 0.

The historical showpiece is 2^31 - 1 = 2,147,483,647, which is prime. Euler proved it by hand, with no computer and no electronic aid, working through the arithmetic himself. That number is still worth pausing over: it is the largest Mersenne prime found by purely manual effort, and it sits at the edge of what a person can verify with patience and careful calculation. Modern searches operate at a different scale entirely.

That scale is where GIMPS comes in. The Great Internet Mersenne Prime Search is a distributed effort: volunteers run the Lucas-Lehmer test on their own machines, and the work is coordinated so that candidate exponents are checked across the network rather than by one person. Record Mersenne primes are now discovered this way, and the search continues because each new exponent must be tested individually. There is no formula that hands over the next prime.

One more connection is worth keeping in mind, because it explains why these numbers attract so much attention. Every Mersenne prime yields an even perfect number, 2^(p-1) x (2^p - 1) — the Euclid-Euler link between the two families. So when the search finds a new Mersenne prime, it also produces a new perfect number, and the two discoveries arrive together.

  • p = 11: 2^11 - 1 = 2,047 = 23 x 89 — composite even though 11 is prime.
  • p = 13: 2^13 - 1 = 8,191 — prime.
  • p = 31: 2^31 - 1 = 2,147,483,647 — prime, proved by Euler by hand.
  • Every Mersenne prime gives an even perfect number: 2^(p-1) x (2^p - 1).
A prime exponent is a filter, not a guarantee: 11 is prime, but 2^11 - 1 = 2,047 = 23 x 89 is not.

Mersenne primes and perfect numbers

A perfect number is one that equals the sum of its proper divisors — the numbers that divide it evenly, not counting the number itself. The smallest example is 6, since 1 + 2 + 3 = 6. The next is 28, since 1 + 2 + 4 + 7 + 14 = 28. These two facts sit at the center of a remarkable bridge between two famous number families, and that bridge is the reason a Mersenne prime calculator is useful for more than just checking primality.

The bridge is the Euclid-Euler theorem. It says that every Mersenne prime yields an even perfect number, computed as 2^(p-1) x (2^p - 1), where 2^p - 1 is the Mersenne prime itself. Take p = 2: the Mersenne number is 2^2 - 1 = 3, which is prime, and the formula gives 2^(2-1) x 3 = 2 x 3 = 6, the first perfect number. Take p = 3: 2^3 - 1 = 7 is prime, and 2^(3-1) x 7 = 4 x 7 = 28, the second. The pattern continues: each Mersenne prime you find hands you an even perfect number for free. The theorem works in both directions for even perfect numbers — every even perfect number arises this way from a Mersenne prime — so the two families rise and fall together.

Finding the Mersenne primes behind these numbers takes real work, which is where the Lucas-Lehmer test comes in. For a prime exponent p, you start with s = 4, then repeat s = (s^2 - 2) mod M exactly p - 2 times, where M = 2^p - 1. M is prime if and only if the final residue is 0. The test only applies when p is itself prime, and that restriction is not arbitrary: 2^a - 1 divides 2^p - 1 whenever a divides p, so a composite exponent can never give a Mersenne prime. Even a prime exponent is no guarantee. The case p = 11 gives 2^11 - 1 = 2,047 = 23 x 89, composite despite 11 being prime. The case p = 13 gives 2^13 - 1 = 8,191, which is prime. And 2^31 - 1 = 2,147,483,647 is prime — a result Euler famously proved by hand, long before any machine could check it.

That last example shows why the connection matters in practice: 2^31 - 1 being prime means 2^30 x (2^31 - 1) is an even perfect number, a number with over half a billion digits' worth of divisor structure behind it. Every new Mersenne prime discovered adds exactly one new even perfect number to the known list.

What about odd perfect numbers? None has ever been found. Every perfect number anyone has ever identified is even, and every one of those traces back to a Mersenne prime through the Euclid-Euler formula. The search for an odd perfect number has run for centuries without success, and no proof exists that one cannot exist — it remains one of the oldest open questions in number theory.

Euclid-Euler in one line: if 2^p - 1 is prime, then 2^(p-1) x (2^p - 1) is an even perfect number — and every even perfect number comes from exactly one Mersenne prime.

FAQs

How can I test if 2^p - 1 is prime?

First check the exponent. If p is composite, 2^p - 1 is not prime. If p is prime, run the Lucas-Lehmer test: start at s = 4 and do p - 2 iterations of s = (s^2 - 2) mod M, where M = 2^p - 1. If the final value is 0, M is prime.

Is 2^11 - 1 prime?

No. 2^11 - 1 equals 2,047, which factors as 23 x 89. This is the classic warning that a prime exponent is necessary but not sufficient: 11 is prime, yet the result is composite. So a prime p does not guarantee a Mersenne prime.

What is the largest exponent this calculator handles?

The largest exponent is 61. The calculator uses big-integer arithmetic, so 2^61 - 1 is printed exactly, all 19 digits of it. No rounding or scientific notation is involved, so you see the full number as it truly is.

Why do people hunt Mersenne primes?

The Lucas-Lehmer test makes Mersenne numbers the easiest huge numbers to prove prime. Because of that, the largest known primes are almost always Mersenne primes, and they are found by the GIMPS project, a distributed search run by volunteers.

What is a perfect number's connection?

Each Mersenne prime 2^p - 1 gives the even perfect number 2^(p-1)(2^p - 1). Every even perfect number arises this way. The pairing has been known since Euclid and Euler, linking Mersenne primes directly to perfect numbers in a one-to-one correspondence.

How many digits does 2^61 - 1 have?

2^61 - 1 has 19 digits. That is small beside the tens of millions of digits in record GIMPS primes, but the exact arithmetic here is the same idea scaled down. The calculator prints all 19 digits exactly.

Sources

Related calculators

Prime Number Calculator
Primality by trial division, the full prime factorization of composites, and the neighbouring primes on either side.
Perfect Number Calculator
Classify a number as perfect, abundant or deficient by comparing it with the sum of its proper divisors (its aliquot sum).
Lucas Number Calculator
The nth Lucas number by its recurrence, with the companion Fibonacci number, the identity check L(n) = F(n-1) + F(n+1) and the ratio that converges to the square root of five.