Playing with Mersenne Numbers

The largest prime number we’re currently aware of has 22,338,618 digits. It was discovered in January 2016 thanks to the coordinated effort of thousands of machines across the globe.

It is:

As many readers will know, this is a Mersenne prime number. The overwhelming majority of the largest prime numbers we’ve discovered over the past century have been Mersenne numbers.

A brief introduction to Mersenne Numbers

Mersenne numbers are defined as:

for n in .

Not all Mersenne numbers are prime (e.g., ), and not all prime numbers are Mersenne numbers (e.g., ).

Nevertheless, Mersenne numbers are currently our best bet when it comes to finding increasingly larger prime numbers.

A key reason for this is that we have a computationally efficient deterministic primality testing algorithm. Namely, the Lucas–Lehmer test (LLT).

This algorithm is significantly faster than primality testing algorithms we have for general integers. So we can try increasingly larger values of n and determine, in reasonable amounts of time (given enough computational power), whether the corresponding Mersenne number is prime or not.

It turns out that n needs to be prime as well, in order for to be prime, which further reduces the pool of candidates we need to try to find a new Mersenne prime.

Mersenne numbers, by the way, take their name from Marin Mersenne, who astutely noticed that many prime numbers took the form (e.g., for n = 2, 3, 5, 7, 13, 17, 19, 31…).

Not being able to take advantage of a modern calculator (he published his findings in 1644), he didn’t get his list of exponents quite right, which in turn made his conjecture about prime numbers of the form for false.

He was wrong, but his mathematical curiosity paid off, leading us to two modern conjectures and today’s search for large primes.

Let’s be mathematically curious

I’m a big believer in mathematical curiosity. Asking, “what if” questions. Experimenting. This is why I’m so fond of experimental mathematics – despite its limitations and challenges.

As a kid, I used to spend hours trying to come up with proofs of Fermat’s Last Theorem and other unsolved problems. Obviously, those were seriously hard problems. I wasn’t going to prove anything.

But in the process of trying, I learned an incredible deal, developed a lifelong passion for mathematics, and even had the joy of discovering some (it turns out well known) simpler results on my own.

Plus one rather than minus one

As I came up with the idea of writing a post on Mersenne numbers, a “what if” came to my mind. What happens if we take numbers of the form:

for n in ? What changes? Let’s find out.

Much like Mersenne did three century ago for his numbers, we can trivially see that some numbers are indeed prime. For example, without breaking out the calculator, we can instantly see that this holds for n = 0, 1, 2, 4, 8.

Obviously, . In base-2, we went from a list of 1s (e.g., ) to bookending zeros with two 1s (e.g., ). Still a palindromic, but no longer a repunit (a number comprised by only 1s).

It’s a small change, but we can already see that we have prime numbers for even values of n, whereas prime values of n where required for Mersenne numbers to be prime.

We also notice a certain pattern in the exponents above. We have 1, 2, 4, 8. With the exception of 0, all powers of 2. Hold on a second, there. The next number in the sequence appears to be 16. Let’s try that .

Prime! Wouldn’t it be nice if the pattern held? We’d call them Cangiano primes and we would have arbitrarily large primes on demand. If only. 😀

Bad news, guys. is predictably not prime (). Damn it, I had the perfect spot in my house already picked out for the Fields Medal. 😛

Now, I’m curious. Mersenne prime numbers are conjectured to be infinite, but increasingly sparse. What about, prime numbers? Do they stop? Do they follow a similar distribution pattern?

Investigating Mersenne + 2 numbers

Time to help ourselves to modern technology. I wrote a script in Python to test whether other prime numbers in the form exist.

I wanted the program to print both and prime numbers alongside for n below a maximum value provided in input.

It’s worth noting that we could narrow down the exponent to prime numbers only for , but they wouldn’t work for . So a different list of exponent values would have to be used (primes for and regular integers for ).

And since several Mersenne primes are known, you could also hard code them in the program instead.

At this initial exploratory stage, I didn’t really bother because, depending on results, I had a much more significant optimization in mind for .

This is the initial Python script:

import sys
from gmpy2 import is_prime

def mersenne(max):
return __prime_builder(max, lambda k: 2**k - 1)

def mersenne_plus_two(max):
return __prime_builder(max, lambda k: 2**k + 1)

def __prime_builder(max, f):
numbers = []
for k in range(max):
number = f(k)
if is_prime(number):
numbers.append((k, number))
return numbers

if __name__ == "__main__":
args = sys.argv[1:]

if args:
max_k = int(args.pop())
else:
max_k = 10000

print("2^k - 1: ", mersenne(max_k))
print("2^k + 1: ", mersenne_plus_two(max_k))


As an example, the output for is:

\$ python mersenne.py 14
2^k - 1:  [(2, 3), (3, 7), (5, 31), (7, 127), (13, 8191)]
2^k + 1:  [(0, 2), (1, 3), (2, 5), (4, 17), (8, 257)]


Each tuple includes the value of n and the value of .

I checked all the numbers in this format up to . That’s a fairly large number (over 6,000 digits), but are still the only primes. Conversely, there were 24 Mersenne primes below .

I didn’t believe that the n values being powers of 2 for known primes is a coincidence, so I wrote a version of the script that simply focuses on numbers with the following format:

import sys
from gmpy2 import is_prime

def mersenne_plus_two(max):
numbers = []
for k in range(max):
n = 2**(2**k) + 1
if is_prime(n):
numbers.append((k, n))
return numbers

if __name__ == "__main__":
args = sys.argv[1:]

if args:
max_k = int(args.pop())
else:
max_k = 10

print("2^2^k + 1 list:", mersenne_plus_two(max_k))


The now much quicker script failed to provide any prime number above despite searching for numbers up to:

(Ignore the prompt saying 2^k + 1, instead of 2^2^k + 1. I didn’t change it before running the program.)

A conjecture emerges

Based on the – admittedly incomplete – evidence, I conjectured that:

There are no prime numbers of the form , for n in , larger than .

I also believe that this conjecture will be extremely hard to prove.

Fermat’s primes

I did not, however, expect this to be a novel finding. So I searched the OEIS sequence database for the first 5 primes I found, and I came across A092506 which in turn linked to A019434.

The subset ones that we considered at the end are known as Fermat’s primes. From there, with some research online, I was able to determine what we already know about them:

• No prime term larger than 65537 has been found.
• It is believed that there are no larger primes in that form.
• It’s fairly easy to prove that is only prime if it’s a Fermat number (so n has to be a power of 2).

With a feeling of validation, it was time to wrap it up. 🙂

Not before, though, writing on the joy of experimenting in mathematics, asking what if questions, and leveraging programming to explore math.