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.

Powers a^x mod 15 for x = 0 … 8. Each row repeats with its own period.
ax=0x=1x=2x=3x=4x=5x=6x=7x=8r
21248124814
7174131741314
1111111111111112
1411411411411412

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.

Every a from 2 to 14 for N = 15. “Lucky” means gcd(a, 15) is already a factor.
aPeriod rOutcomeFactors
24works3 × 5
3nonelucky gcd3 × 5
42works3 × 5
5nonelucky gcd5 × 3
6nonelucky gcd3 × 5
74works3 × 5
84works3 × 5
9nonelucky gcd3 × 5
10nonelucky gcd5 × 3
112works5 × 3
12nonelucky gcd3 × 5
134works3 × 5
142a^(r/2) ≡ −1none

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

Check the original sources for exact citations.