
Mersenne Perfection
How Euclid and Euler found the connection between perfect and prime numbers
Perfect numbers
Every integer has divisors, or numbers that divide evenly. Prime numbers are only divisible by and , but composite numbers like have more divisors,
gp > divisors(12)
%1 = [1, 2, 3, 4, 6, 12]
This uses the divisors
function from the PARI/GP language. The PARI/GP reference card lists built-in functions for number theory in the Elementary Arithmetic Functions, such as numdiv
, which returns the number of divisors, e.g., numdiv(12) = 6
, and the sum of the divisors sigma(12) = 28
. We can define a variant of this function, which is the sum of the divisors less than , , so This is where things get interesting, because if then is called a perfect number.
Perfect numbers are rather rare - the smallest is because the divisors of are and the sum of the divisors less than is The next largest is ,
gp > divisors(28)
%2 = [1, 2, 4, 7, 14, 28]
and The first five perfect numbers are and the OEIS (Online Encyclopedia of Integer Sequences) gives the first ten.
If then is a superabundant number, and if , then is deficient. Only when is a perfect number.
Euclid’s insight
Notice that is the sum of the first three integers because they happen to go into evenly. But, it turns out that,
which are the sums of the first integers. A formula for the sum of the first integers is . For each perfect number , we can find the corresponding by
using the quadratic formula and discarding the negative root. Try this out in PARI,
gp > (sqrt(8*496+1)-1)/2
%3 = 31.000000000000000000000000000000000000
which confirms that the perfect number is the sum of the first integers. You can write a simple PARI function to calculate from ,
q(S) = (sqrt(8*S+1)-1)/2
You might have noticed that is one less than a power of , or for some value of . Solving for ,
and the equivalent PARI function is
r(S) = log(q(S)+1)/log(2)
Input the OEIS list of perfect numbers and you’ll find that the corresponding At the time of Euclid, only the first four perfect numbers were known, but Euclid saw the pattern of prime numbers for .
We don’t know for sure that prime numbers are connected to perfect numbers, but we can work backwards to go from any prime to a potential perfect number,
Written as a PARI function,
S(p) = 2^(p-1)*(2^p - 1)
which returns for the first four primes.

Raphael’s fresco “Stanza della Segnatura” (The School of Athens). Euclid is in the red robe on the right, teaching geometry.
Marin Mersenne
Marin Mersenne was a century French polymath and Catholic priest who studied mathematics, music, and philosophy. He wrote about music theory and developed formulas relating musical pitch to properties of a stretched string.

Marin Mersenne.
Mersenne discovered that is a prime number for some values of when is also a prime number. The OEIS sequence A000043 lists some of the known Mersenne exponents, and the largest known exponent is Notice that in the formula for perfect numbers, the second term is a Mersenne prime while the first term is constructed from the same prime exponent.
Mathematicians are still puzzling over many ideas surrounding Mersenne primes. Mersenne published a conjecture in his Cogitata Physico-Mathematica that the primes generated Mersenne primes (see OEIS A109461), but and do not generate primes, and he missed , and . During his time, nobody could perform the calculations for any prime greater than , but PARI finds the factors for ,
gp > factor(2^67-1)
%4 =
[ 193707721 1]
[761838257287 1]
A more recent conjecture is the Bateman, Selfridge, and Wagstaff conjecture, which says that for any odd natural number if is a Mersenne prime and or for some natural number then is a prime.
A more interesting conjecture is that there are infinitely many Mersenne primes, and the number of these primes less than any integer is
where is the Euler-Mascheroni constant.
Conjectures are interesting, but what can be proven?
- We’ve shown the relationship between Mersenne primes and the sum of the first natural numbers, but why is a perfect number?
- Are there any Mersenne primes where is not a prime number?
Proofs

How mathematicians do proofs.
The Euclid-Euler Theorem
The Euclid-Euler theorem says that an even number is perfect if and only if where is prime. Euclid proved the forward direction, “if”, while Euler proved the reverse, “only if”.
Theorem: Every Mersenne prime generates an even perfect number (Euclid).
Proof: We need to show that the sum of the divisors of (excluding itself) is equal to , or Equivalently, we could show that Since is a Mersenne prime, then the only divisors are 1 and , and the divisors of are the powers of up to , . Then, since there are no divisors in common with the first and second terms other than .
The first term is a geometric series, and if we multiply by two and subtract the original, we get
and the sum of the divisors of the Mersenne prime is , so
so is perfect.
Theorem: Every even perfect number may be written as where is a Mersenne prime, (Euler).
Proof: Let be an even perfect number, so where is odd. Since is perfect,
and so
We can write , or which gives
which implies that
Notice that since is even, so and odd, which means that is a proper divisor of But, is the sum of all proper divisors of which implies that has only one proper divisor, meaning is prime and So, is a Mersenne prime and is a prime.
Perfect is the sum of the first integers
Theorem: If is a Mersenne prime, then is a perfect number.
Proof: Every Mersenne prime is of the form by definition. The sum of the first integers is
The previous theorem showed that every perfect number
Product theorem for
Before we get to the next theorem, let’s look at an example. Suppose you calculate the sum of the divisors of a number, say , which has divisors so. The prime factors of are and , which means that
In this case, and and their product is . We’ve been using this fact without proof, but we’ll show how it is derived in the next theorem.
Theorem: If is a composite integer and () then
Proof: Since the , then and have only as a common divisor, so the divisors of are the products of the divisors of and the divisors of . Suppose these divisors are for and for . The sum of the divisors is and so we can write the divisors of as
Summing each row gives
The Fundamental Theorem of Arithmetic says that every positive integer may be written in exactly one way as the product of primes. For the example of the Product Theorem for says that However, if you split the power of so that the theorem doesn’t hold,
is prime only when is prime
Theorem: No Mersenne prime exists where and is not prime.
Proof: Suppose is composite, so . Then is a binomial number, which factors into
GIMPS
The oldest unsolved problem in mathematics is the question: Are there any odd perfect numbers? The question may remain open for a while longer, but if you would like to search for the next Mersenne prime, you can sign up at the Great Internet Mersenne Prime Search (GIMPS) website. Currently, the largest known Mersenne prime is , but you can use their software to search for the next one.
GIMPS consists of a team of volunteers who run a program on their computers that searches for Mersenne primes. The program uses the Fermat primality test, which can quickly estimate the probability of a number being prime. More details can be found on the How GIMPS Works page.
Exercises
There are some very nice exercises in the LibreTexts chapters Number Theoretic Functions and Perfect Numbers and Mersenne Primes, or you might like to try a few problems using PARI/GP. For details about installing and using PARI/GP and Notepad++, see our article Put Another KenKen on the Barbie.
- Scan a range for Mersenne primes: For primes , print the exponents such that is prime. Hint: Use
forprime
andisprime
. - Generate even perfect numbers and verify perfection: For each Mersenne prime found in (1), compute the corresponding even perfect number and verify Hint: Use
sigma
. - Test that when is a composite number, is also composite. Try this for all values of .
- Count the number of Mersenne primes in an interval, for example, using , how many result in is also prime?
- For each prime , compute the perfect number , and count the number of digits in . Hint: Use
digits(S)
.
Code for this article
The PARI/GP functions are quite simple, and you can easily write your own:
# Sum of the first n numbers for perfect number S.
q(S) = (sqrt(8*S+1)-1)/2
# Prime exponent for perfect number S.
r(S) = log(q(S)+1)/log(2)
# Perfect number S generated by prime p. Not every p generates a perfect S.
S(p) = 2^(p-1)*(2^p - 1)
Software
- PARI/GP is a widely used computer algebra system designed for fast computations in number theory (factorizations, algebraic number theory, elliptic curves, modular forms, L functions…), but also contains a large number of other useful functions to compute with mathematical entities such as matrices, polynomials, power series, algebraic numbers etc., and a lot of transcendental functions.
- Notepad++ is a free (as in “free speech” and also as in “free beer”) source code editor and Notepad replacement that supports several languages.
References
- The Oldest Unsolved Problem in Math, Veritasium (YouTube).
- Perfect Numbers and Fibonacci Sequences, Tianxin Cai, World Scientific Publishing, 2022.
- Euler - The Master of Us All, William Dunham, MAA Press, 1999.
- Distributions of Mersenne Divisors, Sidney Kravitz, American Mathematical Society, Nov 29, 1965.
- The Mysterious Math of Perfection, Patrick Honner, Quanta Magazine, Mar 15, 2021.
- Science Bytes: Prime Attraction Sublime - An Arts and Science Blog, Wallace Fong.
- Great Internet Mersenne Prime Search (GIMPS)
- Perfect numbers, MacTutor.
- Number Theoretic Functions, Mike Barrus & W. Edwin Clark, University of Rhode Island, LibreTexts.
- Perfect Numbers and Mersenne Primes, Mike Barrus & W. Edwin Clark, University of Rhode Island, LibreTexts.
- Perfect Number, Wolfram MathWorld.
- Mersenne Prime, Wolfram MathWorld.
- Mersenne Prime, Wikipedia.
- Euclid-Euler Theorem, Wikipedia.
- Online Encyclopedia of Integer Sequences (OEIS), N. Sloane, Perfect Numbers (A000396), Mersenne Primes (A109461), Mersenne exponents (A000043).
Image credits
- Abstract representation of Mersenne and Perfect numbers, ChatGPT.
- The Stanza della Segnatura, Raphael Rooms, Apostolic Palace, Vatican City - Wikimedia Commons by Lure.
- Marin Mersenne, Wikimedia Commons.
- You want proof? Sidney Harris.