Number Theory | Generators of finite cyclic group under addition - GeeksforGeeks

19 Apr, 2021
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++
Java
Python3
C#
PHP
Javascript
// A simple C++ program to find all generators#include <bits/stdc++.h>using namespace std; // Function to return gcd of a and bint gcd(int a, int b){    if (a == 0)        return b;    return gcd(b%a, a);} // Print generators of nint 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 functionint main(){    int n = 10;    printGenerators(n);    return 0;}
// A simple Java program to find all generators class GFG {      // Function to return gcd of a and bstatic int gcd(int a, int b){    if (a == 0)        return b;    return gcd(b%a, a);} // Print generators of nstatic 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 functionpublic static void main(String args[]){    int n = 10;    printGenerators(n);}}
# Python3 program to find all generators # Function to return gcd of a and bdef gcd(a, b):    if (a == 0):        return b;    return gcd(b % a, a); # Print generators of ndef 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 Coden = 10;printGenerators(n);     # This code is contributed by mits
// A simple C# program to find all generatorsusing System; class GFG{     // Function to return gcd of a and bstatic int gcd(int a, int b){    if (a == 0)        return b;    return gcd(b % a, a);} // Print generators of nstatic 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 codepublic static void Main(String []args){    int n = 10;    printGenerators(n);}} // This code contributed by Rajput-Ji
<?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 nfunction 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?>
<script> // A simple Javascript program to// find all generators // Function to return gcd of a and bfunction gcd(a, b){    if (a == 0)        return b;             return gcd(b % a, a);} // Print generators of nfunction 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 Codevar n = 10; printGenerators(n); // This code is contributed by Kirti </script>
Output : 
1 3 7 9
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 GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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. 
jit_t
Mithun Kumar
Kirti_Mangal
Rajput-Ji
Akanksha_Rai
euler-totient
number-theory
Mathematical
Misc
Misc
number-theory
Mathematical
Misc
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Top 10 algorithms in Interview Questions
Find all factors of a natural number | Set 1
vector::push_back() and vector::pop_back() in C++ STL
Overview of Data Structures | Set 1 (Linear Data Structures)
How to write Regular Expressions?