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 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right



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.



My Personal Notes arrow_drop_up