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).
Counting every a from 2 to N−1. “Works” means a coprime to N whose period is even and with a^(r/2) not ≡ −1.
NLucky gcdOdd perioda^(r/2) ≡ −1WorksShare of coprime a
15601685.7%
21823654.5%
3312451052.6%
3510231878.3%
771614153050.8%
9118895476.1%
1432214159075.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.

Exact chance that one run of the quantum step (with t = ⌈log₂ N²⌉ counting qubits) recovers the period, and that it then yields factors.
NartPeriod recoveredFactors found
1574875.0%75.0%
2126983.1%83.1%
2156983.1%0.0%
352121191.3%91.3%
1432601579.5%79.5%
1433151593.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.

N = 21, a = 2. More counting qubits sharpen the peaks, but the single-shot chance moves only a little.
tQ = 2^tPeriod recovered
53280.1%
66480.9%
712882.2%
825682.8%
951283.1%
10102483.2%
11204883.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

Check the original sources for exact citations.