Given a prime . The task is to count all the primitive roots of .
A primitive root is an integer x (1 <= x < p) such that none of the integers x – 1, x2 – 1, …., xp – 2 – 1 are divisible by but xp – 1 – 1 is divisible by .
Input: P = 3
The only primitive root modulo 3 is 2.
Input: P = 5
Primitive roots modulo 5 are 2 and 3.
Approach: There is always at least one primitive root for all primes. So, using Eulers totient function we can say that f(p-1) is the required answer where f(n) is euler totient function.
Below is the implementation of the above approach:
# Python 3 program to find the number
# of primitive roots modulo prime
from math import gcd
# Function to return the count of
# primitive roots modulo p
result = 1
for i in range(2, p, 1):
if (gcd(i, p) == 1):
result += 1
# Driver code
if __name__ == ‘__main__’:
p = 5
print(countPrimitiveRoots(p – 1))
# This code is contributed by
- Primitive root of a prime number n modulo n
- Find sum of modulo K of first N natural number
- Find coordinates of a prime number in a Prime Spiral
- Program to find the Roots of Quadratic equation
- Seeds (Or Seed Roots) of a number
- Given a number x , find y such that x*y + 1 is not a prime
- Find sum of a number and its maximum prime factor
- Find largest prime factor of a number
- Find Largest Special Prime which is less than or equal to a given number
- Analysis of Different Methods to find Prime Number in Python
- Quick ways to check for Prime and find next Prime in Java
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Program to find remainder without using modulo or % operator
- Discrete logarithm (Find an integer k such that a^k is congruent modulo b)
- Print the nearest prime number formed by adding prime numbers to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.