Shor’s algorithm (1994) factors an integer in time polynomial in , where the best known classical algorithms need super-polynomial time. It does this by turning factoring into the problem of finding the period of , and a quantum computer finds periods efficiently with phase estimation. It is the reason quantum computers matter for cryptography, and it is also the clearest example of a speedup that comes from structure and not from brute-force search.

Shor turns 'find the factors' into 'find how often a pattern repeats', which quantum computers are good at. Image: JavaFXpert, CC0, via Wikimedia Commons.
Mental Model
- Factoring is reduced, classically, to order finding: the smallest with . Only the order-finding step is quantum.
- The quantum step is phase estimation on the unitary , whose eigenphases are .
- A measurement gives a number close to for a random . Continued fractions recover (or a divisor of it) from it.
- The gate cost is dominated by controlled modular exponentiation, not by the QFT.
- The algorithm is probabilistic, and several steps (choice of , even order, shared factors in and ) can fail and need a retry.
1. From Factoring to Order Finding
Pick with and (if the gcd is not 1, you already have a factor). Let be the order of modulo . If is even,
So divides the product without dividing either factor, provided . Then are nontrivial factors. For odd that is not a prime power, a random meets these conditions (even , ) with probability at least , so a few attempts suffice.
Worked example: N = 15, a = 7
The powers of 7 modulo 15 are , so the order is (checked by running
pow(7, k, 15)). It is even, and . Then and . Both are nontrivial, and .
2. The Quantum Step
Define on qubits (and identity on values , to keep it unitary). Its eigenvectors are
for . You cannot prepare without knowing , but you do not need to. The sum , so running phase estimation on the easy input gives the same result as picking a random and estimating .
With counting qubits, take so that . The measured integer then satisfies with good probability, and since two distinct fractions with denominators below differ by at least , the fraction is the unique one in that window.
3. Continued Fractions on N = 15
Take , so . The phases are , which are exact multiples of , so the four possible outcomes are , each with probability (phase estimation of is dyadic). Post-processing each one with Python’s Fraction.limit_denominator (which is a continued-fraction expansion):
from fractions import Fraction
from math import gcd
def factor_from_phase(y, t, a, N):
r = Fraction(y, 2**t).limit_denominator(N).denominator
if r % 2 or pow(a, r, N) != 1:
return r, None
x = pow(a, r // 2, N)
if x == N - 1:
return r, None
return r, (gcd(x - 1, N), gcd(x + 1, N))
for y in (0, 64, 128, 192):
print(y, factor_from_phase(y, 8, 7, 15))Running this prints (1, None) for , (4, (3, 5)) for and , and (2, None) for . The cases and share a factor with , so they give and , which are only divisors of the true order. This single run succeeds with probability here. In practice you rerun, or combine two results with a least common multiple.
For a case where the fractions are not exact I also computed the distribution for , , which has order , with (). The outcome distribution has two sharp peaks at and (probability each, and ) and four peaks at (about 0.114 each, plus smaller neighbours at 86, 170 and so on). Continued fractions turn into , which gives , while gives (a divisor of 6).
4. Why Modular Exponentiation Dominates
The counting register needs controlled- for . Since , each power is a controlled modular multiplication by a classically precomputed constant. That is about controlled multiplications for an -bit modulus, and each multiplication is a sequence of modular additions. With schoolbook arithmetic the total gate count is , and with asymptotically faster multiplication it drops to roughly times polylogarithmic factors. The QFT, by contrast, costs gates (or fewer, with the approximate QFT). Space-saving constructions, such as Beauregard’s, need only about qubits. In real resource estimates the arithmetic, including the non-Clifford Toffoli or T gates it needs, is what drives the cost.
5. Resource Estimates, Stated Carefully
Numbers for breaking RSA-2048 depend on the assumptions about error rate, code and cycle time, and they have dropped over the years because of better algorithms. As reported by Gidney and Ekerå (2019), factoring a 2048-bit RSA integer would take roughly 20 million noisy physical qubits for about 8 hours, assuming surface-code error correction and a physical error rate near . Gidney (2025) reported a further reduction to under one million noisy qubits at the cost of running for under a week. Those are estimates, not demonstrations. The largest actual hardware factoring demonstrations are tiny (such as 15 and 21), and many of them used circuits simplified using knowledge of the answer, as Smolin, Smith and Vargo pointed out in 2013. No quantum computer today can factor a number that a laptop cannot.
6. RSA Implications Without Hype
Shor’s algorithm breaks RSA, finite-field Diffie-Hellman, and elliptic-curve cryptography, because all three rest on factoring or discrete logarithms. It does not break symmetric ciphers or hash functions; Grover’s search (see Grover’s Search) only gives a quadratic speedup against those, which is why guidance is to use 256-bit symmetric keys. The practical concern is “harvest now, decrypt later”: data encrypted today with RSA may be recorded and decrypted once a large enough machine exists. NIST published its first post-quantum standards (FIPS 203, 204 and 205) in August 2024, and migration is mostly an engineering and inventory problem. When such a machine will exist is unknown; the estimates above only say what it would need.
Related
- Quantum Fourier Transform and Phase Estimation: the subroutine that does the work.
- Grover’s Search: the other famous speedup, quadratic and structure-free.
- Quantum Error Correction: why the physical qubit counts above are in the millions.
- Qubit Hardware Platforms: how far today’s devices are from those numbers.
- Deutsch-Jozsa and Bernstein-Vazirani: the hidden-structure idea in its simplest form.
- QGSS Lab 4a: Shor as the classic case of a separation that rests on assumptions about classical algorithms.