Given two positive numbers x and y. Find the maximum valued integer a such that:
- a divides x i.e. x % a = 0
- a and y are co-prime i.e. gcd(a, y) = 1
Input : x = 15 y = 3 Output : a = 5 Explanation: 5 is the max integer which satisfies both the conditions. 15 % 5 =0 gcd(5, 3) = 1 Hence, output is 5. Input : x = 14 y = 28 Output : a = 1 Explanation: 14 % 1 =0 gcd(1, 28) = 1 Hence, output is 1.
Approach: Here, first we will remove the common factors of x and y from x by finding the greatest common divisor (gcd) of x and y and dividing x with that gcd.
x = x / gcd(x, y) —— STEP1
Now, we repeat STEP1 till we get gcd(x, y) = 1.
At last, we return a = x
5 1 7
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Largest number less than or equal to N/2 which is coprime to N
- Find the largest composite number that divides N but is strictly lesser than N
- Largest Coprime Set Between two integers
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Find two co-prime integers such that the first divides A and the second divides B
- Count of prime digits of a Number which divides the number
- Highest power of a number that divides other number
- Find the Smallest number that divides X^X
- Minimum value that divides one number and divisible by other
- Check if the sum of digits of a number N divides it
- Highest power of two that divides a given number
- Check if a given number divides the sum of the factorials of its digits
- Greatest divisor which divides all natural number in range [L, R]
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t