Given a prime number n, the task is to find its primitive root under modulo n. Primitive root of a prime number n is an integer r between[1, n-1] such that the values of r^x(mod n) where x is in range[0, n-2] are different. Return -1 if n is a non-prime number.
Input : 7 Output : Smallest primitive root = 3 Explanation: n = 7 3^0(mod 7) = 1 3^1(mod 7) = 3 3^2(mod 7) = 2 3^3(mod 7) = 6 3^4(mod 7) = 4 3^5(mod 7) = 5 Input : 761 Output : Smallest primitive root = 6
A simple solution is to try all numbers from 2 to n-1. For every number r, compute values of r^x(mod n) where x is in range[0, n-2]. If all these values are different, then return r, else continue for next value of r. If all values of r are tried, return -1.
An efficient solution is based on below fact.
If the multiplicative order of a number r modulo n is equal to Euler Totient Function Φ(n) (Note that Euler Totient Function for a prime n is n-1), then it is a primitive root [Source : Wiki]
1- Euler Totient Function phi = n-1 [Assuming n is prime] 1- Find all prime factors of phi. 2- Calculate all powers to be calculated further using (phi/prime-factors) one by one. 3- Check for all numbered for all powers from i=2 to n-1 i.e. (i^ powers) modulo n. 4- If it is 1 then 'i' is not a primitive root of n. 5- If it is never 1 then return i;.
Although there can be multiple primitive root for a prime number but we are only concerned for smallest one.If you want to find all roots then continue the process till p-1 instead of breaking up on finding first primitive root.
Smallest primitive root of 761 is 6
This article is contributed by Niteesh kumar and Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the number of primitive roots modulo prime
- Number of factors of very large number N modulo M where M is any prime number
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- Primitive Abundant Number
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Print the nearest prime number formed by adding prime numbers to N
- Find sum of modulo K of first N natural number
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find coordinates of a prime number in a Prime Spiral
- Fibonacci Number modulo M and Pisano Period
- Expressing a fraction as a natural number under modulo 'm'
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Nth root of a number using log
- N-th root of a number
- Square root of a number using log
- Find cubic root of a number