Given two integers A and B. The task is to find the count of maximum elements from the common divisors of A and B such that all selected elements are co-prime to one another.
Input: A = 12, B = 18
Common divisors of A and B are 1, 2, 3 and 6.
Select 1, 2, and 3. All the pairs are co primes to
one another i.e. gcd(1, 2) = gcd(1, 3) = gcd(2, 3) = 1.
Input: A = 1, B = 3
Approach: It can be observed that all the common factors of A and B must be a factor of their gcd. And, in order for the factors of this gcd to be co-prime to one another, one element of the pair must be either 1 or both the elements must be prime. So the answer will be 1 more than the count of prime divisors of gcd(A, B). Note that 1 is added because 1 can also be a part of the chosen divisors as its gcd with the other pairs will always be 1.
Below is the implementation of the above approach:
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
- Count divisors of n that have at-least one digit common with n
- Count elements in the given range which have maximum number of divisors
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Common divisors of N numbers
- Common Divisors of Two Numbers
- Sum of common divisors of two numbers A and B
- C++ Program for Common Divisors of Two Numbers
- Divide the two given numbers by their common divisors
- Java Program for Common Divisors of Two Numbers
- Count Divisors of n in O(n^1/3)
- Count Divisors of Factorial
- Check if count of divisors is even or odd
- Count of the non-prime divisors of a given number
- Count of Fibonacci divisors of a given number
- Count divisors of array multiplication
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.