Continued fractions: from a measurement to the period
The measurement gives y, near a multiple of Q/r. Continued fractions turn y/Q into r.
8 min read · reviewed December 2026
What the measurement gives
After the Fourier transform you read an integer y. It is close to k·Q/r for some unknown k between 0 and r−1. So y/Q is close to the fraction k/r. You know y and Q. You want r, the denominator.
Best rational approximations
The continued-fraction expansion of y/Q yields a list of fractions called convergents, each a better approximation than the last. A standard theorem says that if y/Q is within 1/(2r²) of k/r, then k/r (in lowest terms) appears among the convergents. Choosing Q at least N² guarantees that closeness, since r is below N.
The code lists the convergents with denominators up to N. For each denominator d it tests whether a^d ≡ 1 (mod N). The first d that passes, or a small multiple of it, is the period.
N = 21, a = 2
The five strong outcomes, with their convergents. Note y = 171: it gives k/r = 1/3 in lowest terms, so the denominator is 3 and not 6. Testing 2^3 = 8 fails, but twice 3 gives 2^6 = 64 ≡ 1 (mod 21), so r = 6 is recovered by trying multiples of d.
| y | Probability | Convergents of y/512 | r found |
|---|---|---|---|
| 0 | 16.7% | none | none |
| 85 | 11.4% | 0/1, 1/6 | 6 |
| 171 | 11.4% | 0/1, 1/2, 1/3 | 6 |
| 256 | 16.7% | 0/1, 1/2 | 6 |
| 341 | 11.4% | 0/1, 1/1, 1/2, 2/3 | 6 |
| 427 | 11.4% | 0/1, 1/1, 5/6 | 6 |
Failure cases
- y = 0 carries no information: its fraction is 0/1.
- If k and r share a factor, the denominator of the reduced fraction is smaller than r. Testing small multiples repairs most such cases.
- If y lands far from any multiple of Q/r, no convergent works and the shot is repeated.
Try it
Questions
What is a convergent?
One of the fractions you get by cutting a continued-fraction expansion short. Each one is the best approximation of its size to the target number.
Why try multiples of the denominator?
Because k/r may reduce to a smaller denominator when k and r share a factor. The lab tries d, 2d, up to 8d.
Could a larger N break this step?
The method scales. The convergents are computed classically with a few arithmetic steps, so this stage is cheap even for 2048-bit numbers.
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.