1. Home
  2. Explore
  3. Mathematics
  4. Prime Numbers and Euclid’s Proof

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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 111 whose only positive divisors are 111 and itself. A number greater than 111 that is not prime is composite. The number 111 is neither prime nor composite, and 222 is the only even prime.

Prime numbers can be extremely large. A prime with 687 digits can be written compactly as 22281−12^{2281}-122281−1. As of 2024, the largest known prime was

282,589,933−1,2^{82,589,933}-1,282,589,933−1,

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 111 is either prime or can be expressed uniquely as a product of primes, apart from the order of the factors. For example,

260=22⋅5⋅13.260=2^2\cdot 5\cdot 13.260=22⋅5⋅13.

Suppose, for contradiction, that all primes have been listed as p1,p2,…,pkp_1,p_2,\ldots,p_kp1​,p2​,…,pk​. Form the number

Q=p1p2⋯pk+1.Q=p_1p_2\cdots p_k+1.Q=p1​p2​⋯pk​+1.

There are two possibilities:

  • If QQQ is prime, it is not on the list, because it is larger than each listed prime.

  • If QQQ is composite, it has a prime divisor. Dividing QQQ by any listed prime leaves remainder 111, 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 QQQ 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 nnn is prime is to check whether any integer from 222 through n−1n-1n−1 divides it. This is inefficient. Divisors occur in pairs: if ddd divides nnn, then n/dn/dn/d is also a divisor. In each pair, at least one divisor is at most n\sqrt nn​. 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 nnn, that divisor has a prime factor, which also divides nnn.

For example, to test whether 1,000,0011,000,0011,000,001 is prime, it is sufficient to check prime divisors no larger than 1,000,001\sqrt{1,000,001}1,000,001​, which is just over 1,0001,0001,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:

  1. Write down the integers from 222 through the limit.

  2. Mark 222 as prime, then cross out its larger multiples as composite.

  3. Move to the next number that has not been crossed out. It is prime; cross out its larger multiples.

  4. 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 222, the next unmarked number is 333. Marking its multiples removes composite numbers such as 999 and 151515. 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 222, 333, and 555, then examines values produced by these expressions:

4x2+y2,3x2+y2,3x2−y2.4x^2+y^2,\qquad 3x^2+y^2,\qquad 3x^2-y^2.4x2+y2,3x2+y2,3x2−y2.

For positive integers xxx and yyy, candidate values are selected according to their remainders modulo 606060:

  • For 4x2+y24x^2+y^24x2+y2, candidates have remainders 1,13,17,29,37,41,49,1,13,17,29,37,41,49,1,13,17,29,37,41,49, or 535353.

  • For 3x2+y23x^2+y^23x2+y2, candidates have remainders 7,19,31,7,19,31,7,19,31, or 434343.

  • For 3x2−y23x^2-y^23x2−y2, with x>yx>yx>y, candidates have remainders 11,23,47,11,23,47,11,23,47, or 595959.

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 727^272 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 ppp is prime, then for any integer aaa,

ap≡a(modp).a^p\equiv a\pmod p.ap≡a(modp).

Equivalently, if aaa is not divisible by ppp, then

ap−1≡1(modp).a^{p-1}\equiv 1\pmod p.ap−1≡1(modp).

Here, congruence modulo ppp means that the two sides have the same remainder when divided by ppp.

For example, because 777 is prime,

27−2=1262^7-2=12627−2=126

is divisible by 777. 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 aaa.

For example, 341341341 passes the test using a=2a=2a=2, even though

341=11⋅31.341=11\cdot 31.341=11⋅31.

Using a=3a=3a=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,0001,000,0001,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>2n>2n>2, write

n−1=2sd,n-1=2^s d,n−1=2sd,

where ddd is odd. Choose a base aaa with 1<a<n1<a<n1<a<n, and calculate ada^dad modulo nnn. The test then checks this value and successive squares.

The candidate passes the round if either:

  • ad≡1(modn)a^d\equiv 1\pmod nad≡1(modn), or

  • one of the values obtained by repeatedly squaring ada^dad modulo nnn, for at most s−1s-1s−1 squarings, is congruent to −1-1−1 modulo nnn.

