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.
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:
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 email@example.com. 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.
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Number formed by adding product of its max and min digit K times
- Number formed after K times repeated addition of smallest divisor of N
- Print all K digit repeating numbers in a very large number
- Count of times second string can be formed from the characters of first string
- Sum of all subsets of a set formed by first n natural numbers
- Sum of sum of all subsets of a set formed by first N natural numbers
- Maximum factors formed by two numbers
- Count different numbers possible using all the digits their frequency times
- Count of decreasing pairs formed from numbers 1 to N
- Product of all Subsets of a set formed by first N natural numbers
- Minimum sum of two numbers formed from digits of an array
- Count numbers formed by given two digit with sum having given digits
- N digit numbers divisible by 5 formed from the M digits
- Count of Numbers in a Range where digit d occurs exactly K times
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Find if a molecule can be formed from 3 atoms using their valence numbers
- Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Sum of series formed by difference between product and sum of N natural numbers