Math Core

Module 3.3 · Number Theory

Orders and primitive roots

Euler's theorem says aφ(n)≡1(modn)a^{\varphi(n)} \equiv 1 \pmod n, but powers of aa often return to 11 much sooner. The order of aa is the exact length of that cycle. Orders explain how long repeating decimals are, which primes can divide numbers like 229−12^{29} - 1 or n4+1n^4 + 1, and how many solutions xk≡1x^k \equiv 1 has. They drive many of the hardest AIME number theory problems.

Order

Definition

Order

If gcd⁡(a,n)=1\gcd(a, n) = 1, the order of aa modulo nn, written ord⁡n(a)\operatorname{ord}_n(a), is the smallest positive integer dd with ad≡1(modn)a^d \equiv 1 \pmod n.

For example, mod 77 the powers of 22 are 2,4,1,2,4,1,…2, 4, 1, 2, 4, 1, \dots, so ord⁡7(2)=3\operatorname{ord}_7(2) = 3. The powers of 33 are 3,2,6,4,5,13, 2, 6, 4, 5, 1, so ord⁡7(3)=6\operatorname{ord}_7(3) = 6.

The order divides every exponent that gives 1

Let d=ord⁡n(a)d = \operatorname{ord}_n(a). Then

ak≡1(modn)  ⟺  d∣k.a^k \equiv 1 \pmod n \iff d \mid k.

In particular d∣φ(n)d \mid \varphi(n), and for a prime pp, d∣p−1d \mid p - 1.

Proof. If d∣kd \mid k, then ak=(ad)k/d≡1a^k = (a^d)^{k/d} \equiv 1. Conversely, suppose ak≡1a^k \equiv 1. Divide: k=qd+rk = qd + r with 0≤r<d0 \le r < d. Then 1≡ak=(ad)qar≡ar1 \equiv a^k = (a^d)^q a^r \equiv a^r. Since r<dr < d and dd is the smallest positive exponent giving 11, rr must be 00. So d∣kd \mid k. Euler's theorem gives aφ(n)≡1a^{\varphi(n)} \equiv 1, so d∣φ(n)d \mid \varphi(n).

How to compute an order. The order divides φ(n)\varphi(n), so only divisors need checking. Better: d=md = m exactly when am≡1a^m \equiv 1 but am/q≢1a^{m/q} \not\equiv 1 for every prime q∣mq \mid m. For ord⁡101(2)\operatorname{ord}_{101}(2), you only check 2502^{50} and 2202^{20}.

Primitive roots

Definition

Primitive root

gg is a primitive root mod nn if ord⁡n(g)=φ(n)\operatorname{ord}_n(g) = \varphi(n). Then the powers g,g2,…,gφ(n)g, g^2, \dots, g^{\varphi(n)} run through every residue coprime to nn.

Primitive roots mod a prime

Every prime pp has a primitive root gg. So every nonzero residue mod pp is gkg^k for exactly one k∈{1,…,p−1}k \in \{1, \dots, p-1\}, and

ord⁡p(gk)=p−1gcd⁡(k, p−1).\operatorname{ord}_p(g^k) = \frac{p-1}{\gcd(k, \, p-1)}.

Consequences:

  • For each d∣p−1d \mid p - 1 there are exactly φ(d)\varphi(d) residues of order dd. In particular there are φ(p−1)\varphi(p-1) primitive roots.
  • xk≡1(modp)x^k \equiv 1 \pmod p has exactly gcd⁡(k,p−1)\gcd(k, p-1) solutions.

Why the formula holds. (gk)m≡1(g^k)^m \equiv 1 iff (p−1)∣km(p-1) \mid km iff p−1gcd⁡(k,p−1)∣m\dfrac{p-1}{\gcd(k,p-1)} \mid m. The smallest such mm is p−1gcd⁡(k,p−1)\dfrac{p-1}{\gcd(k, p-1)}. The counting facts follow: gkg^k has order dd exactly when gcd⁡(k,p−1)=(p−1)/d\gcd(k, p-1) = (p-1)/d, which happens for φ(d)\varphi(d) values of kk; and gkm≡1g^{km} \equiv 1 iff (p−1)∣km(p-1) \mid km, which holds for gcd⁡(k,p−1)\gcd(k, p-1) values of mm.

The existence of a primitive root is a deeper fact (it uses that a polynomial of degree dd has at most dd roots mod pp); on the AIME you can quote it. Primitive roots also exist mod pkp^k and 2pk2p^k for odd primes pp, and mod 22 and 44, but not mod 88 or mod 10001000.

Three classic applications

Repeating decimals. If gcd⁡(n,10)=1\gcd(n, 10) = 1, the decimal for 1n\dfrac{1}{n} repeats with period exactly ord⁡n(10)\operatorname{ord}_n(10), because 1n=A10d−1\dfrac{1}{n} = \dfrac{A}{10^d - 1} for an integer AA exactly when n∣10d−1n \mid 10^d - 1.

