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.
| a | x=0 | x=1 | x=2 | x=3 | x=4 | x=5 | x=6 | x=7 | x=8 | r |
|---|---|---|---|---|---|---|---|---|---|---|
| 2 | 1 | 2 | 4 | 8 | 1 | 2 | 4 | 8 | 1 | 4 |
| 7 | 1 | 7 | 4 | 13 | 1 | 7 | 4 | 13 | 1 | 4 |
| 11 | 1 | 11 | 1 | 11 | 1 | 11 | 1 | 11 | 1 | 2 |
| 14 | 1 | 14 | 1 | 14 | 1 | 14 | 1 | 14 | 1 | 2 |
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.
| y | y/Q | Probability | Recovered r |
|---|---|---|---|
| 0 | 0.0000 | 25.0% | none (y = 0) |
| 62 | 0.2422 | 0.0% | 4 |
| 64 | 0.2500 | 25.0% | 4 |
| 126 | 0.4922 | 0.0% | 4 |
| 128 | 0.5000 | 25.0% | 4 |
| 192 | 0.7500 | 25.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
- 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.