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 ?> |
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.
Recommended Posts:
- Cyclic Number
- Generate all cyclic permutations of a number
- Number Theory (Interesting Facts and Algorithms)
- Add N digits to A such that it is divisible by B after each addition
- Addition and Subtraction of Matrix using pthreads
- Addition of two numbers without propagating Carry
- Check perfect square using addition/subtraction
- Exterior angle of a cyclic quadrilateral when the opposite interior angle is given
- Find sum of N-th group of Natural Numbers
- Total ways of selecting a group of X men from N men with or without including a particular man
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Ways of selecting men and women from a group to make a team
- Ways of dividing a group into two halves such that two elements are in different groups
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Clustering Coefficient in Graph Theory