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
- Largest number less than or equal to N/2 which is coprime to N
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Find two co-prime integers such that the first divides A and the second divides B
- Highest power of a number that divides other number
- Minimum value that divides one number and divisible by other
- Highest power of two that divides a given number
- Check if the sum of digits of a number N divides it
- 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]
- Highest power of 2 that divides a number represented in binary
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Find integers that divides maximum number of elements of the array
- Composite XOR and Coprime AND
- Check if a M-th fibonacci number divides N-th fibonacci number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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