What Shor’s algorithm does, in plain steps

Most of Shor’s algorithm is ordinary arithmetic. One step needs a quantum computer. Here is the split.

8 min read · reviewed December 2026

The goal

Given a large number N that is the product of two primes, find those primes. Classical computers have no known method that stays fast as N grows, and the security of RSA rests on that. Shor’s algorithm (1994, published in full in 1997) is a method that a large enough quantum computer could run in a number of steps that grows polynomially with the number of digits of N.

The trick is that it does not attack factoring head on. It turns factoring into a different problem, finding the period of a repeating sequence, and the quantum computer solves that problem.

The five steps

  • Pick a number a between 2 and N−1 at random.
  • Compute gcd(a, N). If it is more than 1, you already have a factor and you are done. This is rare for large N.
  • Find the period r of the sequence a^x mod N, meaning the smallest r > 0 with a^r ≡ 1 (mod N). This is the quantum step.
  • If r is even and a^(r/2) is not ≡ −1 (mod N), compute gcd(a^(r/2) − 1, N) and gcd(a^(r/2) + 1, N). These are factors of N.
  • If r is odd or a^(r/2) ≡ −1, pick another a and try again.

Only step 3 is quantum. Steps 1, 2, 4 and 5 run on an ordinary computer, and the Factor tab of the lab shows each of them.

Why the period gives factors

If a^r ≡ 1 (mod N) and r = 2k is even, then (a^k − 1)(a^k + 1) is a multiple of N. N does not divide either bracket on its own (that is what the two failure cases rule out), so the prime factors of N are split between the two brackets, and gcd pulls them apart.

a^r ≡ 1 (mod N)

(a^(r/2) − 1)(a^(r/2) + 1) ≡ 0 (mod N)

Where the speed-up lives

Finding r by brute force means computing a^1, a^2, … until you reach 1. For a 2048-bit N that is astronomically many steps. A quantum computer prepares a superposition over many exponents x at once, and the quantum Fourier transform turns the hidden repetition into sharp measurement peaks. The next guides show how.

Try it

Questions

Does Shor’s algorithm try every factor at once?

No. It finds the period of a^x mod N, and the factors come from that period by ordinary arithmetic. Interference, not parallel trial, produces the answer.

Is the algorithm certain to work?

No. Each run succeeds with a fixed chance, and a failed run is repeated. The Success guide shows the numbers for small N.

Why must N be odd and not a prime power?

Even numbers are trivial to factor by hand, and prime powers can be handled by a classical root-finding step before Shor starts. The lab only offers N that are odd and not prime powers.

Read next

Sources and further reading

Check the original sources for exact citations.