How to Test Whether a Large Number Is Prime
Work through trial division, the Fermat test and certificate methods, with checked examples showing which tool fits which size of number.
Key takeaways
- Trial division only needs divisors up to √n, but it becomes hopeless for numbers with dozens of digits.
- The Fermat test can prove a number composite, yet passing it does not prove a number prime.
- Randomized tests such as Miller-Rabin make the chance of a false "prime" answer extremely small.
- Proof methods exist that give certainty, and some produce a certificate others can check.
- For real software, use a well-reviewed library and do not write your own cryptographic code.
For a small number, try dividing by primes up to its square root. For a large one, with hundreds of digits, use a test based on how powers behave in modular arithmetic. Such tests can decide the question quickly without ever finding a factor.
This guide walks through the options in order of power. You will see where each one works, where it fails and how they are combined in practice.
Start with trial division
If n is composite, it can be written as a × b with 1 < a ≤ b. Then a × a ≤ n, so a ≤ √n. A composite number therefore has a prime factor no bigger than its square root.
Test n = 397. Since √397 is just under 20, you only need the primes up to 19.
397 is odd not divisible by 2
3 + 9 + 7 = 19 not divisible by 3
does not end in 0 or 5 not divisible by 5
397 = 7 × 56 + 5
397 = 11 × 36 + 1
397 = 13 × 30 + 7
397 = 17 × 23 + 6
397 = 19 × 20 + 17
no divisor found 397 is prime Compare 221. Its square root is under 15, and 221 = 13 × 17, found at the last prime tried. For quick checks, divisibility rules handle 2, 3, 5 and 11 at a glance.
A simple speedup is to notice that every prime above 3 has the form 6k − 1 or 6k + 1, so only those need trying. To list all primes in a range at once, use the sieve of Eratosthenes.
Why trial division runs out
The number of divisors to try grows with √n, which grows exponentially with the number of digits.
| digits in n | size of √n |
|---|---|
| 6 | about a thousand |
| 12 | about a million |
| 24 | about a trillion |
| 100 | about 10⁵⁰ |
A laptop handles the first rows in moments. The last row is beyond any computer that could be built in practice. Yet numbers with hundreds of digits are routinely certified prime in a fraction of a second. The trick is to ask a different question.
The Fermat test
Fermat's little theorem says that if p is prime and a is not divisible by p, then a^(p−1) leaves remainder 1 on division by p. Turn this around. If you find some a, not sharing a factor with n, for which a^(n−1) does not leave remainder 1, then n is certainly composite.
Try n = 91 with a = 2.
2¹² = 4096 ≡ 1 (mod 91) since 4096 = 45 × 91 + 1
2⁹⁰ = (2¹²)⁷ × 2⁶ ≡ 64 (mod 91)
64 ≠ 1, so 91 is composite Now try a = 3.
3⁶ = 729 ≡ 1 (mod 91) since 729 = 8 × 91 + 1
3⁹⁰ = (3⁶)¹⁵ ≡ 1 (mod 91) Base 3 says nothing, even though 91 = 7 × 13. A composite number that passes for a base is called a pseudoprime to that base.
Fast powers by repeated squaring
The test is practical because huge powers are cheap modulo n. Square repeatedly, reduce after every step, and multiply together the pieces you need.
To compute 5¹³ modulo 23, note that 13 = 8 + 4 + 1.
5¹ ≡ 5
5² = 25 ≡ 2
5⁴ ≡ 2² = 4
5⁸ ≡ 4² = 16
5¹³ ≡ 16 × 4 × 5 = 320 ≡ 21 (mod 23) 320 − 13 × 23 = 21 An exponent with hundreds of digits needs only a couple of thousand such squarings. No intermediate number ever exceeds n².
Liars that fool every base
Some composite numbers pass the Fermat test for every base that shares no factor with them. These are Carmichael numbers, and the smallest is 561 = 3 × 11 × 17. There are infinitely many of them, a fact proved in the 1990s.
Choosing more bases therefore does not fully repair the Fermat test. The fix is to look more closely at the chain of squarings. In arithmetic modulo a prime, the only square roots of 1 are 1 and −1. If the chain reveals any other square root of 1, the number is composite.
That refinement is the Miller-Rabin test. For any odd composite number, at least three quarters of the possible bases expose it. Run it with many random bases and the chance that a composite slips through becomes tiny.
Tests that give proof
Sometimes "overwhelmingly likely" is not enough. A classical method, going back to Lucas, proves primality when you can factor n − 1.
If some a satisfies a^(n−1) ≡ 1 (mod n), but a^((n−1)/q) is not 1 for every prime q dividing n − 1, then n is prime.
Take n = 31, so n − 1 = 30 = 2 × 3 × 5, and try a = 3.
3³⁰ ≡ 1 (mod 31)
3¹⁵ ≡ 30 not 1
3¹⁰ ≡ 25 not 1
3⁶ ≡ 16 not 1
so 31 is prime The list a = 3 together with the factors 2, 3 and 5 is a certificate: anyone can verify it quickly. The weakness is that factoring n − 1 may be hard. Special forms avoid the problem, as with Mersenne numbers, which have their own very fast test.
For general numbers, elliptic curve primality proving produces certificates and handles numbers with thousands of digits. In 2002 Manindra Agrawal, Neeraj Kayal and Nitin Saxena gave the AKS test, the first method proved to be deterministic and to run in polynomial time for all inputs. It is a landmark in theory, though other methods are much faster in practice.
Four kinds of answer
It helps to be clear about what a test is entitled to say, because different methods make different promises.
Proven composite. The test found something impossible for a prime: a divisor, a failed Fermat condition or a strange square root of 1. This answer is final, and it can usually be checked in moments.
Probably prime. The number survived randomized rounds. The wording is slightly loose, since the number either is or is not prime. What is small is the chance that the procedure would let a composite number through.
Proven prime. A deterministic argument has covered every possibility, as with trial division up to the square root, a Lucas-style certificate or an elliptic curve proof.
Conditionally prime. Some fast deterministic procedures are correct only if an unproved hypothesis holds. The best known depends on a generalized form of the Riemann hypothesis. These answers are trusted in many settings but are not theorems.
Which tool for which size
For numbers you meet in schoolwork, up to a few thousand, trial division by hand is the clearest method and it explains itself. Up to around a trillion, a short computer loop over divisors still finishes quickly.
Beyond that, up to the largest integers that fit in a 64-bit machine word, programs usually run a strong test with a fixed list of bases. Exhaustive computer studies have shown that suitable fixed lists never give a wrong answer in that range, so the result is certain even though the method looks probabilistic.
For numbers with hundreds or thousands of digits, as used in cryptography, randomized strong tests are the standard tool. Certificates come into play when a mathematician wants to cite a particular number as prime.
Special shapes deserve special treatment. Before running a general test, look at whether your number is one less or one more than a highly factorable number, since that opens the door to a fast and complete proof.
Putting a strategy together
Most composite numbers are removed by the first screen, which is cheap. Many libraries then use a combination called the Baillie-PSW test, joining a strong test to base 2 with a test based on Lucas sequences. No composite number that passes it is known, although nobody has proved that none exists.
Finding a new large prime
Generating a prime reverses the problem: you pick random odd numbers of the wanted size and test until one passes. The prime number theorem says this will not take long. Near a 300-digit number, ln n is about 690, so about one odd number in 345 is prime.
A worked screening example
Suppose you want to decide about 1,003. Its square root is about 31.7, so trial division needs the primes up to 31. It is odd, its digits add to 4, so 3 does not divide it, and it does not end in 0 or 5.
Dividing by 7 gives remainder 2, by 11 remainder 2, by 13 remainder 2. Then 1,003 = 17 × 59, so the search ends. A divisor is the most convincing evidence there is: one multiplication verifies it.
Now try 1,009. The same primes, 2 through 31, all leave nonzero remainders, so 1,009 is prime. Notice the asymmetry. Showing that a number is composite can be quick, but showing primality by this method means finishing the whole list.
Verifying someone else's claim
A good habit with any large computation is to ask how it can be checked. If a program says a number is composite and supplies a factor, multiply or divide to confirm it. If it supplies a failing base, repeat the one power calculation. If it claims primality, ask for a certificate or run an independent implementation, ideally on different hardware, so that a shared bug is unlikely to fool you twice.
Common mistakes
Trusting one Fermat test. As 91 showed with base 3, a pass can be a lie.
Testing divisors only up to half of n. The square root is enough.
Confusing testing with factoring. A test may tell you a number is composite while giving no clue to its factors. Factoring is a much harder task.
Using teaching code for security. Real key generation depends on good randomness and many safeguards. Rely on maintained, reviewed software for anything that protects actual data.