Thursday, October 16, 2025
HomeData Modelling & AINumber Theory | Generators of finite cyclic group under addition

Number Theory | Generators of finite cyclic group under addition

Given a number n, find all generators of cyclic additive group under modulo n. Generator of a set {0, 1, … n-1} is an element x such that x is smaller than n, and using x (and addition operation), we can generate all elements of the set.
Examples: 
 

Input : 10
Output : 1 3 7 9
The set to be generated is {0, 1, .. 9}
By adding 1, single or more times, we 
can create all elements from 0 to 9.
Similarly using 3, we can generate all
elements.
30 % 10 = 0, 21 % 10 = 1, 12 % 10 = 2, ...
Same is true for 7 and 9.

Input  : 24
Output : 1 5 7 11 13 17 19 23

 

A simple solution is to run a loop from 1 to n-1 and for every element check if it is generator. To check generator, we keep adding element and we check if we can generate all numbers until remainder starts repeating.
An Efficient solution is based on the fact that a number x is generator if x is relatively prime to n, i.e., gcd(n, x) =1.
Below is the implementation of above approach:
 

C++




// A simple C++ program to find all generators
#include <bits/stdc++.h>
using namespace std;
  
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b%a, a);
}
  
// Print generators of n
int printGenerators(unsigned int n)
{
    // 1 is always a generator
    cout << "1 ";
  
    for (int i=2; i < n; i++)
  
        // A number x is generator of GCD is 1
        if (gcd(i, n) == 1)
            cout << i << " ";
}
  
// Driver program to test above function
int main()
{
    int n = 10;
    printGenerators(n);
    return 0;
}


Java




// A simple Java program to find all generators
  
class GFG {
      
  
// Function to return gcd of a and b
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b%a, a);
}
  
// Print generators of n
static void printGenerators(int n)
{
    // 1 is always a generator
    System.out.println("1 ");
  
    for (int i=2; i < n; i++)
  
        // A number x is generator of GCD is 1
        if (gcd(i, n) == 1)
            System.out.println(i +" ");
}
  
// Driver program to test above function
public static void main(String args[])
{
    int n = 10;
    printGenerators(n);
}
}


Python3




# Python3 program to find all generators
  
# Function to return gcd of a and b
def gcd(a, b):
    if (a == 0):
        return b;
    return gcd(b % a, a);
  
# Print generators of n
def printGenerators(n):
      
    # 1 is always a generator
    print("1", end = " ");
  
    for i in range(2, n):
  
        # A number x is generator 
        # of GCD is 1
        if (gcd(i, n) == 1):
            print(i, end = " ");
  
# Driver Code
n = 10;
printGenerators(n);
      
# This code is contributed by mits


C#




// A simple C# program to find all generators
using System;
  
class GFG 
{
      
// Function to return gcd of a and b
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Print generators of n
static void printGenerators(int n)
{
    // 1 is always a generator
    Console.Write("1 ");
  
    for (int i = 2; i < n; i++)
  
        // A number x is generator of GCD is 1
        if (gcd(i, n) == 1)
            Console.Write(i +" ");
}
  
// Driver code
public static void Main(String []args)
{
    int n = 10;
    printGenerators(n);
}
}
  
// This code contributed by Rajput-Ji


PHP




<?php
// PHP program to find all generators
  
// Function to return gcd of a and b
  
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
  
// Print generators of n
function printGenerators($n)
{
      
    // 1 is always a generator
    echo "1 ";
  
    for ($i = 2; $i < $n; $i++)
  
        // A number x is generator 
        // of GCD is 1
        if (gcd($i, $n) == 1)
            echo $i, " ";
}
  
// Driver program to test
// above function
    $n = 10;
    printGenerators($n);
      
// This code is contributed by Ajit
?>


Javascript




<script>
  
// A simple Javascript program to 
// find all generators
  
// Function to return gcd of a and b
function gcd(a, b)
{
    if (a == 0)
        return b;
          
    return gcd(b % a, a);
}
  
// Print generators of n
function printGenerators(n)
{
      
    // 1 is always a generator
    document.write("1 ");
  
    for(var i = 2; i < n; i++)
  
        // A number x is generator of 
        // GCD is 1
        if (gcd(i, n) == 1)
            document.write(i + " ");
}
  
// Driver Code
var n = 10;
  
printGenerators(n);
  
// This code is contributed by Kirti
  
</script>


Output : 

1 3 7 9

Time Complexity: O(nlogn) 
Auxiliary space: O(1)

How does this work? 
If we consider all remainders of n consecutive multiples of x, then some remainders would repeat if GCD of x and n is not 1. If some remainders repeat, then x cannot be a generator. Note that after n consecutive multiples, remainders would anyway repeat.
Interesting Observation : 
Number of generators of a number n is equal to ?(n) where ? is Euler Totient Function.
This article is contributed by Ujjwal Goyal. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS