Large Prime Number Generation
Primality Testing and Prime Number Generation
The Challenge of Large Primes
Finding a prime number with thousands of digits is impossible by trial division — checking divisibility by all smaller numbers — because the number of operations required exceeds the age of the universe. Yet computers routinely generate such primes for use in public-key cryptography, where they protect sensitive information on the internet. The key is that these algorithms do not guarantee 100% certainty; instead they produce probable primes with extremely high confidence (e.g., 99.9999%).
Fermat's Little Theorem
In 1640, the French mathematician Pierre de Fermat proved a fundamental property of all prime numbers. For any prime p and any positive integer a less than p:
This congruence means that ap−1 is exactly one more than a multiple of p. The notation (modp) refers to modular arithmetic, where numbers are considered only by their remainder when divided by the modulus p.
Computing ap−1(modp) for large numbers is done efficiently using modular exponentiation — repeatedly squaring and reducing modulo p — rather than computing the enormous intermediate value.
Fermat Primality Test
The Fermat test uses the contrapositive of Fermat's Little Theorem: if an−1≡1(modn) for some base a, then n is composite (not prime). The base a is called a witness to compositeness.
Example with n=51, a=2: 250≡4(mod51). Since 4≡1, 51 is composite (3×17).
Example with n=17, a=5: 516≡1(mod17). The test declares prime, which is correct.
🚨 Fatal Flaw: Carmichael Numbers
The test can fail for composite numbers because some bases satisfy the congruence even though n is composite. A base that incorrectly passes the test is called a liar.
Carmichael numbers are composite numbers that satisfy an−1≡1(modn) for all bases a that are coprime to n.
The first Carmichael number is 561=3×11×17. For most bases (e.g., a=100), 100560≡1(mod561), so the Fermat test falsely declares 561 prime. Only bases that share a factor with 561 (like a=42, which gives 42560≡375(mod561)) reveal compositeness.
Václav Šimerka discovered the first seven Carmichael numbers in the 19th century. Paul Erdős conjectured infinitely many exist, and in 1994 Red Alford, Andrew Granville, and Carl Pomerance proved that there are indeed infinitely many Carmichael numbers. This means the Fermat test is fundamentally unreliable for large numbers, because randomly finding a base that shares a factor with a huge Carmichael number is as hard as factoring the number itself.
Miller's Test
In the early 1970s, graduate student Gary Miller developed a stronger primality test that fixes the Carmichael problem by examining a sequence of exponentiations instead of just one.
How Miller's Sequence Works
Given n (candidate) and base a, first compute an−1(modn). Then repeatedly divide the exponent by 2, computing a(n−1)/2, a(n−1)/4, etc., until the exponent becomes odd. This produces a sequence of values x0,x1,…,xk where xk=an−1(modn) and each xi is the square of xi−1 modulo n.
Example with n=561, a=100:
Compute 100560≡1(mod561) (Fermat passes here).
Halve exponent: 100280≡1(mod561).
Halve again: 100140≡67(mod561).
10070≡166(mod561).
10035≡298(mod561) (35 is odd; stop).
Sequence: 298,166,67,1,1.
Conditions for Primality
Miller’s test declares n prime if:
The final term is 1 (same as the Fermat condition).
Whenever a 1 appears in the sequence, its immediate predecessor is either 1 or n−1.
In the example above, the final term is 1 (condition 1 met), but the predecessor of the first 1 is 67 — not 1 or 560 — so condition 2 fails. Thus 561 is correctly identified as composite. Base 100 is a witness for Miller’s test but a liar for Fermat’s test.
Example with n=41, a=8:
Sequence: 840≡1(mod41), 820≡40(mod41), 810≡9(mod41) (odd exponent reached). Sequence: 9,40,1. Condition 1: final is 1. Condition 2: before the 1 is 40 (which is n−1), and before that is 9 (but the 9 is not preceded by 1? Actually the sequence goes 9→40→1, so the number before the 1 is 40 (good), and the number before 40 is 9 (no 1 to check). The condition checks every 1's predecessor; here there is only one 1, and its predecessor is 40 (=41-1), so condition 2 holds. The test declares prime, which is correct.
Why Condition 2 Works
The intuition comes from a property of prime moduli: if p is prime, the only square roots of 1 modulo p are 1 and p−1 (which is −1modp). In the sequence, each term is the square of the previous term. If a 1 appears, its predecessor squared must be 1, so that predecessor must be either 1 or p−1. If it is anything else, n cannot be prime because that would be an "illegal" square root of 1.
Miller's Test Still Has Liars
Example with n=221 (13×17), base a=174:
Sequence: 174220≡1, 174110≡220, 17455≡47 (odd). Sequence: 47,220,1. Final term is 1, and predecessor of 1 is 220 = 221−1. Both conditions are satisfied, so Miller’s test falsely declares 221 prime. Base 174 is a liar for Miller’s test.
So Miller’s test, while better, is not perfect for all bases.
The Extended Riemann Hypothesis and a Deterministic Miller Test
Miller proved a remarkable result: if the Extended Riemann Hypothesis (ERH) is true, then for every composite n, there exists a witness base a such that a<2(lnn)2. Consequently, to test n deterministically, one can run Miller’s test with every base a from 2 up to 2(lnn)2. If none reveal compositeness, n must be prime. This is efficient because (lnn)2 grows slowly; even for thousand-digit numbers, the bound is manageable.
The ERH is an extension of the famous Riemann Hypothesis about the distribution of prime numbers. Although most mathematicians believe it is true, it remains unproven. Using a deterministic test that depends on an unproven conjecture is risky, especially for applications like cryptography where lives depend on security.
The Miller–Rabin Probabilistic Test
Miller also showed that for any odd composite n, at least 50% of the possible bases (from 1 to n−1) are witnesses to compositeness under Miller’s test. This means that if one randomly selects a base and runs the test, a false declaration of primality has probability at most 1/2. Repeating the test with independent random bases drives the error probability down exponentially.
Michael Rabin later improved the bound from 50% to 75% (at least 3/4 of bases are witnesses). After k random tests all declaring "prime," the probability that n is actually composite is at most (1/4)k.
With k=10: false positive probability <1/106.
With k=20: <1/1012 (about 1 in a trillion).
This Miller–Rabin test became the gold standard for practical primality testing. It is used in virtually every secure internet connection (TLS/SSL), where large primes are needed for key generation. The test is fast, reliable enough for practical purposes, and does not depend on any unproven hypothesis.
Generating Large Primes with the Prime Number Theorem
Testing for primality and generating a random prime are two different tasks. The strategy for generating a large prime with n digits is deceptively simple:
Pick a random n-digit number.
Test if it is prime (e.g., with Miller–Rabin).
If yes, output it; if no, repeat.
The success of this algorithm relies on the Prime Number Theorem, which estimates the density of primes. For an n-digit number, the probability that a randomly chosen number is prime is approximately:
For 10-digit numbers: ~1 in 23.
For 100-digit numbers: ~1 in 230.
For 1000-digit numbers: ~1 in 2300.
Even though 1 in 2300 seems low, a computer can test thousands of candidates per second, so a 1000-digit prime is typically found within a few seconds. In practice, the Miller–Rabin test is applied to each candidate, and the process terminates quickly.
(An actual implementation in Python can generate a 2281-digit prime — corresponding to the number of subscribers at the time — in a few minutes with 99.9999% confidence using 10 random bases per iteration.)
The AKS Primality Test
In 2002, Manindra Agrawal, Neeraj Kayal, and Nitin Saxena (two undergraduates at IIT Kanpur) discovered a deterministic polynomial-time primality test that always gives a correct answer — no probabilistic uncertainty and no reliance on unproven conjectures. The test, known as the AKS primality test, is a landmark theoretical achievement.
However, the AKS algorithm runs much slower than Miller–Rabin in practice, so it has not replaced the probabilistic test for everyday use. The search continues for a test that is both fast and deterministic, or for a proof of the Extended Riemann Hypothesis that would make Miller’s original deterministic test unconditionally correct. The intersection of number theory and computer science remains an active and fascinating field.

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