If neither condition holds, nnn is definitely composite. If it passes, it is a strong probable prime to base aaa. 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=221n=221n=221,

221−1=220=22⋅55.221-1=220=2^2\cdot 55.221−1=220=22⋅55.

Using a=13a=13a=13, the initial calculation gives 1355≡208(mod221)13^{55}\equiv 208\pmod{221}1355≡208(mod221). The next square is 2082≡169(mod221)208^2\equiv169\pmod{221}2082≡169(mod221), and the following square is 1692≡52(mod221)169^2\equiv52\pmod{221}1692≡52(mod221). None of the checked values is 111 or −1-1−1 modulo 221221221, so the test proves that 221221221 is composite.

For n=37n=37n=37,

37−1=36=22⋅9.37-1=36=2^2\cdot 9.37−1=36=22⋅9.

Using a=8a=8a=8, the initial value is 89≡6(mod37)8^9\equiv6\pmod{37}89≡6(mod37). Squaring gives 62≡36≡−1(mod37)6^2\equiv36\equiv-1\pmod{37}62≡36≡−1(mod37), so 373737 passes this round. Using a different base, such as a=11a=11a=11, provides another passing round.

Passing one or more rounds still does not prove primality. A composite such as 858585 can pass for a particular base: with 85−1=22⋅2185-1=2^2\cdot2185−1=22⋅21, using a=13a=13a=13 gives 1321≡13(mod85)13^{21}\equiv13\pmod{85}1321≡13(mod85), and 132≡−1(mod85)13^2\equiv-1\pmod{85}132≡−1(mod85). Thus, this round does not detect that 85=5⋅1785=5\cdot1785=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 kkk rounds is therefore at most

(14)k.\left(\frac14\right)^k.(41​)k.

After five independent rounds, this bound is 1/10241/10241/1024, or less than 0.1%0.1\%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 ppp, the binomial theorem gives

(x+1)p≡xp+1(modp),(x+1)^p\equiv x^p+1\pmod p,(x+1)p≡xp+1(modp),

because every intermediate binomial coefficient is divisible by ppp. In other words, the coefficients of the difference (x+1)p−xp−1(x+1)^p-x^p-1(x+1)p−xp−1 are divisible by ppp.

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

Mn=2n−1.M_n=2^n-1.Mn​=2n−1.

If nnn is composite, say n=xyn=xyn=xy for integers x,y>1x,y>1x,y>1, then

2xy−1=(2x−1)(1+2x+22x+⋯+2(y−1)x).2^{xy}-1=(2^x-1)\left(1+2^x+2^{2x}+\cdots+2^{(y-1)x}\right).2xy−1=(2x−1)(1+2x+22x+⋯+2(y−1)x).

This factorization shows that 2n−12^n-12n−1 is composite whenever nnn 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, 111111 is prime, but

211−1=2047=23⋅89.2^{11}-1=2047=23\cdot89.211−1=2047=23⋅89.

The first Mersenne primes are 333, 777, and 313131. 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 666, 282828, and 496496496. For instance, the proper divisors of 282828 are 1,2,4,7,1,2,4,7,1,2,4,7, and 141414, and

1+2+4+7+14=28.1+2+4+7+14=28.1+2+4+7+14=28.

A Mersenne prime produces a perfect number. If 2p−12^p-12p−1 is prime, then

N=2p−1(2p−1)N=2^{p-1}(2^p-1)N=2p−1(2p−1)

is perfect. For example, 23−1=72^3-1=723−1=7 is prime, and

23−1(23−1)=4⋅7=28.2^{3-1}(2^3-1)=4\cdot7=28.23−1(23−1)=4⋅7=28.

To see why the formula works, the positive divisors of NNN consist of the powers

1,2,22,…,2p−11,2,2^2,\ldots,2^{p-1}1,2,22,…,2p−1

and those same powers multiplied by the prime factor 2p−12^p-12p−1. The sum of the powers of 222 is

1+2+⋯+2p−1=2p−1.1+2+\cdots+2^{p-1}=2^p-1.1+2+⋯+2p−1=2p−1.

Thus, the sum of all divisors is

(2p−1)(1+(2p−1))=(2p−1)2p=2N.(2^p-1)\bigl(1+(2^p-1)\bigr) =(2^p-1)2^p =2N.(2p−1)(1+(2p−1))=(2p−1)2p=2N.

