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.

N = 21, a = 2, r = 6, Q = 512. Five strong outcomes and what the continued fractions make of them.
yProbabilityConvergents of y/512r found
016.7%nonenone
8511.4%0/1, 1/66
17111.4%0/1, 1/2, 1/36
25616.7%0/1, 1/26
34111.4%0/1, 1/1, 1/2, 2/36
42711.4%0/1, 1/1, 5/66

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

Check the original sources for exact citations.