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:
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.
- Count divisors of n that have at-least one digit common with n
- Check if a number has an odd count of odd divisors and even count of even divisors
- Check if count of even divisors of N is equal to count of odd divisors
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Find sum of divisors of all the divisors of a natural number
- Count pairs in array such that one element is power of another
- Count pairs in array such that one element is reverse of another
- Check if a number is divisible by all prime divisors of another number
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Break a number such that sum of maximum divisors of all parts is minimum
- Divisors of n-square that are not divisors of n
- Sum of common divisors of two numbers A and B
- Sum of all second largest divisors after splitting a number into one or more parts
- Divide the two given numbers by their common divisors
- C++ Program for Common Divisors of Two Numbers
- Java Program for Common Divisors of Two Numbers
- Common divisors of N numbers
- Common Divisors of Two Numbers
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.