Consequently, the sum of the proper divisors is NNN, 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 ppp, define a sequence by

S0=4,Sk+1=Sk2−2.S_0=4,\qquad S_{k+1}=S_k^2-2.S0​=4,Sk+1​=Sk2​−2.

The test computes the sequence modulo Mp=2p−1M_p=2^p-1Mp​=2p−1 and checks the term Sp−2S_{p-2}Sp−2​. The criterion is:

For an odd prime ppp, the Mersenne number 2p−12^p-12p−1 is prime if and only if Sp−2≡0(mod2p−1)S_{p-2}\equiv0\pmod{2^p-1}Sp−2​≡0(mod2p−1).

For p=3p=3p=3, the first sequence terms are S0=4S_0=4S0​=4 and S1=42−2=14S_1=4^2-2=14S1​=42−2=14. Since 141414 is divisible by 7=23−17=2^3-17=23−1, the test confirms that 777 is prime.

The sequence grows rapidly if calculated as ordinary integers. Instead, after every step the value can be reduced modulo MpM_pMp​. Since only the remainder matters, this keeps the numbers manageable. For example, testing 213−1=81912^{13}-1=8191213−1=8191 requires the term S11S_{11}S11​, calculated with modular reduction at each step; that term is congruent to 000 modulo 819181918191.

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 000, and the inverse of an integer is its negative. The integers modulo 101010, under addition modulo 101010, form a group with elements 000 through 999. For example, 5+6=11≡1(mod10)5+6=11\equiv1\pmod{10}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 101010 under addition:

  • The order of 444 is 555, since 4+4+4+4+4≡0(mod10)4+4+4+4+4\equiv0\pmod{10}4+4+4+4+4≡0(mod10).

  • The order of 333 is 101010, since ten copies of 333 sum to 30≡0(mod10)30\equiv0\pmod{10}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

u=2+3,v=2−3.u=2+\sqrt3,\qquad v=2-\sqrt3.u=2+3​,v=2−3​.

Then uv=1uv=1uv=1, and the sequence has the expression

Sk=u2k+v2k.S_k=u^{2^k}+v^{2^k}.Sk​=u2k+v2k.

This expression gives S0=4S_0=4S0​=4. Squaring the expression and subtracting 222 produces the next term because uv=1uv=1uv=1.

For a Mersenne number Mp=2p−1M_p=2^p-1Mp​=2p−1, the Lucas–Lehmer test checks whether MpM_pMp​ divides Sp−2S_{p-2}Sp−2​. To outline why this implies primality, suppose the test passes but MpM_pMp​ is composite, and let qqq be its smallest prime factor. Then q2≤Mpq^2\le M_pq2≤Mp​.

Work with expressions of the form a+b3a+b\sqrt3a+b3​ whose coefficients are taken modulo qqq. These expressions form a finite multiplication system with q2q^2q2 possible elements. Because uv=1uv=1uv=1, the element uuu has an inverse, so it belongs to the group of invertible elements. That group has at most q2−1q^2-1q2−1 elements, since zero is not invertible.

If Sp−2≡0(modq)S_{p-2}\equiv0\pmod qSp−2​≡0(modq), then

u2p−2+u−2p−2≡0(modq).u^{2^{p-2}}+u^{-2^{p-2}}\equiv0\pmod q.u2p−2+u−2p−2≡0(modq).

Multiplying by u2p−2u^{2^{p-2}}u2p−2 gives u2p−1≡−1(modq)u^{2^{p-1}}\equiv-1\pmod qu2p−1≡−1(modq), and squaring gives u2p≡1(modq)u^{2^p}\equiv1\pmod qu2p≡1(modq). But u2p−1≢1(modq)u^{2^{p-1}}\not\equiv1\pmod qu2p−1≡1(modq). Therefore, the order of uuu is exactly 2p2^p2p.

That is impossible: the order of an element cannot exceed the size of its group, which is at most q2−1q^2-1q2−1. Since

q2−1<2p,q^2-1<2^p,q2−1<2p,

the order cannot be 2p2^p2p. This contradiction shows that MpM_pMp​ 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−12^{82,589,933}-1282,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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent