Why Integer Factoring Is So Much Harder Than Primality Testing
Understand how you can know a number is composite without knowing its factors, and why that gap in difficulty matters for security.
Key takeaways
- Primality can be decided in polynomial time, as the AKS algorithm proved in 2002.
- Fast primality tests detect compositeness through broken rules, which usually reveal no factor.
- The best known classical factoring methods for large general numbers take time that grows much faster than polynomially.
- Nobody has proved that factoring is hard; it is an assumption backed by long experience.
- A large enough, reliable quantum computer running Shor's algorithm could factor efficiently.
You can find out that a large number is composite without learning a single one of its factors. Testing primality only asks whether a number behaves the way primes must behave. Factoring asks you to produce the hidden pieces, and no known classical method does that quickly for large numbers.
This difference is one of the most striking in computational mathematics. It also underpins a large part of digital security.
Two questions that sound alike
Question one: is 8,051 prime? Question two: if not, what are its factors? The second answer includes the first, so factoring is at least as hard as testing. The surprise is how much harder it appears to be.
A multiplication shows the imbalance. Computing 83 × 97 takes a few seconds by hand. Starting from 8,051 and recovering 83 and 97 takes real searching. With primes of several hundred digits, the multiplication is still instant on a computer, while the search is out of reach for known classical methods.
How a test can say composite without a factor
Fermat's little theorem says that for a prime p and a not divisible by p, a^(p−1) leaves remainder 1. Take n = 91 and a = 2: the power 2⁹⁰ leaves remainder 64. A prime could not do that, so 91 is composite.
Nowhere in that argument did 7 or 13 appear. It is like proving that a locked box is not empty by weighing it. You learn something solid about the contents without seeing them.
The stronger Miller-Rabin test works the same way. Occasionally its calculation stumbles on a square root of 1 that yields a factor, but for a large product of two big primes that is very unlikely with a random base.
Measuring the difficulty
Computer scientists measure running time against the length of the input, meaning the number of digits d, not the size of n. An algorithm runs in polynomial time if the work is bounded by some power of d.
Trial division tries divisors up to √n, which is about 10 raised to d/2. Every two extra digits multiply the work by about 10. That is exponential growth in d. The contrast is explained in polynomial versus exponential time.
| task | how work grows with the digit count | practical reach |
|---|---|---|
| multiply two numbers | polynomial, very mild | millions of digits |
| test primality | polynomial | thousands of digits routinely |
| factor by trial division | exponential | a couple of dozen digits |
| factor by best classical methods | between polynomial and exponential | a few hundred digits at great cost |
Better ways to factor
Trial division is not the end of the story. Several clever methods exploit structure.
Fermat's difference of squares
If you can write n as x² − y², then n = (x − y)(x + y). Try x just above √n and see whether x² − n is a square. Take n = 5,959, whose square root is about 77.2.
78² − 5959 = 125 not a square
79² − 5959 = 282 not a square
80² − 5959 = 441 = 21²
5959 = (80 − 21)(80 + 21) = 59 × 101 This is fast when the two factors are close together, and very slow when they are far apart.
Pollard's rho method
John Pollard's idea from the 1970s is to generate a pseudorandom sequence modulo n and look for two terms that agree modulo an unknown factor. A greatest common divisor then exposes it.
Take n = 8,051 and the rule x → x² + 1, starting at 2. One value advances a single step each time, the other two steps.
slow fast gcd(difference, 8051)
5 26 1
26 7474 1
677 871 97 So 8,051 = 97 × 83. The expected work grows roughly with the square root of the smallest factor, a big gain over trial division but still exponential in the digit count. The gcd step uses the Euclidean algorithm.
Sieve methods
The strongest general methods combine many congruences to build an equation x² ≡ y² modulo n, then apply the difference-of-squares idea. The quadratic sieve and its successor, the general number field sieve, work this way.
The number field sieve is subexponential: faster than any exponential method in the long run, yet slower than any polynomial one. In 2020 a team used it to factor a challenge number known as RSA-250, with 250 decimal digits, using a very large amount of computing time. Records advance, so check recent sources.
Pollard's p − 1 method
A third idea succeeds when one prime factor p has the property that p − 1 is made entirely of small primes. Raise 2 to a huge exponent M that is a product of many small prime powers. If p − 1 divides M, Fermat's theorem forces 2ᴹ to leave remainder 1 modulo p, and gcd(2ᴹ − 1, n) reveals p.
Take n = 299, which is 13 × 23. Since 13 − 1 = 12 = 4 × 3, the exponent 12 is enough.
2¹² = 4096 ≡ 209 (mod 299)
gcd(208, 299) = 13 Of course, you do not know p in advance. You choose M to cover all small primes and hope. Lenstra's elliptic curve method, from the 1980s, generalizes the idea and gets many fresh chances by changing curves. It is the usual choice for pulling medium-sized factors out of very large numbers.
A strategy for an unknown number
Faced with a number to factor, a computer algebra system typically works in stages, from cheap to expensive.
- Divide out all small primes.
- Check whether what remains is a perfect power.
- Run a primality test; if the remainder is probably prime, there is nothing left to split.
- Try Pollard-style and elliptic curve methods for factors of modest size.
- Fall back on a sieve method for the hard core.
Step 3 shows the partnership between the two problems. Testing tells the program when to stop, and when it is worth continuing. Unique factorization guarantees that whatever order the factors are found in, the final list is the same.
Why the gap protects secrets
RSA encryption exploits the imbalance. Creating a key needs two large primes, which testing makes easy to find. The public key includes their product. Recovering the secret key is straightforward for anyone who can factor that product.
Real systems choose key sizes far beyond published factoring records and add other safeguards, such as padding. Standards bodies like the US National Institute of Standards and Technology publish current recommendations. Textbook-sized examples are not secure.
Is factoring provably hard?
No. It is possible, as far as anyone has proved, that a fast classical algorithm exists and has not been found. Factoring sits in an interesting place: a proposed factorization is easy to check, which puts the problem in the class NP.
It is not believed to be NP-complete, the class of hardest problems in NP. One reason is that both "yes" and "no" answers to suitable factoring questions have short certificates, and a problem like that being NP-complete would have consequences most theorists think false.
So factoring could be easier than the hardest search problems and still far harder than testing. Any proof that it requires more than polynomial time would also resolve the P versus NP question.
What quantum computers change
In 1994 Peter Shor found a quantum algorithm that factors integers in polynomial time. It turns factoring into the problem of finding the period of the powers of a number modulo n, which quantum interference can extract.
Experiments so far, as of the mid-2020s, have been far from the scale needed to threaten real keys, because that requires many high-quality, error-corrected quantum bits. Timelines are debated. Governments and standards groups are nevertheless moving to replacements, described in how Shor's algorithm threatens factoring-based codes.
Not every large number is hard
Difficulty depends on the number, not only on its length. A random large integer usually has some small factors that are found at once. A number built as the square of a prime is trivial, since you can take a square root.
The hard cases are products of two large primes of similar length, chosen at random and not too close together. Poor choices have caused real weaknesses: if two different keys accidentally share a prime, a single gcd computation breaks both, without any factoring effort at all.
Common misconceptions
"If we can test primality fast, factoring should follow." The tests answer a yes-or-no question through properties of the whole number, which leak almost nothing about the parts.
"Computers just need to get faster." A faster machine adds only a few digits to what an exponential search can handle. Adding digits to a key costs the defender very little and the attacker a great deal.
"A list of all primes would solve it." There are vastly more primes of a few hundred digits than there are atoms available to write them on, as the prime number theorem makes clear. Any such list is physically impossible.