给定素数 。任务是计算所有的原始根 。 ,因此整数x – 1,x 2 – 1,…。,x p – 2 – 1都不可被整数整除。 但是x p – 1 – 1可被整除 。 Input: P = 3 方法:所有素数始终至少有一个原始根。因此,使用Eulers上位函数,我们可以说f(p-1)是必需的答案,而f(n)是上位上函数。 如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。
基本根是整数x(1 <= x
例子:
Output: 1
The only primitive root modulo 3 is 2.
Input: P = 5
Output: 2
Primitive roots modulo 5 are 2 and 3.
下面是上述方法的实现:C++
// CPP program to find the number of
// primitive roots modulo prime
#include
Java
// Java program to find the number of
// primitive roots modulo prime
import java.io.*;
class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function to return the count of
// primitive roots modulo p
static int countPrimitiveRoots(int p)
{
int result = 1;
for (int i = 2; i < p; i++)
if (__gcd(i, p) == 1)
result++;
return result;
}
// Driver code
public static void main (String[] args) {
int p = 5;
System.out.println( countPrimitiveRoots(p - 1));
}
}
// This code is contributed by anuj_67..
Python3
# 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
def countPrimitiveRoots(p):
result = 1
for i in range(2, p, 1):
if (gcd(i, p) == 1):
result += 1
return result
# Driver code
if __name__ == '__main__':
p = 5
print(countPrimitiveRoots(p - 1))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to find the number of
// primitive roots modulo prime
using System;
class GFG {
// Recursive function to return gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function to return the count of
// primitive roots modulo p
static int countPrimitiveRoots(int p)
{
int result = 1;
for (int i = 2; i < p; i++)
if (__gcd(i, p) == 1)
result++;
return result;
}
// Driver code
static public void Main (String []args) {
int p = 5;
Console.WriteLine( countPrimitiveRoots(p - 1));
}
}
// This code is contributed by Arnab Kundu
PHP
$b)
return __gcd($a - $b, $b);
return __gcd($a, $b - $a);
}
// Function to return the count of
// primitive roots modulo p
function countPrimitiveRoots($p)
{
$result = 1;
for ($i = 2; $i < $p; $i++)
if (__gcd($i, $p) == 1)
$result++;
return $result;
}
// Driver code
$p = 5;
echo countPrimitiveRoots($p - 1);
// This code is contributed by anuj_67
?>
Javascript
2