Shor’s Algorithm Lab
Factor a small number the way Shor’s algorithm does. Pick N and a guess a, see the sequence a^x mod N repeat, look at the exact measurement distribution of the quantum step, watch continued fractions recover the period, count which guesses work, and break a toy RSA key.
Reviewed December 2026. New to this? Start on the Factor tab with N = 15, a = 7.
Step 1: is a already lucky?
gcd(7, 15) = 1
Step 2: the powers of a, mod N
The sequence repeats with period r = 4: a^r ≡ 1 (mod N). Finding r is the only step a quantum computer helps with.
Step 3: turn r into factors
a^(r/2) = 4. gcd(4 − 1, 15) = 3 and gcd(4 + 1, 15) = 5. So 15 = 3 × 5.
The algebra
a^r ≡ 1 (mod N)
(a^(r/2) − 1)(a^(r/2) + 1) ≡ 0 (mod N)
If N divides neither bracket alone, the two brackets share the factors of N between them, and gcd finds them. This part is ordinary arithmetic. Try a few values of a: some work, some hit the odd-period or −1 traps.
A five-minute path
- Factor tab, N = 15, a = 7: see the sequence 1, 7, 4, 13, 1 … and the period 4. The factors 3 and 5 appear.
- Change a to 14. The run fails because a^(r/2) ≡ −1. Try “Pick a working a” and “Pick a failing a”.
- Period finding tab: look at the four equal peaks. Press Measure once a few times and read the convergents.
- Switch to N = 21. The peaks no longer sit on whole numbers, so they spread. Compare the measured y values.
- Which a works? tab: count the failures for N = 77, then click any a to inspect it.
- Toy RSA tab: choose primes, encrypt a message, and decrypt it as an attacker who has factored N.
What the lab does and does not do
- It computes the exact output distribution of the quantum period-finding step, with an ideal noiseless machine.
- It does not build the modular-exponentiation circuit gate by gate. That circuit is much larger than any browser simulator handles.
- The post-processing is the standard one: continued fractions, then testing d, 2d, up to 8d.
- It does not simulate noise or error correction.
Reference tables
Computed by the same code as the lab.
| 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 |
| a | Period r | Outcome | Factors |
|---|---|---|---|
| 2 | 4 | works | 3 × 5 |
| 3 | none | lucky gcd | 3 × 5 |
| 4 | 2 | works | 3 × 5 |
| 5 | none | lucky gcd | 5 × 3 |
| 6 | none | lucky gcd | 3 × 5 |
| 7 | 4 | works | 3 × 5 |
| 8 | 4 | works | 3 × 5 |
| 9 | none | lucky gcd | 3 × 5 |
| 10 | none | lucky gcd | 5 × 3 |
| 11 | 2 | works | 5 × 3 |
| 12 | none | lucky gcd | 3 × 5 |
| 13 | 4 | works | 3 × 5 |
| 14 | 2 | a^(r/2) ≡ −1 | none |
| N | Lucky gcd | Odd period | a^(r/2) ≡ −1 | Works | Share of coprime a |
|---|---|---|---|---|---|
| 15 | 6 | 0 | 1 | 6 | 85.7% |
| 21 | 8 | 2 | 3 | 6 | 54.5% |
| 33 | 12 | 4 | 5 | 10 | 52.6% |
| 35 | 10 | 2 | 3 | 18 | 78.3% |
| 77 | 16 | 14 | 15 | 30 | 50.8% |
| 91 | 18 | 8 | 9 | 54 | 76.1% |
| 143 | 22 | 14 | 15 | 90 | 75.6% |
Eight guides
What Shor’s algorithm does, in plain steps
8 min read
Most of Shor’s algorithm is ordinary arithmetic. One step needs a quantum computer. Here is the split.
Order finding: periods of a^x mod N
7 min read
The sequence a^x mod N always repeats. Its period is the number Shor’s algorithm needs.
Why the quantum Fourier transform reveals the period
10 min read
How a superposition and a Fourier transform turn a hidden period r into peaks at multiples of Q/r.
Continued fractions: from a measurement to the period
8 min read
The measurement gives y, near a multiple of Q/r. Continued fractions turn y/Q into r.
When Shor’s algorithm fails, and how often
7 min read
Odd periods, −1 traps and unlucky measurements. Exact counts for small N, and why repeating works.
Shor’s algorithm worked by hand: factoring 15
9 min read
A complete run for N = 15, a = 7: powers, period, quantum outcome, gcd and the factors 3 and 5.
Shor’s algorithm and RSA: why factoring matters
8 min read
How a public RSA key hides two primes, and how learning them gives away the private key.
How big a machine does Shor’s algorithm need?
9 min read
From the lab’s 8-bit numbers to RSA-2048: the published estimates, and why they are so large.
Predict, then check
N = 21, a = 4. What is the period, and do you get factors?
r = 3. The period r = 3 is odd, so a^(r/2) does not make sense. Try another a.
N = 15, a = 14. Predict the outcome before you open the Factor tab.
a^(r/2) = 14 ≡ −1 (mod 15), so both gcds are trivial. Try another a. It always fails, because 14 ≡ −1.
For N = 15, a = 7 and 8 counting qubits, what is the chance one shot recovers the period?
75.0%. The outcome y = 0 (25%) is useless, and the other three outcomes all work.
For N = 21, how many of the coprime values of a give factors?
6 of 11 (54.5%).
N = 21, a = 2: one shot returns y = 171 with Q = 512. What period does the post-processing find?
r = 6. The fraction 171/512 is close to 1/3, so the first denominator is 3, and 2³ ≠ 1, but 2⁶ ≡ 1 (mod 21). The chance one shot gives the period is 83.1%.
Related on QuantumAtlas
Frequently asked questions
What does Shor’s algorithm do?
It factors an integer N into primes by finding the period of the sequence a^x mod N. Only that period-finding step needs a quantum computer. The rest is ordinary arithmetic.
Does this lab run on a quantum computer?
No. It simulates the quantum step exactly on a classical computer, which is possible only for small N (here up to 143).
Is the period-finding distribution exact or random?
Exact. The probability of each outcome is computed from the formula and checked against a direct Fourier-transform calculation. Only the “Measure once” and “Run 100 shots” buttons use a seeded random generator.
Why does the lab only offer odd numbers that are not prime powers?
Even numbers and prime powers can be factored classically before Shor’s algorithm starts, so they are not interesting cases.
Why do some choices of a fail?
If the period r is odd, or a^(r/2) ≡ −1 (mod N), both gcds are trivial. The Which a works? tab counts them.
How many counting qubits are used?
By default t = ⌈log₂ N²⌉, the textbook choice, capped at 11 in the browser. The slider adds extra qubits.
Can this lab break real encryption?
No. Real keys are thousands of bits long. The Real scale tab shows the published resource estimates for that size.
Are the RSA resource numbers certain?
No. They are published estimates under stated assumptions about error rates and hardware speed, and they have changed a lot over time.
Has Shor’s algorithm been run on real hardware?
Small demonstrations such as factoring 15 have been reported. They do not show the scaling that matters. Check the original papers for what was implemented.
Where can I build the circuit gate by gate?
The Circuit Builder includes the quantum Fourier transform. A full modular-exponentiation circuit is too large for its 5-qubit limit.
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). The original paper.
- M. Nielsen and I. Chuang, Quantum Computation and Quantum Information, chapter 5. The standard textbook account of the quantum Fourier transform, phase estimation, order finding and continued fractions.
- C. Gidney and M. Ekerå (2021), “How to factor 2048 bit RSA integers in 8 hours using 20 million noisy qubits”. The 2019 resource estimate quoted in the tables.
- C. Gidney (2025), “How to factor 2048 bit RSA integers with less than a million noisy qubits”. The 2025 estimate quoted in the tables.
- Algorithm page: Shor’s algorithm. A shorter overview on this site.
References were written from memory of the original works. Check the originals for exact citations.