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