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

`// 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*

*filter_none*

## 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); ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**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
- Check perfect square using addition/subtraction
- Find sum of N-th group of Natural Numbers
- 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
- Minimize the sum of the squares of the sum of elements of each group the array is divided into
- Ways of dividing a group into two halves such that two elements are in different groups
- Clustering Coefficient in Graph Theory
- Mathematics | Graph theory practice questions
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)