Order finding: periods of a^x mod N
The sequence a^x mod N always repeats. Its period is the number Shor’s algorithm needs.
7 min read · reviewed December 2026
The sequence repeats
Take a coprime to N and compute a^0, a^1, a^2 … mod N. There are only finitely many residues, so the sequence must revisit one, and from then on it cycles. When a is coprime to N it cycles back to 1, so the whole sequence is periodic from the start. The smallest r with a^r ≡ 1 is called the order of a modulo 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 |
Different a, different order
In the table, 2 has order 4 and 7 has order 4, while 14 (which is −1 mod 15) has order 2. For N = 15 no a has order above 4, so the sequences are short. For a 2048-bit N the order can be astronomically large, which is why the classical method of walking the sequence fails.
What the lab does for every a
Here is the whole set for N = 15. Six values of a share a factor with 15 and are lucky. Of the seven coprime values, one (14) has a^(r/2) ≡ −1, and six give the factors 3 and 5.
| 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 |
The hard part is size, not idea
For tiny N a loop finds r instantly, and the lab does that to draw the answer line. The point of the quantum step is that its cost grows only polynomially with the number of bits of N, while the loop’s cost grows exponentially with them.
Try it
Questions
What does coprime mean here?
That gcd(a, N) = 1. Only then does a^x mod N return to 1.
Is the order the same as the period?
Yes, for coprime a. The period of the sequence a^x mod N is the order of a modulo N.
Can the order be larger than N?
No. It divides the number of integers below N that are coprime to N, so it is always smaller than N.
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.