Given three positive integer **n**, **x**, **y**. The task is to print Greatest Common Divisor of numbers formed by n repeating x times and number formed by n repeating y times.

0 <= n, x, y <= 1000000000.

**Examples :**

Input : n = 123, x = 2, y = 3. Output : 123 Number formed are 123123 and 123123123. Greatest Common Divisor of 123123 and 123123123 is 123. Input : n = 4, x = 4, y = 6. Output : 44

The idea is based on Euclidean algorithm to compute GCD of two number.

Let f(n, x) be a function which gives a number n repeated x times. So, we need to find GCD(f(n, x), f(n, y)).

Let n = 123, x = 3, y = 2.

So, first number A is f(123, 3) = 123123123 and second number B is f(123, 2) = 123123. We know, GCD(A,B) = GCD(A – B, B), using this property we can subtract any multiple of B, say B’ from first A as long as B’ is smaller than A.

So, A = 123123123 and B’ can be 123123000. On subtracting A will became 123 and B remains same.

Therfore, A = A – B’ = f(n, x – y).

So, GCD(f(n, x), f(n, y)) = GCD(f(n, x – y), f(n, y))

We can conclude following,

GCD(f(n, x), f(n, y)) = f(n, GCD(x, y)).

Below is the implementation based on this approach:

## CPP

`// C++ program to print Greatest Common Divisor ` `// of number formed by N repeating x times and ` `// y times. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the Greatest common Divisor of two numbers. ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b%a, a); ` `} ` ` ` `// Prints Greatest Common Divisor of number formed ` `// by n repeating x times and y times. ` `void` `findgcd(` `int` `n, ` `int` `x, ` `int` `y) ` `{ ` ` ` `// Finding GCD of x and y. ` ` ` `int` `g = gcd(x,y); ` ` ` ` ` `// Print n, g times. ` ` ` `for` `(` `int` `i = 0; i < g; i++) ` ` ` `cout << n; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 123, x = 5, y = 2; ` ` ` `findgcd(n, x, y); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print Greatest Common Divisor ` `// of number formed by N repeating x times and ` `// y times ` `class` `GFG { ` ` ` ` ` `// Return the Greatest common Divisor ` ` ` `// of two numbers. ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) { ` ` ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` ` ` `} ` ` ` ` ` `// Prints Greatest Common Divisor of ` ` ` `// number formed by n repeating x ` ` ` `// times and y times. ` ` ` `static` `void` `findgcd(` `int` `n, ` `int` `x, ` `int` `y) { ` ` ` ` ` `// Finding GCD of x and y. ` ` ` `int` `g = gcd(x, y); ` ` ` ` ` `// Print n, g times. ` ` ` `for` `(` `int` `i = ` `0` `; i < g; i++) ` ` ` `System.out.print(n); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `int` `n = ` `123` `, x = ` `5` `, y = ` `2` `; ` ` ` `findgcd(n, x, y); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to print Greatest ` `# Common Divisor of number formed ` `# by N repeating x times and y times ` ` ` `# Return the Greatest common Divisor ` `# of two numbers. ` `def` `gcd(a, b): ` ` ` ` ` `if` `(a ` `=` `=` `0` `): ` ` ` `return` `b ` ` ` ` ` `return` `gcd(b ` `%` `a, a) ` ` ` `# Prints Greatest Common Divisor of ` `# number formed by n repeating x times ` `# and y times. ` `def` `findgcd(n, x, y): ` ` ` ` ` `# Finding GCD of x and y. ` ` ` `g ` `=` `gcd(x, y) ` ` ` ` ` `# Print n, g times. ` ` ` `for` `i ` `in` `range` `(g): ` ` ` `print` `(n) ` ` ` `# Driver code ` `n ` `=` `123` `x ` `=` `5` `y ` `=` `2` ` ` `findgcd(n, x, y) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print Greatest Common ` `// Divisor of number formed by N ` `// repeating x times and y times ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return the Greatest common ` ` ` `// Divisor of two numbers. ` ` ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` ` ` `return` `gcd(b % a, a); ` ` ` `} ` ` ` ` ` `// Prints Greatest Common ` ` ` `// Divisor of number formed ` ` ` `// by n repeating x times ` ` ` `// and y times. ` ` ` `static` `void` `findgcd(` `int` `n, ` ` ` `int` `x, ` `int` `y) ` ` ` `{ ` ` ` ` ` `// Finding GCD of x and y. ` ` ` `int` `g = gcd(x, y); ` ` ` ` ` `// Print n, g times. ` ` ` `for` `(` `int` `i = 0; i < g; i++) ` ` ` `Console.Write(n); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() { ` ` ` ` ` `int` `n = 123, x = 5, y = 2; ` ` ` ` ` `findgcd(n, x, y); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to print ` `// Greatest Common Divisor ` `// of number formed by N ` `// repeating x times and y times. ` ` ` `// Return the Greatest common ` `// Divisor of two numbers. ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `return` `gcd(` `$b` `% ` `$a` `, ` `$a` `); ` `} ` ` ` `// Prints Greatest Common Divisor ` `// of number formed by n repeating ` `// x times and y times. ` `function` `findgcd(` `$n` `, ` `$x` `, ` `$y` `) ` `{ ` ` ` `// Finding GCD of x and y. ` ` ` `$g` `= gcd(` `$x` `, ` `$y` `); ` ` ` ` ` `// Print n, g times. ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$g` `; ` `$i` `++) ` ` ` `echo` `(` `$n` `); ` `} ` ` ` `// Driver Code ` `$n` `= 123; ` `$x` `= 5; ` `$y` `= 2; ` `findgcd(` `$n` `, ` `$x` `, ` `$y` `); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

123

This article is contributed by **Anuj Chauhan**. 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Number formed by adding product of its max and min digit K times
- GCD of elements which occur prime number of times
- GCD of elements occurring Fibonacci number of times in an Array
- Find two numbers whose sum and GCD are given
- Times required by Simple interest for the Principal to become Y times itself
- Count of times second string can be formed from the characters of first string
- Number formed after K times repeated addition of smallest divisor of N
- Find the repeating and the missing number using two equations
- GCD of two numbers when one of them can be very large
- GCD of more than two (or array) numbers
- Finding LCM of more than two (or array) numbers without using GCD
- Program to find GCD or HCF of two numbers
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- GCD of factorials of two numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K