Factoring as a rhythm.
+ The questionWhat does finding the period of a repeating sequence have to do with factoring?
A sequence that repeats
Start with 1, multiply by 7, and keep only the remainder after dividing by 15. Then multiply that remainder by 7 and keep the remainder again. The sequence runs 1, 7, 4, 13, 1, 7, 4, 13, and then repeats forever. Arithmetic in which only remainders are kept is called modular arithmetic, and it works like a clock: on a clock with fifteen hours, 7 × 7 = 49 lands on hour 4, because 49 is three full turns of fifteen plus four.
The terms of the sequence are the powers of 7, each reduced to its remainder, and 7 is called the base. Reduced in this way, the powers of any base must eventually repeat, because only so many remainders are available, and once one remainder repeats, the whole cycle does. The length of the cycle is called the period of the base, often written r. For the base 7 and the number 15, the period is 4. The surprising fact on which Shor's algorithm rests is that knowing the period of a base chosen at random is enough, at least half the time, to factor the number.
From a period to the factors
The reasoning takes three steps of school arithmetic, with N standing for the number to be factored. If the period r is even, the base can be raised to the power r/2, half the period. Multiplying that power by itself gives the base raised to the power r, which leaves a remainder of 1, because the sequence has come back to its start. The two neighbors of that power, one less and one more than it, multiply to its square minus 1, so their product leaves no remainder when divided by N: it is a multiple of N. Unless one of the neighbors is itself a multiple of N, each must share a factor with N. A shared factor is found quickly by Euclid's algorithm for the greatest common divisor, which needs at most a few thousand divisions even for numbers of hundreds of digits, a moment's work for a laptop.
For N = 15 and base 7, the period is 4, so r/2 = 2 and 7² = 49. The neighbors of 49 are 48 and 50. The greatest common divisor of 48 and 15 is 3, and that of 50 and 15 is 5: these are the factors, since 3 × 5 = 15. For N = 21 and base 2, the sequence runs 1, 2, 4, 8, 16, 11 and then returns to 1, a period of 6. Half the period gives 2³ = 8, whose neighbors 7 and 9 share the factors 7 and 3 with 21. A base can fail: for 15 and base 14, the period is 2, and 14¹ = 14 has neighbors 13 and 15, which give only the useless factors 1 and 15.
The arithmetic can be checked directly. The product 48 × 50 = 2,400 is 160 × 15, so the product of the two neighbors is indeed a multiple of 15, while neither neighbor is a multiple of 15 on its own. Each must therefore hold part of 15, one the 3 and the other the 5. The same holds for 7 × 9 = 63, which is 3 × 21.
The failure in the example has a simple cause. The base 14 leaves a remainder of 14 at half its period, and 14 is one less than 15, so the "one more" neighbor is 15 itself and carries no information. A base fails when its period is odd, or when its power at half the period leaves a remainder one less than the number, and the remedy is to try another base.
How often a base works
Shor proved that for a number with two distinct odd prime factors, at least half of the bases that share no factor with it work. Every RSA number has two such factors. The table shows the count for the two small examples. A base that shares a factor with the number is even better, since Euclid's algorithm finds that factor directly, but for a 617-digit number such a lucky choice essentially never happens.
| Number | Bases with no common factor | Bases that give the factors | Share that works |
|---|---|---|---|
| 15 | 8 | 6 (2, 4, 7, 8, 11, 13) | 75 % |
| 21 | 12 | 6 (2, 8, 10, 11, 13, 19) | 50 % |
The number 21 meets the bound exactly, which shows that the bound cannot be improved in general. In practice, a run that picks a base at random succeeds about half the time or better, and two or three tries make failure unlikely. These repeated runs add to the cost, as the shots of Chapter 2 do. Large designs also plan for several runs, though for a different reason. In a 2025 preprint, Craig Gidney of Google Quantum AI set out a design for RSA-2048 that later chapters follow; it needs about nine runs for every factorization, because its classical post-processing combines their results.
Only one step of the whole procedure needs a quantum computer. An ordinary computer picks the base and checks it for a common factor. Once a period comes back, the ordinary computer tests whether it is even, computes the two neighbors, runs Euclid's algorithm and multiplies the factors it finds to confirm them. The quantum computer is asked a single question, the period of one base, and its answer can be checked in a moment. Every quantum algorithm in this guide has the same shape: a classical program that calls a quantum computer for the one step it cannot do itself.
Why the period is hard to find
Knowing that the period gives the factors does not help an ordinary computer, which knows no fast way to find the period. For a 617-digit number, the period of a typical base can itself run to hundreds of digits, so stepping through the sequence until it repeats would take far longer than the age of the universe. Computing one term far out in the sequence is easy: squaring repeatedly gives the 1,024th power in ten steps. But no known ordinary method can find where the sequence starts over from such scattered terms without work comparable to factoring the number in the first place.
A quantum computer can compute the terms for every exponent at once: it spreads a register over all the exponents and uses the reversible arithmetic of Chapter 4 to leave each exponent's arrow paired with its remainder. This step is called modular exponentiation. It holds most of the gates of Shor's algorithm and nearly all its Toffoli gates, and it is the part that the authors of every resource estimate for RSA-2048 work hardest to shrink. The register then holds a pattern that repeats with the period. Reading the register directly would return one exponent and its remainder, chosen at random, which reveals nothing about the period. Chapter 8 shows the step that makes the repeating pattern visible.
The same machinery solves the problem Shor solved first. In the discrete logarithm problem, a base and a remainder are given, and the task is to find the exponent that produces that remainder. One version of the problem replaces the multiplication of remainders with a rule for adding points on a particular kind of curve, called an elliptic curve. That elliptic-curve version secures much of the key exchange on today's internet, the step in which two computers agree on a secret key. Finding the exponent also comes down to a hidden period, and Shor's method finds it in the same way. Chapter 22 shows that, by a 2026 preprint from a Google-led team, breaking elliptic-curve keys needs far fewer Toffoli gates than factoring RSA-2048.
Where the idea came from
The idea of finding a hidden period with a quantum computer did not begin with Shor. Earlier work had already posed problems, built for the purpose, on which a quantum computer could outperform an ordinary one: David Deutsch's 1985 problem, met in Chapter 5, and a 1993 paper by Ethan Bernstein and Umesh Vazirani. In 1994 Daniel Simon described the first problem in which a quantum computer finds a hidden pattern exponentially faster than any ordinary method given the same access to an oracle of the kind met in Chapter 6, meaning that both may only call the function that hides the pattern and see its answers. The program committee of one major conference rejected the result before it appeared at another.
Shor sat on the committee that rejected Simon's result. He recognized that Simon's method was finding a kind of period, and that periods are the key to the discrete logarithm problem. In his 1997 journal paper he wrote that "Simon's algorithm inspired the work presented in this paper." Shor solved the discrete logarithm first, and factoring followed in the week described at the opening of this guide.
Factoring is not a search through possible factors, and Shor's algorithm does not try divisors in parallel. It converts factoring into a question about a repeating pattern and answers that question with interference. Problems with no such pattern get at most the square-root speedup of Chapter 6, which is why the list of problems with large quantum speedups is short.
When a problem is proposed for a quantum computer, ask what structure the algorithm exploits: a period, a symmetry, or the physics of a quantum system. If the answer is none, expect at most a square-root speedup and apply the break-even test of Chapter 6. If the answer is a period or a symmetry, ask which known algorithm uses it and what its arithmetic costs, because that arithmetic dominates the gate count.
- The remainders of the powers of a base repeat, and the length of the cycle is the base's period: 4 for the base 7 and the number 15.
- If the period is even, the neighbors of the base raised to half the period share factors with the number, so 48 and 50 reveal 3 and 5 for 15, and 7 and 9 reveal 7 and 3 for 21.
- For any number with two distinct odd prime factors, at least half of the bases that share no factor with it work, and 21 meets that bound exactly.
- Ordinary computers know no fast way to find the period of a large number; a quantum computer can compute every term at once, and the next chapter shows how interference reads the period.
Let's Talk