# 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:

$$\displaystyle 2^{74,207,281}-1$$

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:

$$\displaystyle M_n \equiv 2^n-1$$

for n in $$\mathbb{N}$$.

Not all Mersenne numbers are prime (e.g., $$M_4 = 15$$), and not all prime numbers are Mersenne numbers (e.g., $$M_4 < 29 < M_5$$).

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 $$M_n$$ is prime or not.

It turns out that n needs to be prime as well, in order for $$M_n$$ 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 $$2^n-1$$ (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 $$M_n$$ form for $$n <= 257$$ 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:

$$\displaystyle C_n \equiv 2^n+1$$

for n in $$\mathbb{N}$$? What changes? Let’s find out.

Much like Mersenne did three century ago for his numbers, we can trivially see that some $$C_n$$ 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, $$C_n = M_n + 2$$. In base-2, we went from a list of 1s (e.g., $$M_3 = 111_2$$) to bookending zeros with two 1s (e.g., $$C_4 = 10001_2$$). 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 $$C_{16} = 65537$$.

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. $$C_{32} = 4294967297$$ is predictably not prime ($$641 \times 6700417$$). 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, $$2^n + 1$$ 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 $$2^n + 1$$ exist.

I wanted the program to print both $$M_n$$ and $$C_n$$ 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 $$M_n$$, but they wouldn’t work for $$C_n$$. So a different list of exponent values would have to be used (primes for $$M_n$$ and regular integers for $$C_n$$).

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 $$C_n$$.

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 $$n = 14$$ 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 $$2^n+1$$.

I checked all the numbers in this format up to $$2^{19,999}+1$$. That’s a fairly large number (over 6,000 digits), but $$C_0, C_1, C_2, C_4, C_8, C_{16}$$ are still the only primes. Conversely, there were 24 Mersenne primes below $$2^{19,999}-1$$.

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 $$C_n$$ numbers with the following format:

$$\displaystyle 2^{2^k}+1$$

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 $$65537$$ despite searching for numbers up to:

$$\displaystyle 2^{2^{19}}+1 = 2^{524,288}+1$$

(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 $$2^{n}+1$$, for n in $$\mathbb{N}$$, larger than $$2^{16}+1$$.

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 $$2^2^k + 1$$ 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 $$2^n+1$$ 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.

#### Get more stuff like this

Something went wrong.