Sub topic 4
Why the Primes Never Run Out
As you go up the number line, primes get rarer. Among the first 100 integers, 25 are prime. Between 1,000,000 and 1,000,100 there are only 6. You might expect them to stop at some point. They don't. More than 2,000 years ago, Euclid gave a short argument that every finite list of primes leaves at least one prime out. This chapter walks through that argument, checks it with code, and corrects the misreading of it that people make most often.
This chapter assumes you know:
- the definitions of prime and composite numbers from earlier chapters;
- the fundamental theorem of arithmetic, in particular the fact that every integer greater than 1 has at least one prime factor;
- how
%and integer division work in Python.
The Statement
Theorem (Euclid). No finite list of primes contains every prime. Equivalently, there are infinitely many primes.
The two forms say the same thing. A set is infinite exactly when no finite list exhausts it. The first form is easier to work with because it gives you a concrete task: take any finite list of primes and produce a prime that is missing from it.
The Proof
Let p1, p2, ..., pk be any finite list of primes. Build the number
N = p1 · p2 · ... · pk + 1.
The proof has two steps.
Step 1: N has a prime factor. N is at least 3, because the list contains at least one prime and every prime is at least 2. By the fundamental theorem of arithmetic, every integer greater than 1 is a product of primes, so some prime q divides N. You don't need the full uniqueness part of the theorem here. You only need existence. The smallest divisor of N that is greater than 1 must be prime, since any factor of that divisor would be an even smaller divisor of N.
Step 2: q is not on the list. Suppose q equals some pi. Then q divides the product p1 · ... · pk, and q also divides N. A number that divides two integers also divides their difference, so q divides
N − p1 · ... · pk = 1.
No prime divides 1. So q is not any of the pi.
Starting from an arbitrary finite list, we have found a prime that is not on it. No finite list can be complete.
The remainder view
Step 2 is easier to see with the modulo operation. For every pi on the list, the product is a multiple of pi, so
N % pi == 1.
Every listed prime leaves a remainder of 1, never 0, so none of them divides N. But N must have some prime divisor. That divisor has to come from outside the list.
Direct, not by contradiction
Euclid's argument is often presented as a proof by contradiction: "assume there are finitely many primes, multiply all of them, add 1..." That framing works, but you don't need it. The argument above is direct. It takes any finite list and constructs a missing prime. The direct version is clearer, and it heads off the misconception in the next section.
The Most Common Misreading
The contradiction framing makes the mistake easy to fall into. If you assumed the list held all primes, then N has no prime factor on the list, and it is tempting to conclude "so N is prime." Under that false assumption the conclusion follows, but the assumption is false, so you learn nothing about N in general.
The standard counterexample uses the first six primes:
2 · 3 · 5 · 7 · 11 · 13 + 1 = 30031 = 59 · 509.
30031 is composite. Its prime factors 59 and 509 are new primes, exactly as the proof promises, but neither of them is 30031.
The following program checks the first several "primorial plus one" numbers. The primorial pk# is the product of the first k primes.
def smallest_factor(n):
"""Return the smallest prime factor of n (n >= 2) by trial division."""
d = 2
while d * d <= n:
if n % d == 0:
return d
d += 1
return n
primes = [2, 3, 5, 7, 11, 13, 17, 19]
product = 1
for k, p in enumerate(primes, start=1):
product *= p
n = product + 1
q = smallest_factor(n)
status = "prime" if q == n else f"composite, smallest factor {q}"
assert all(n % pi == 1 for pi in primes[:k]) # remainder 1 for every listed prime
print(f"k={k}: {n} -> {status}")Output:
k=1: 3 -> prime
k=2: 7 -> prime
k=3: 31 -> prime
k=4: 211 -> prime
k=5: 2311 -> prime
k=6: 30031 -> composite, smallest factor 59
k=7: 510511 -> composite, smallest factor 19
k=8: 9699691 -> composite, smallest factor 347The assert line checks Step 2 directly: every listed prime leaves remainder 1. Look at k = 7. The smallest factor is 19, which is smaller than N but still missing from the list {2, 3, 5, 7, 11, 13, 17}. Nothing in the proof says the new prime is larger than every listed prime. It only says the new prime is different from all of them.
The List Does Not Need to Be the First k Primes
The proof works for any finite set of primes. They don't have to be consecutive or start at 2. That makes the proof into a procedure for generating primes. Start with [2]. Repeatedly multiply everything you have, add 1, and append the smallest prime factor of the result. This gives the Euclid–Mullin sequence:
def smallest_factor(n):
d = 2
while d * d <= n:
if n % d == 0:
return d
d += 1
return n
found = [2]
product = 2
while len(found) < 8:
q = smallest_factor(product + 1)
assert q not in found # Euclid guarantees a new prime
found.append(q)
product *= q
print(found) # [2, 3, 7, 43, 13, 53, 5, 6221671]Every step produces a new prime, so the procedure never stalls. The order is erratic. The prime 5 shows up only at the seventh step, and the eighth term is already in the millions. Whether the sequence eventually contains every prime is an open problem. The program stops at eight terms because the ninth term, 38709183810571, takes trial division millions of steps to confirm.
What the Proof Tells You About Size
The construction gives an upper bound. If pk is the k-th prime, the next prime is at most p1 · ... · pk + 1, because some prime factor of that number is new, and every prime larger than pk is no smaller than the next prime. The bound is correct but extremely weak. For k = 6, it says the 7th prime is at most 30031. The 7th prime is actually 17.
| What it shows | Source | Strength |
|---|---|---|
| A new prime is at most p1 · ... · pk + 1 | Euclid | Very weak, grows like a product |
| There is a prime between n and 2n for every n ≥ 1 | Bertrand's postulate (proved by Chebyshev) | Much stronger, doubles at most |
| The count of primes up to n is roughly n / ln n | Prime number theorem | Gives the density, covered in a later chapter |
Euclid's proof shows that the primes are infinite. It says almost nothing about how they are spaced. The later chapter on prime density gives the quantitative picture, and the analysis of the sieve's running time depends on it.
Infinite, Yet with Arbitrarily Long Gaps
Infinitely many primes does not mean they show up at regular intervals. For any m ≥ 2, the m − 1 consecutive numbers
m! + 2, m! + 3, ..., m! + m
are all composite, because m! + j is divisible by j for each j from 2 to m. So there are runs of composites as long as you like. Both facts hold together: the primes never end, and the gaps between them can be arbitrarily long.
import math
def is_prime(n):
if n < 2:
return False
d = 2
while d * d <= n:
if n % d == 0:
return False
d += 1
return True
m = 10
start = math.factorial(m)
run = range(start + 2, start + m + 1)
assert all((start + j) % j == 0 for j in range(2, m + 1))
print(any(is_prime(x) for x in run)) # False: 9 composites in a rowThere are much shorter gaps of the same length elsewhere. For example, 114 to 126 is a run of 13 composites. The factorial construction is just the easiest one to prove.
Common Errors
- Claiming the product plus one is prime. It is only guaranteed to have a prime factor that is not on the list. 30031 = 59 · 509 is the standard counterexample.
- Assuming the new prime is larger than all listed primes. For k = 7 the new prime is 19, which is smaller than the product. It is only guaranteed to be different from the listed primes.
- Skipping Step 1. The proof needs the fact that every integer greater than 1 has a prime factor. Without it, "no listed prime divides N" proves nothing.
- Forgetting the +1. The product by itself is divisible by every listed prime. Adding 1 is what makes every remainder equal to 1.
- Reading infinitude as regularity. The primes are infinite, but the gaps between them have no upper bound.
Exercise
Use the list [3, 5] in Euclid's construction. Compute N, find its prime factors, and confirm that none of them is 3 or 5. What does this show about whether the list must start with 2?
Show answer
N = 3 · 5 + 1 = 16 = 2^4. The only prime factor is 2, which is not in the list. N % 3 == 1 and N % 5 == 1, as Step 2 predicts. The construction works for any finite set of primes, so the list does not need to contain 2 or be consecutive.
Exercise
Someone argues: "Let p be the largest prime. Then p! + 1 is not divisible by any number from 2 to p, so p! + 1 is prime and larger than p, a contradiction." Is the conclusion "p! + 1 is prime" justified? Give a corrected version of the argument.
Show answer
No. p! + 1 is not divisible by any prime up to p, but it can still be composite with all its prime factors larger than p. For example, 5! + 1 = 121 = 11^2. The corrected argument: p! + 1 > 1, so it has a prime factor q. Every integer from 2 to p divides p!, so if one of them divided p! + 1 it would divide 1, which is impossible. So q > p, and that contradicts p being the largest prime.
Exercise
Write a Python function that takes a list of primes and returns one prime not in the list, using Euclid's construction and trial division. Test it on [2, 3, 5, 7, 11, 13].
Show answer
def missing_prime(ps): n = 1 for p in ps: n = p n += 1 d = 2 while d d <= n: if n % d == 0: return d d += 1 return n For [2, 3, 5, 7, 11, 13], n = 30031 and the function returns 59, a prime not in the list.