Prime Numbers and Euclid’s Proof
Prime Numbers: Discovery, Testing, and Mersenne Primes
Prime numbers and the search for larger primes
A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. A number greater than 1 that is not prime is composite. The number 1 is neither prime nor composite, and 2 is the only even prime.
Prime numbers can be extremely large. A prime with 687 digits can be written compactly as 22281−1. As of 2024, the largest known prime was
a Mersenne prime with 24,862,048 digits, discovered in 2018.
There is no known simple, practical formula that directly generates all prime numbers in order. Instead, mathematicians use algorithms to find primes or to test whether particular numbers are prime. The methods differ in what they do:
Sieves find many primes up to a chosen limit.
Primality tests examine one particular number.
Some tests quickly identify composite numbers or probable primes; others can prove primality with certainty.
Why there are infinitely many primes
Euclid’s proof shows that no finite list can contain all the primes. It uses the Fundamental Theorem of Arithmetic: every integer greater than 1 is either prime or can be expressed uniquely as a product of primes, apart from the order of the factors. For example,
Suppose, for contradiction, that all primes have been listed as p1,p2,…,pk. Form the number
There are two possibilities:
If Q is prime, it is not on the list, because it is larger than each listed prime.
If Q is composite, it has a prime divisor. Dividing Q by any listed prime leaves remainder 1, so none of the listed primes divides it. Its prime divisor must therefore be missing from the list.
Both possibilities contradict the assumption that the list contains every prime. Thus, there are infinitely many primes.
The number Q itself need not be prime. The argument only requires that it has a prime divisor not included in the supposed complete list.
Finding primes by sieving
Divisor pairs and the square-root limit
A direct way to test whether n is prime is to check whether any integer from 2 through n−1 divides it. This is inefficient. Divisors occur in pairs: if d divides n, then n/d is also a divisor. In each pair, at least one divisor is at most n. Therefore, a number needs to be tested for divisors only up to its square root.
It is enough to test prime divisors in that range. If a composite number divides n, that divisor has a prime factor, which also divides n.
For example, to test whether 1,000,001 is prime, it is sufficient to check prime divisors no larger than 1,000,001, which is just over 1,000. This is far fewer checks than testing every smaller integer.
The Sieve of Eratosthenes
The Sieve of Eratosthenes finds all primes up to a chosen limit:
Write down the integers from 2 through the limit.
Mark 2 as prime, then cross out its larger multiples as composite.
Move to the next number that has not been crossed out. It is prime; cross out its larger multiples.
Continue until the next number to process is greater than the square root of the limit. Any remaining unmarked numbers are prime.
For example, after marking the multiples of 2, the next unmarked number is 3. Marking its multiples removes composite numbers such as 9 and 15. The process avoids testing each number independently because a composite number is crossed out as a multiple of one of its prime factors.
The Sieve of Atkin
The Sieve of Atkin is another method for generating all primes up to a limit. It uses quadratic expressions and modular arithmetic. It begins by listing 2, 3, and 5, then examines values produced by these expressions:
For positive integers x and y, candidate values are selected according to their remainders modulo 60:
For 4x2+y2, candidates have remainders 1,13,17,29,37,41,49, or 53.
For 3x2+y2, candidates have remainders 7,19,31, or 43.
For 3x2−y2, with x>y, candidates have remainders 11,23,47, or 59.
The sieve toggles candidate numbers according to the number of ways they are produced by these expressions: an eligible number is marked or unmarked as the count changes between odd and even. It then removes multiples of the squares of numbers still marked as prime, starting with 72 and continuing in the same way for later candidates.
This method can find primes up to large limits efficiently. A sieve is useful when many primes are needed from the beginning of the number line, but it is not necessarily the best tool when only one very large number needs to be tested.
Probabilistic tests for a specific number
Fermat’s Little Theorem
Fermat’s Little Theorem states that if p is prime, then for any integer a,
Equivalently, if a is not divisible by p, then
Here, congruence modulo p means that the two sides have the same remainder when divided by p.
For example, because 7 is prime,
is divisible by 7. But passing this test does not prove that a number is prime. A composite number may also satisfy the same congruence for a particular choice of a.
For example, 341 passes the test using a=2, even though
Using a=3 exposes its compositeness: it fails the test. A composite number that passes a Fermat test for a particular base is called a Fermat pseudoprime to that base.
Some composites, called Carmichael numbers, pass the Fermat test for every base relatively prime to the number. They are uncommon—there are 43 up to 1,000,000—but they show why Fermat testing alone cannot guarantee primality. Passing such tests provides evidence for a probable prime, not a proof.
The Miller–Rabin test
The Miller–Rabin test is a stronger probabilistic test. For an odd candidate n>2, write
where d is odd. Choose a base a with 1<a<n, and calculate ad modulo n. The test then checks this value and successive squares.
The candidate passes the round if either:
ad≡1(modn), or
one of the values obtained by repeatedly squaring ad modulo n, for at most s−1 squarings, is congruent to −1 modulo n.
If neither condition holds, n is definitely composite. If it passes, it is a strong probable prime to base a. A base that reveals compositeness is called a witness; a base that fails to reveal it is sometimes called a liar for that composite number.
For n=221,
Using a=13, the initial calculation gives 1355≡208(mod221). The next square is 2082≡169(mod221), and the following square is 1692≡52(mod221). None of the checked values is 1 or −1 modulo 221, so the test proves that 221 is composite.
For n=37,
Using a=8, the initial value is 89≡6(mod37). Squaring gives 62≡36≡−1(mod37), so 37 passes this round. Using a different base, such as a=11, provides another passing round.
Passing one or more rounds still does not prove primality. A composite such as 85 can pass for a particular base: with 85−1=22⋅21, using a=13 gives 1321≡13(mod85), and 132≡−1(mod85). Thus, this round does not detect that 85=5⋅17.
For any odd composite number, at most one quarter of the possible bases can cause it to pass a Miller–Rabin round. With independently and randomly selected bases, the probability that a composite passes k rounds is therefore at most
After five independent rounds, this bound is 1/1024, or less than 0.1%. The test is especially useful because, unlike Fermat’s test, no composite number passes Miller–Rabin for every possible base.
A definitive primality test: AKS
The Agrawal–Kayal–Saxena (AKS) primality test gives a deterministic answer: it can establish with certainty whether any input number is prime or composite.
Its central idea comes from a polynomial identity. For prime p, the binomial theorem gives
because every intermediate binomial coefficient is divisible by p. In other words, the coefficients of the difference (x+1)p−xp−1 are divisible by p.
AKS uses this property as part of a more extensive test involving polynomial congruences. Its theoretical importance is that it proves primality testing can be performed in polynomial time in the number of digits. However, theoretical efficiency does not necessarily mean practical speed: AKS is much slower in practice than specialized methods for many large-number problems. The test is a conclusive alternative when certainty is required.
Mersenne numbers and Mersenne primes
A Mersenne number has the form
If n is composite, say n=xy for integers x,y>1, then
This factorization shows that 2n−1 is composite whenever n is composite. Therefore, a Mersenne number can be prime only if its exponent is prime.
The converse is not true: a prime exponent does not guarantee a prime Mersenne number. For example, 11 is prime, but
The first Mersenne primes are 3, 7, and 31. Many of the largest known primes are Mersenne primes because there is a particularly efficient test for numbers of this form.
Perfect numbers and Mersenne primes
A perfect number is a positive integer equal to the sum of its positive divisors other than itself. Equivalently, the sum of all its positive divisors, including itself, is twice the number.
The first perfect numbers are 6, 28, and 496. For instance, the proper divisors of 28 are 1,2,4,7, and 14, and
A Mersenne prime produces a perfect number. If 2p−1 is prime, then
is perfect. For example, 23−1=7 is prime, and
To see why the formula works, the positive divisors of N consist of the powers
and those same powers multiplied by the prime factor 2p−1. The sum of the powers of 2 is
Thus, the sum of all divisors is
Consequently, the sum of the proper divisors is N, as required. The result is part of the Euclid–Euler theorem: every even perfect number arises from a Mersenne prime in this way. Whether any odd perfect numbers exist remains unknown.
The Lucas–Lehmer test
The Lucas–Lehmer test is a specialized, highly efficient method for testing Mersenne numbers. For an odd prime exponent p, define a sequence by
The test computes the sequence modulo Mp=2p−1 and checks the term Sp−2. The criterion is:
For an odd prime p, the Mersenne number 2p−1 is prime if and only if Sp−2≡0(mod2p−1).
For p=3, the first sequence terms are S0=4 and S1=42−2=14. Since 14 is divisible by 7=23−1, the test confirms that 7 is prime.
The sequence grows rapidly if calculated as ordinary integers. Instead, after every step the value can be reduced modulo Mp. Since only the remainder matters, this keeps the numbers manageable. For example, testing 213−1=8191 requires the term S11, calculated with modular reduction at each step; that term is congruent to 0 modulo 8191.
Why the Lucas–Lehmer criterion works
The proof uses group theory, the study of sets equipped with operations that satisfy particular rules.
A group must satisfy four conditions:
Closure: combining two elements produces another element of the group.
Associativity: grouping the operations differently does not change the result.
Identity: there is an element that leaves every other element unchanged.
Inverses: every element has another element that combines with it to give the identity.
The integers under addition form a group: the identity is 0, and the inverse of an integer is its negative. The integers modulo 10, under addition modulo 10, form a group with elements 0 through 9. For example, 5+6=11≡1(mod10).
The order of a group is its number of elements. The order of an element is the smallest positive number of times it must be combined with itself to obtain the identity. In the group of integers modulo 10 under addition:
The order of 4 is 5, since 4+4+4+4+4≡0(mod10).
The order of 3 is 10, since ten copies of 3 sum to 30≡0(mod10).
An element’s order cannot exceed the group’s order. More strongly, Lagrange’s theorem says that its order divides the group’s order.
To connect the sequence to group theory, define
Then uv=1, and the sequence has the expression
This expression gives S0=4. Squaring the expression and subtracting 2 produces the next term because uv=1.
For a Mersenne number Mp=2p−1, the Lucas–Lehmer test checks whether Mp divides Sp−2. To outline why this implies primality, suppose the test passes but Mp is composite, and let q be its smallest prime factor. Then q2≤Mp.
Work with expressions of the form a+b3 whose coefficients are taken modulo q. These expressions form a finite multiplication system with q2 possible elements. Because uv=1, the element u has an inverse, so it belongs to the group of invertible elements. That group has at most q2−1 elements, since zero is not invertible.
If Sp−2≡0(modq), then
Multiplying by u2p−2 gives u2p−1≡−1(modq), and squaring gives u2p≡1(modq). But u2p−1≡1(modq). Therefore, the order of u is exactly 2p.
That is impossible: the order of an element cannot exceed the size of its group, which is at most q2−1. Since
the order cannot be 2p. This contradiction shows that Mp cannot be composite, so it must be prime.
Distributed searches for Mersenne primes
The Great Internet Mersenne Prime Search (GIMPS) is a volunteer computing project that uses distributed computers to search for Mersenne primes. Its software allows many volunteers to test different candidate exponents in parallel.
The largest prime known as of 2024, 282,589,933−1, was discovered through this effort. A single computer can take a long time to test a Mersenne number with millions of digits, while the combined computing power of many volunteers allows many candidates to be examined.
Prime numbers can be written compactly using mathematical notation even when their decimal expansions are enormous. Some other numbers are so large that writing out their digits would require more symbols than there are atoms in the observable universe.

Ask anything
We've read this whole study set, so questions about it get the best answers.