On this page
Take a prime, say 7, and keep multiplying a number by itself, writing down only the remainder after dividing by 7. Some numbers visit every possible remainder before returning to 1. Others get stuck in a small loop. The ones that visit everything are primitive roots.
What is the order of a number mod p?
For a coprime to n, the order of a modulo n is the smallest positive power k with a^k ≡ 1 (mod n), as the Wikipedia article on primitive roots defines it.
For a prime p, Fermat's little theorem gives a^(p−1) ≡ 1 (mod p) whenever p does not divide a, so the order is always a divisor of p − 1. A primitive root is a number whose order is exactly p − 1, the largest possible.
Equivalently, g is a primitive root modulo n if every number coprime to n is congruent to some power of g. They exist exactly when n is 1, 2, 4, p^k or 2p^k for an odd prime p, a result first proved by Gauss, the same article notes.
Why is 3 a primitive root mod 7?
List the powers of 3 mod 7, multiplying the last remainder by 3 each time.
3^1: remainder 3
3^2: 3 × 3 = 9, remainder 2
3^3: 2 × 3 = 6, remainder 6
3^4: 6 × 3 = 18, remainder 4
3^5: 4 × 3 = 12, remainder 5
3^6: 5 × 3 = 15, remainder 1
The remainders 3, 2, 6, 4, 5, 1 are all six nonzero remainders mod 7, each once. The first 1 appears at the sixth power, so the order of 3 is 6 = 7 − 1, and 3 is a primitive root mod 7.
Why does 2 fail mod 7?
Powers of 2 give 2, then 4, then 8, which leaves remainder 1. After that the list repeats 2, 4, 1 forever.
The order of 2 mod 7 is 3, not 6. The remainders 3, 5 and 6 never appear, so 2 is not a primitive root.
How many primitive roots does a prime have?
Every prime p has exactly φ(p − 1) primitive roots, where φ is Euler's totient function, as stated on Wolfram MathWorld's primitive root page.
For p = 7, φ(6) = 2, so there are exactly two: 3 and 5. Check 5 yourself: its powers give 5, 4, 6, 2, 3, 1. The OEIS sequence A008330 lists φ(p − 1) for the primes 2, 3, 5, 7, 11, 13, 17, 19, 23 as 1, 1, 2, 2, 4, 4, 8, 6, 10.
How do you test for a primitive root quickly?
Listing every power is fine for 7 and painful for 101. The shortcut, given in the Wikipedia article: g (not a multiple of p) is a primitive root mod p if g^((p−1)/q) is not 1 mod p for every prime q that divides p − 1.
Worked example, p = 11:
Factor p − 1 = 10 = 2 × 5. The powers to check are 10/2 = 5 and 10/5 = 2.
Test 2: 2^5 = 32, remainder 10, and 2^2 = 4. Neither is 1, so 2 is a primitive root mod 11.
Test 3: 3^5 = 243 = 22 × 11 + 1, remainder 1. So 3 fails; its order is 5.
Count check: φ(10) = 4, so there are four primitive roots mod 11. They are 2, 6, 7 and 8.
If a step like this trips you up, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.
What is a discrete logarithm?
If g is a primitive root mod n and g^k ≡ a (mod n), then k is called the index or discrete logarithm of a to the base g, per the Wikipedia article on primitive roots.
Mod 7 with base 3, read the table backwards: the discrete log of 6 is 3, and of 4 is 4. Because 3 is a primitive root, every nonzero remainder has exactly one answer between 1 and 6.
Going forwards is easy; going backwards is hard for large primes. No efficient method is known for computing discrete logarithms in general, and Diffie–Hellman key exchange bases its security on that being hard in carefully chosen groups, as the Wikipedia article on discrete logarithms explains. Diffie and Hellman published the idea in 1976 in New directions in cryptography.
RSA rests on a different hard problem: see our worked RSA example with tiny primes. For more on primes, read which primes are a sum of two squares and why 1 is not a prime number. To place orders and Euler's totient in the wider map of mathematics, browse the Fermi Concept Library.



