Open In App

Number Theory | Generators of finite cyclic group under addition

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

 



Previous Article
Next Article

Similar Reads

Prove that Every Cyclic Group is an Abelian Group
Groups, subgroups, rings, fields, integral domains, graphs, trees, cut sets, etc are one of the most important concepts in Discrete Mathematics. In this article, we are going to discuss and prove that every cyclic group is an abelian group. Before proceeding to the proof let's first understand some basic terminologies like what is an abelian group,
2 min read
Number of possible Equivalence Relations on a finite set
An equivalence relation is Reflexive, Symmetric and Transitive. Before counting the number of possible equivalence relations on a set |A|=n, let us see an example of a equivalence relation and identify Equivalence Classes in it. Let A = {1, 2, 3, 4} be a set and R = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 3), (3, 4), (4, 3), (4, 4)} be an equivalence
4 min read
Number of different cyclic paths of length N in a tetrahedron
Given a tetrahedron(vertex are A, B, C, D), the task is to find the number of different cyclic paths with length n from a vertex.Note: Considering only a single vertex B i.e. to find the number of different cyclic paths of length N from B to itself. Examples: Input: 2 Output: 3 The paths of length 2 which starts and ends at D are: B-A-B B-D-B B-C-B
5 min read
Count number of ways to convert string S to T by performing K cyclic shifts
Given two strings S and T and a number K, the task is to count the number of ways to convert string S to string T by performing K cyclic shifts. The cyclic shift is defined as the string S can be split into two non-empty parts X + Y and in one operation we can transform S to Y + X from X + Y. Note: Since count can be very large print the answer to
16 min read
Generate all cyclic permutations of a number
Given a number N, our task is to generate all the possible cyclic permutations of the number. A cyclic permutation shifts all the elements of a set by a fixed offset. For a set with elements [Tex]a_0 [/Tex], [Tex]a_1 [/Tex], ..., [Tex]a_n [/Tex], a cyclic permutation of one place to the left would yield [Tex]a_1 [/Tex], ..., [Tex]a_n [/Tex], [Tex]a
6 min read
Cyclic Number
A cyclic number is an integer in which cyclic permutations of the digits are successive multiples of the number. The most widely known is the six-digit number 142857 (Please see below explanation given in examples).The following trivial cases are typically excluded for Cyclic Numbers. Single digits, e.g.: 5Repeated digits, e.g.: 555Repeated cyclic
9 min read
Number of cyclic elements in an array where we can jump according to value
Given a array arr[] of n integers. For every value arr[i], we can move to arr[i] + 1 clockwise considering array elements in cycle. We need to count cyclic elements in the array. An element is cyclic if starting from it and moving to arr[i] + 1 leads to same element. Examples: Input : arr[] = {1, 1, 1, 1} Output : 4 All 4 elements are cyclic elemen
11 min read
Find smallest number with given number of digits and sum of digits under given constraints
Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: Input: S = 25, D = 4 Output: 6667 The difference be
7 min read
Nearest smaller number to N having multiplicative inverse under modulo N equal to that number
Given a prime number N, the task is to find the closest smaller number than N such that modulo multiplicative inverse of a number under modulo N is equal to the number itself. Examples: Input: N = 7Output: 6Explanation:Modulo multiplicative inverse of all possible natural numbers from 1 to less than N are:Modulo multiplicative inverse of 1 under mo
3 min read
Check If every group of a's is followed by a group of b's of same length
Given string str, the task is to check whether every group of consecutive a's is followed by a group of consecutive b's of the same length. If the condition is true for every group then print 1 else print 0. Examples: Input: str = "ababaabb" Output: 1 ab, ab, aabb. All groups are valid Input: str = "aabbabb" Output: 0 aabb, abb (A single 'a' follow
5 min read