Shor’s algorithm worked by hand: factoring 15

A complete run for N = 15, a = 7: powers, period, quantum outcome, gcd and the factors 3 and 5.

9 min read · reviewed December 2026

Choose a

N = 15. Take a = 7. gcd(7, 15) = 1, so there is no lucky shortcut.

The sequence

7^0 = 1, 7^1 = 7, 7^2 = 49 ≡ 4, 7^3 = 28 ≡ 13, 7^4 = 91 ≡ 1. The period is r = 4. A computer can see it at once here; a quantum computer is only needed for huge N.

Powers a^x mod 15 for x = 0 … 8. Each row repeats with its own period.
ax=0x=1x=2x=3x=4x=5x=6x=7x=8r
21248124814
7174131741314
1111111111111112
1411411411411412

The quantum step

With 8 counting qubits Q = 256. Since 4 divides 256, the Fourier transform gives only the outcomes 0, 64, 128 and 192, each with probability 25 percent.

N = 15, a = 7, r = 4, t = 8 counting qubits (Q = 256). The only outcomes with non-zero probability.
yy/QProbabilityRecovered r
00.000025.0%none (y = 0)
620.24220.0%4
640.250025.0%4
1260.49220.0%4
1280.500025.0%4
1920.750025.0%4

Reading the outcome

  • y = 0: no information. Measure again. (25 percent of shots.)
  • y = 64: 64/256 = 1/4, denominator 4, and 7^4 ≡ 1, so r = 4.
  • y = 192: 192/256 = 3/4, denominator 4, so r = 4.
  • y = 128: 128/256 = 1/2, denominator 2. 7^2 = 4, not 1, but twice 2 is 4 and 7^4 ≡ 1, so r = 4.

So one shot succeeds with probability 75 percent. Repeating is cheap.

From r to the factors

r = 4 is even. 7^(4/2) = 7² = 49 ≡ 4 (mod 15), which is not −1 (that would be 14). So gcd(4 − 1, 15) = gcd(3, 15) = 3 and gcd(4 + 1, 15) = gcd(5, 15) = 5. Therefore 15 = 3 × 5.

gcd(7² − 1, 15) = gcd(48, 15) = 3

gcd(7² + 1, 15) = gcd(50, 15) = 5

A note on real experiments

Factoring 15 on quantum hardware has been reported since 2001 in NMR and later in other platforms. Be careful with such demonstrations: for N = 15 the circuit can be simplified using prior knowledge of the answer, and the demonstrations do not show the scaling that matters. Check the original papers for what exactly was implemented.

Try it

Questions

Why is 15 used so often?

It is the smallest number that is odd, not prime and not a prime power, so it is the smallest meaningful case for Shor’s algorithm.

Is 15 hard for a classical computer?

No. A child can factor it. The demonstration shows only that the quantum steps can be carried out, not any advantage.

What if I choose a = 14 for N = 15?

Then a ≡ −1 mod 15, the period is 2 and a^(r/2) = 14 ≡ −1, so the run fails. The Which a works? tab shows this.

Read next

Sources and further reading

Check the original sources for exact citations.