Prime factors of aq−1a^q - 1. Let qq be prime and suppose a prime p∤a−1p \nmid a - 1 divides aq−1a^q - 1. Then ord⁡p(a)\operatorname{ord}_p(a) divides qq and isn't 11, so it equals qq. Since the order divides p−1p - 1, you get p≡1(modq)p \equiv 1 \pmod q. For 211−12^{11} - 1, every prime factor is ≡1(mod22)\equiv 1 \pmod{22} (it's odd too). The first candidate, 2323, works: 2047=23⋅892047 = 23 \cdot 89.

Prime factors of a2k+1a^{2^k} + 1. If an odd prime pp divides a2k+1a^{2^k} + 1, then a2k≡−1a^{2^k} \equiv -1 and a2k+1≡1a^{2^{k+1}} \equiv 1, so the order is exactly 2k+12^{k+1}. Hence p≡1(mod2k+1)p \equiv 1 \pmod{2^{k+1}}.

Orders mod prime powers

If x≡1(modpj)x \equiv 1 \pmod{p^j} exactly (with pp odd and j≥1j \ge 1), then xp≡1(modpj+1)x^p \equiv 1 \pmod{p^{j+1}} exactly. (Write x=1+tpjx = 1 + tp^j and expand (1+tpj)p(1 + tp^j)^p; this is a case of lifting the exponent.) So once you know the order mod pp, each extra power of pp in the modulus multiplies the order by at most pp.

Worked example: Computing an order

Find ord⁡13(2)\operatorname{ord}_{13}(2).

The order divides 1212. The maximal proper divisors are 66 and 44. 24=16≡32^4 = 16 \equiv 3 and 26=64≡12≡−12^6 = 64 \equiv 12 \equiv -1. Neither is 11, so the order is 1212: 22 is a primitive root mod 1313.

Worked example: A repeating decimal

How long is the repeating block of 137\dfrac{1}{37}?

101≡1010^1 \equiv 10, 102≡2610^2 \equiv 26, 103=1000=27⋅37+1≡1(mod37)10^3 = 1000 = 27 \cdot 37 + 1 \equiv 1 \pmod{37}. So ord⁡37(10)=3\operatorname{ord}_{37}(10) = 3, and indeed 137=0.027‾\dfrac{1}{37} = 0.\overline{027}.

Worked example: Factoring with orders

Find the smallest prime factor of 124+1=2073712^4 + 1 = 20737.

Any odd prime factor pp has 124≡−112^4 \equiv -1, so ord⁡p(12)=8\operatorname{ord}_p(12) = 8 and p≡1(mod8)p \equiv 1 \pmod 8. And p≠2p \ne 2 since 2073720737 is odd. The primes ≡1(mod8)\equiv 1 \pmod 8 are 17,41,73,89,…17, 41, 73, 89, \dots Testing: 20737=17⋅1219+1420737 = 17 \cdot 1219 + 14, 20737=41⋅505+3220737 = 41 \cdot 505 + 32, 20737=73⋅284+520737 = 73 \cdot 284 + 5, and 20737=89⋅23320737 = 89 \cdot 233. The answer is 8989 (and 233≡1(mod8)233 \equiv 1 \pmod 8 too, as it must be).

Worked example: Counting solutions

How many x∈{1,2,…,100}x \in \{1, 2, \dots, 100\} satisfy x20≡1(mod101)x^{20} \equiv 1 \pmod{101}?

With a primitive root gg, write x=gkx = g^k. Then x20≡1x^{20} \equiv 1 iff 100∣20k100 \mid 20k iff 5∣k5 \mid k. There are 2020 such kk in 1,…,1001, \dots, 100, matching gcd⁡(20,100)=20\gcd(20, 100) = 20.

Common mistake

"ak≡1a^k \equiv 1" tells you the order divides kk, not that it equals kk. To prove the order is exactly mm, you must rule out every m/qm/q for primes q∣mq \mid m.

Practice

Practice 1

Find the smallest positive integer nn such that 2n−12^n - 1 is divisible by 101101.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 2

How many integers gg with 1≤g≤961 \le g \le 96 are primitive roots modulo 9797?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

How many integers xx with 1≤x≤1001 \le x \le 100 satisfy x30≡1(mod101)x^{30} \equiv 1 \pmod{101}?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 4

How many positive integers n≤1000n \le 1000 make 2n+3n2^n + 3^n divisible by 1313?

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

Find the smallest positive integer nn such that 11n≡1(mod1000)11^n \equiv 1 \pmod{1000}.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

Find the smallest prime pp such that the decimal expansion of 1p\dfrac{1}{p} repeats with period exactly 77.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 7

Find the smallest prime factor of 229−12^{29} - 1.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 8

Find the smallest positive integer nn such that 2n≡1(mod243)2^n \equiv 1 \pmod{243}.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Real contest practice

  • 2019 AIME I, Problem 14: the least odd prime factor of 20198+12019^8 + 1; the order argument forces p≡1(mod16)p \equiv 1 \pmod{16}.
  • 2020 AIME I, Problem 12: the least nn making 149n−2n149^n - 2^n divisible by a product of prime powers; the mod-55 part is an order computation.