Primitive root of a prime number n modulo n
Last Updated :
23 Jul, 2025
Given a prime number n, the task is to find its primitive root under modulo n. The 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 the range[0, n-2] are different. Return -1 if n is a non-prime number.
Examples:
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 the range[0, n-2]. If all these values are different, then return r, else continue for the next value of r. If all values of r are tried, return -1.
An efficient solution is based on the below facts.
If the multiplicative order of a number r modulo n is equal to Euler Totient Function ?(n) ( note that the Euler Totient Function for a prime n is n-1), then it is a primitive root.
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 roots for a prime number, we are only concerned with the smallest one. If you want to find all the roots, then continue the process till p-1 instead of breaking up by finding the first primitive root.
C++
// C++ program to find primitive root of a
// given number n
#include<bits/stdc++.h>
using namespace std;
// Returns true if n is prime
bool isPrime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n%2 == 0 || n%3 == 0) return false;
for (int i=5; i*i<=n; i=i+6)
if (n%i == 0 || n%(i+2) == 0)
return false;
return true;
}
/* Iterative Function to calculate (x^n)%p in
O(logy) */
int power(int x, unsigned int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x*x) % p;
}
return res;
}
// Utility function to store prime factors of a number
void findPrimefactors(unordered_set<int> &s, int n)
{
// Print the number of 2s that divide n
while (n%2 == 0)
{
s.insert(2);
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
s.insert(i);
n = n/i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
s.insert(n);
}
// Function to find smallest primitive root of n
int findPrimitive(int n)
{
unordered_set<int> s;
// Check if n is prime or not
if (isPrime(n)==false)
return -1;
// Find value of Euler Totient function of n
// Since n is a prime number, the value of Euler
// Totient function is n-1 as there are n-1
// relatively prime numbers.
int phi = n-1;
// Find prime factors of phi and store in a set
findPrimefactors(s, phi);
// Check for every number from 2 to phi
for (int r=2; r<=phi; r++)
{
// Iterate through all prime factors of phi.
// and check if we found a power with value 1
bool flag = false;
for (auto it = s.begin(); it != s.end(); it++)
{
// Check if r^((phi)/primefactors) mod n
// is 1 or not
if (power(r, phi/(*it), n) == 1)
{
flag = true;
break;
}
}
// If there was no power with value 1.
if (flag == false)
return r;
}
// If no primitive root found
return -1;
}
// Driver code
int main()
{
int n = 761;
cout << " Smallest primitive root of " << n
<< " is " << findPrimitive(n);
return 0;
}
Java
// Java program to find primitive root of a
// given number n
import java.util.*;
class GFG
{
// Returns true if n is prime
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
{
return false;
}
if (n <= 3)
{
return true;
}
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
{
return false;
}
for (int i = 5; i * i <= n; i = i + 6)
{
if (n % i == 0 || n % (i + 2) == 0)
{
return false;
}
}
return true;
}
/* Iterative Function to calculate (x^n)%p in
O(logy) */
static int power(int x, int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y % 2 == 1)
{
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Utility function to store prime factors of a number
static void findPrimefactors(HashSet<Integer> s, int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
s.add(2);
n = n / 2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
s.add(i);
n = n / i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
s.add(n);
}
}
// Function to find smallest primitive root of n
static int findPrimitive(int n)
{
HashSet<Integer> s = new HashSet<Integer>();
// Check if n is prime or not
if (isPrime(n) == false)
{
return -1;
}
// Find value of Euler Totient function of n
// Since n is a prime number, the value of Euler
// Totient function is n-1 as there are n-1
// relatively prime numbers.
int phi = n - 1;
// Find prime factors of phi and store in a set
findPrimefactors(s, phi);
// Check for every number from 2 to phi
for (int r = 2; r <= phi; r++)
{
// Iterate through all prime factors of phi.
// and check if we found a power with value 1
boolean flag = false;
for (Integer a : s)
{
// Check if r^((phi)/primefactors) mod n
// is 1 or not
if (power(r, phi / (a), n) == 1)
{
flag = true;
break;
}
}
// If there was no power with value 1.
if (flag == false)
{
return r;
}
}
// If no primitive root found
return -1;
}
// Driver code
public static void main(String[] args)
{
int n = 761;
System.out.println(" Smallest primitive root of " + n
+ " is " + findPrimitive(n));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 program to find primitive root
# of a given number n
from math import sqrt
# Returns True if n is prime
def isPrime( n):
# Corner cases
if (n <= 1):
return False
if (n <= 3):
return True
# This is checked so that we can skip
# middle five numbers in below loop
if (n % 2 == 0 or n % 3 == 0):
return False
i = 5
while(i * i <= n):
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
""" Iterative Function to calculate (x^n)%p
in O(logy) */"""
def power( x, y, p):
res = 1 # Initialize result
x = x % p # Update x if it is more
# than or equal to p
while (y > 0):
# If y is odd, multiply x with result
if (y & 1):
res = (res * x) % p
# y must be even now
y = y >> 1 # y = y/2
x = (x * x) % p
return res
# Utility function to store prime
# factors of a number
def findPrimefactors(s, n) :
# Print the number of 2s that divide n
while (n % 2 == 0) :
s.add(2)
n = n // 2
# n must be odd at this point. So we can
# skip one element (Note i = i +2)
for i in range(3, int(sqrt(n)), 2):
# While i divides n, print i and divide n
while (n % i == 0) :
s.add(i)
n = n // i
# This condition is to handle the case
# when n is a prime number greater than 2
if (n > 2) :
s.add(n)
# Function to find smallest primitive
# root of n
def findPrimitive( n) :
s = set()
# Check if n is prime or not
if (isPrime(n) == False):
return -1
# Find value of Euler Totient function
# of n. Since n is a prime number, the
# value of Euler Totient function is n-1
# as there are n-1 relatively prime numbers.
phi = n - 1
# Find prime factors of phi and store in a set
findPrimefactors(s, phi)
# Check for every number from 2 to phi
for r in range(2, phi + 1):
# Iterate through all prime factors of phi.
# and check if we found a power with value 1
flag = False
for it in s:
# Check if r^((phi)/primefactors)
# mod n is 1 or not
if (power(r, phi // it, n) == 1):
flag = True
break
# If there was no power with value 1.
if (flag == False):
return r
# If no primitive root found
return -1
# Driver Code
n = 761
print("Smallest primitive root of",
n, "is", findPrimitive(n))
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program to find primitive root of a
// given number n
using System;
using System.Collections.Generic;
class GFG
{
// Returns true if n is prime
static bool isPrime(int n)
{
// Corner cases
if (n <= 1)
{
return false;
}
if (n <= 3)
{
return true;
}
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
{
return false;
}
for (int i = 5; i * i <= n; i = i + 6)
{
if (n % i == 0 || n % (i + 2) == 0)
{
return false;
}
}
return true;
}
/* Iterative Function to calculate (x^n)%p in
O(logy) */
static int power(int x, int y, int p)
{
int res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y % 2 == 1)
{
res = (res * x) % p;
}
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Utility function to store prime factors of a number
static void findPrimefactors(HashSet<int> s, int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
s.Add(2);
n = n / 2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= Math.Sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
s.Add(i);
n = n / i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
s.Add(n);
}
}
// Function to find smallest primitive root of n
static int findPrimitive(int n)
{
HashSet<int> s = new HashSet<int>();
// Check if n is prime or not
if (isPrime(n) == false)
{
return -1;
}
// Find value of Euler Totient function of n
// Since n is a prime number, the value of Euler
// Totient function is n-1 as there are n-1
// relatively prime numbers.
int phi = n - 1;
// Find prime factors of phi and store in a set
findPrimefactors(s, phi);
// Check for every number from 2 to phi
for (int r = 2; r <= phi; r++)
{
// Iterate through all prime factors of phi.
// and check if we found a power with value 1
bool flag = false;
foreach (int a in s)
{
// Check if r^((phi)/primefactors) mod n
// is 1 or not
if (power(r, phi / (a), n) == 1)
{
flag = true;
break;
}
}
// If there was no power with value 1.
if (flag == false)
{
return r;
}
}
// If no primitive root found
return -1;
}
// Driver code
public static void Main(String[] args)
{
int n = 761;
Console.WriteLine(" Smallest primitive root of " + n
+ " is " + findPrimitive(n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript program to find primitive root of a
// given number n
// Returns true if n is prime
function isPrime(n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (let i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
/* Iterative Function to calculate (x^n)%p in
O(logy) */
function power(x, y, p) {
let res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Utility function to store prime factors of a number
function findPrimefactors(s, n) {
// Print the number of 2s that divide n
while (n % 2 == 0) {
s.add(2);
n = n / 2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (let i = 3; i <= Math.sqrt(n); i = i + 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
s.add(i);
n = n / i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
s.add(n);
}
// Function to find smallest primitive root of n
function findPrimitive(n) {
let s = new Set();
// Check if n is prime or not
if (isPrime(n) == false)
return -1;
// Find value of Euler Totient function of n
// Since n is a prime number, the value of Euler
// Totient function is n-1 as there are n-1
// relatively prime numbers.
let phi = n - 1;
// Find prime factors of phi and store in a set
findPrimefactors(s, phi);
// Check for every number from 2 to phi
for (let r = 2; r <= phi; r++) {
// Iterate through all prime factors of phi.
// and check if we found a power with value 1
let flag = false;
for (let it of s) {
// Check if r^((phi)/primefactors) mod n
// is 1 or not
if (power(r, phi / it, n) == 1) {
flag = true;
break;
}
}
// If there was no power with value 1.
if (flag == false)
return r;
}
// If no primitive root found
return -1;
}
// Driver code
let n = 761;
document.write(" Smallest primitive root of " + n + " is " + findPrimitive(n));
// This code is contributed by gfgking
</script>
Output:
Smallest primitive root of 761 is 6
Time Complexity : O(n^2 * logn)
Space Complexity : O(sqrt(n))
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem