When Shor’s algorithm fails, and how often
Odd periods, −1 traps and unlucky measurements. Exact counts for small N, and why repeating works.
7 min read · reviewed December 2026
Two classical failures
A standard result says that for odd N with at least two different prime factors, a random coprime a avoids both traps with probability at least one half. The table counts every case for several N and agrees.
- The order r is odd. Then r/2 is not an integer and the algebra stops.
- a^(r/2) ≡ −1 (mod N). Then a^(r/2) + 1 is a multiple of N, and gcd(a^(r/2) − 1, N) is 1, so both gcds are trivial (N and 1).
| N | Lucky gcd | Odd period | a^(r/2) ≡ −1 | Works | Share of coprime a |
|---|---|---|---|---|---|
| 15 | 6 | 0 | 1 | 6 | 85.7% |
| 21 | 8 | 2 | 3 | 6 | 54.5% |
| 33 | 12 | 4 | 5 | 10 | 52.6% |
| 35 | 10 | 2 | 3 | 18 | 78.3% |
| 77 | 16 | 14 | 15 | 30 | 50.8% |
| 91 | 18 | 8 | 9 | 54 | 76.1% |
| 143 | 22 | 14 | 15 | 90 | 75.6% |
One quantum failure
Separately, a single quantum run can return a y whose convergents do not reveal r. The table shows the exact chance that the period is recovered, and that factors follow, for several choices of a. Where the period is recovered but factors are not, the cause is one of the two classical failures, not the quantum step.
| N | a | r | t | Period recovered | Factors found |
|---|---|---|---|---|---|
| 15 | 7 | 4 | 8 | 75.0% | 75.0% |
| 21 | 2 | 6 | 9 | 83.1% | 83.1% |
| 21 | 5 | 6 | 9 | 83.1% | 0.0% |
| 35 | 2 | 12 | 11 | 91.3% | 91.3% |
| 143 | 2 | 60 | 15 | 79.5% | 79.5% |
| 143 | 3 | 15 | 15 | 93.3% | 0.0% |
More counting qubits help a little
Extra counting qubits sharpen the peaks, but with this post-processing the single-shot chance barely moves, as the exact numbers show.
| t | Q = 2^t | Period recovered |
|---|---|---|
| 5 | 32 | 80.1% |
| 6 | 64 | 80.9% |
| 7 | 128 | 82.2% |
| 8 | 256 | 82.8% |
| 9 | 512 | 83.1% |
| 10 | 1024 | 83.2% |
| 11 | 2048 | 83.3% |
Repetition fixes everything
Each run is independent. If one run succeeds with probability p, the chance that all of n runs fail is (1 − p)^n. With p around one half, ten runs fail together about once in a thousand. The cost is a small constant factor, not an exponential.
Try it
Questions
Does failure mean the machine is faulty?
No. These are failures of the mathematics for a particular a or a particular measurement, not hardware errors. Hardware noise is a separate problem.
Why is a = N−1 always bad?
Because N−1 ≡ −1 (mod N), so its order is 2 and a^(r/2) = −1. It always hits the trap.
Is the classical part ever the bottleneck?
No. gcd, modular powers and continued fractions run in polynomial time on an ordinary computer.
Read next
Sources and further reading
- P. W. Shor, “Polynomial-time algorithms for prime factorization and discrete logarithms on a quantum computer” (SIAM J. Comput. 1997, arXiv:quant-ph/9508027)
- M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, chapter 5
- C. Gidney and M. Ekerå (2021), “How to factor 2048 bit RSA integers in 8 hours using 20 million noisy qubits”
- C. Gidney (2025), “How to factor 2048 bit RSA integers with less than a million noisy qubits”
- Algorithm page: Shor’s algorithm
Check the original sources for exact citations.