Primitive Root
Definition
In modular arithmetic, a number is called a primitive root modulo n if every number coprime to is congruent to a power of modulo . Mathematically, is a primitive root modulo n if and only if for any integer such that , there exists an integer such that:
.
is then called the index or discrete logarithm of to the base modulo . is also called the generator of the multiplicative group of integers modulo .
In particular, for the case where is a prime, the powers of primitive root runs through all numbers from to .
Existence
Primitive root modulo exists if and only if:
- is 1, 2, 4, or
- is power of an odd prime number , or
- is twice power of an odd prime number .
This theorem was proved by Gauss in 1801.
Relation with the Euler function
Let be a primitive root modulo . Then we can show that the smallest number for which is equal . Moreover, the reverse is also true, and this fact will be used in this article to find a primitive root.
Furthermore, the number of primitive roots modulo , if there are any, is equal to .
Algorithm for finding a primitive root
A naive algorithm is to consider all numbers in range . And then check if each one is a primitive root, by calculating all its power to see if they are all different. This algorithm has complexity , which would be too slow. In this section, we propose a faster algorithm using several well-known theorems.
From previous section, we know that if the smallest number for which is , then is a primitive root. Since for any number relative prime to , we know from Euler's theorem that , then to check if is primitive root, it is enough to check that for all less than , . However, this algorithm is still too slow.
From Lagrange's theorem, we know that the index of 1 of any number modulo must be a divisor of . Thus, it is sufficient to verify for all proper divisor that . This is already a much faster algorithm, but we can still do better.
Factorize . We prove that in the previous algorithm, it is sufficient to consider only the values of which have the form . Indeed, let be any proper divisor of . Then, obviously, there exists such that , i.e. . However, if , we would get:
.
i.e. among the numbers of the form , there would be at least one such that the conditions were not met.
Now we have a complete algorithm for finding the primitive root:
First, find and factorize it.
Then iterate through all numbers , and for each number, to check if it is primitive root, we do the following:
- Calculate all .
- If all the calculated values are different from , then is a primitive root.
Running time of this algorithm is (assume that has divisors).
Shoup (1990, 1992) proved, assuming the generalized Riemann hypothesis, that is .
Implementation
The following code assumes that the modulo p is a prime number. To make it works for any value of p, we must add calculation of .
int powmod (int a, int b, int p) {
int res = 1;
while (b)
if (b & 1)
res = int (res * 1ll * a % p), --b;
else
a = int (a * 1ll * a % p), b >>= 1;
return res;
}
int generator (int p) {
vector<int> fact;
int phi = p-1, n = phi;
for (int i=2; i*i<=n; ++i)
if (n % i == 0) {
fact.push_back (i);
while (n % i == 0)
n /= i;
}
if (n > 1)
fact.push_back (n);
for (int res=2; res<=p; ++res) {
bool ok = true;
for (size_t i=0; i<fact.size() && ok; ++i)
ok &= powmod (res, phi / fact[i], p) != 1;
if (ok) return res;
}
return -1;
}