Finding primitive roots modulo a number is of great interest in number theory, both in a theoretical standpoint and in a computational standpoint. In this post we compare and contrast three different ways of checking for primitive roots, continuing a discussion in an earlier post An elementary algorithm for finding primitive roots.
___________________________________________________________________________________________________________________
Background
Let be a positive integer. Let be a positive integer that is relative prime to . Let be Euler’s phi function, which counts the number of least residues that are relatively prime to the modulus. For example, as and are the only numbers relatively prime to (out of the numbers ). Furthermore, for any prime number . Previous posts on the phi function: Euler’s phi function, part 1 and Euler’s phi function, part 2.
Euler’s theorem tells us that . By the order of modulo we mean the least positive exponent such that (Euler’s theorem indicates that this notion of order is well defined). The number is said to be a primitive root modulo if the order of modulo is .
___________________________________________________________________________________________________________________
Three Checks
Let be a positive integer. Let be a positive integer that is relative prime to . How can we determine whether the number is a primitive root modulo ? We discuss three ways of answering this question.

____________________________________________________________________________________________
Check # 1

Check for all positive integers .
If each such congruence , then the number is a primitive root modulo .
____________________________________________________________________________________________
Check # 1 is merely a restatement of the definition of primitive root. It is a dumb test as it requires too much calculation. For large moduli, it would be an inefficient method of checking for primitive roots. The following is a much better test.

____________________________________________________________________________________________
Check # 2

Check for all positive divisors of with .
If each such congruence , then the number is a primitive root modulo .
____________________________________________________________________________________________
Check # 2 narrows down the checking by quite a bit – simply checking among the divisors of . This works because the only possible numbers for the order modulo of the number are the divisors of . So we can skip all that are not divisors of . The following lemma shows why this is so. Actually, the lemma proves something more general. It shows that if , then the order of must be a divisor of . Euler’s theorem says that . So the order of must be a divisor of .

Lemma 1

Let be the order of modulo . If , then .
Proof of Lemma 1
We have since is least with the property . By the division algorithm, we have where is some integer and . We have the following:
With and , it follows that and . Thus is a divisor of .
Though Check # 2 is definitely an improvement over Check # 1, the following further narrows the list of exponents to check.

____________________________________________________________________________________________
Check # 3

Find all prime divisors of . Then compute over all .
Check for all calculated above.
If each such congruence , then the number is a primitive root modulo .
____________________________________________________________________________________________
Check # 3 further eliminates the exponents to try when we check . Instead of checking over all the divisors of , we only need to try the divisors of the form where is a prime divisor of . The following lemma shows why this works.

Lemma 2

The number is a primitive root modulo if and only if for all prime divisors of .
Proof of Lemma 2
The direction is clear.
To show , suppose is not a primitive root modulo . Then
for some that is a divisor of . We have for some integer . Let be a prime factor of . Then for some integer . Consider the following derivation.
Thus if for all prime divisors of , then must be a primitive root modulo .
___________________________________________________________________________________________________________________
Examples
We now work some examples using Check # 3. The modular arithmetic is done using an online calculator. It can also be done using the fast powering algorithm (discussed in the post Congruence Arithmetic and Fast Powering Algorithm).
Example 1
Consider . Find all primitive roots modulo .
First . The divisors of are:
To use Check # 2, in order to find out if is a primitive root, we would need to calculate nine times, one for each of the above divisors of .
To use Check # 3, only two of these nine divisors are needed. There are two prime divisors of , namely and . We use and . So we check and modulo . The calculation is presented in the following tables.
The primitive roots are the rows with both congruences . They are:
One comment about the above tables. The nonone values in the above table seem to follow a pattern. In the columns for the calculation for , the values are either or . The nonone value is . It turns out that it has order modulo . The nonone values in the columns for are and . It turns out that they have order modulo . See the exercise stated below.
Example 2
Consider . Find all primitive roots modulo .
Since , the only prime divisor of $latex is . We use . For any , we only need to calculate .
The primitive roots modulo are:
Note that the nonone value in the above table has order modulo . See the exercise below.
___________________________________________________________________________________________________________________
Special Case
Based on Example 2, the following is a special case for Check # 3.

____________________________________________________________________________________________
Check # 3 (A Special Case)
Let be a prime such that for some positive integer .
Note that is the only prime divisor of .
Check where .
If , then the number is a primitive root modulo .
____________________________________________________________________________________________
___________________________________________________________________________________________________________________
Exercise
This is the exercise mentioned at the end of Example 1.
Let be a prime number. Let be a prime divisor of . Let be an integer with . Show that the number is either or has order modulo .
___________________________________________________________________________________________________________________
What a wonderful article! I really like this presentation and plan to make notes for myself from the material. I’m a Java/Scala programmer brushing up on number theory in the context of pseudo random number generators, where number theory talks, and ignorance walks.
in Lemma 2, you are testing only against prime factors of y, not t. What if there is a prime factor of t that is not a prime factor of y?