Given two integer numbers, the task is to find count of all common divisors of given numbers?
Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common divisors are 1, 2, 4
It is recommended to refer all divisors of a given number as a prerequisite of this article.
A simple solution is to first find all divisors of first number and store them in an array or hash. Then find common divisors of second number and store them. Finally print common elements of two stored arrays or hash. The key is that the magnitude of powers of prime factors of a divisor should be equal to the minimum power of two prime factors of a and b.
- Find the prime factors of a using prime factorization.
- Find the count of each prime factor of a and store it in a Hashmap.
- Prime factorize b using distinct prime factors of a.
- Then the total number of divisors would be equal to the product of (count + 1)
of each factor.
- count is the minimum of counts of each prime factors of a and b.
- This gives the count of all divisors of a and b.
Efficient Solution –
A better solution is to calculate the greatest common divisor (gcd) of given two numbers, and then count divisors of that gcd.
Time complexity: O(log(n)+ n1/2) where n is the gcd of two numbers.
This article is contributed by Shashank Mishra ( Gullu ). 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.
- 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
- Sum of common divisors of two numbers A and B
- 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
- Common divisors of N numbers
- Find sum of divisors of all the divisors of a natural number
- Divisors of n-square that are not divisors of n
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Count divisors of n that have at-least one digit common with n
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Check if sum of divisors of two numbers are same
- Find two numbers whose divisors are given in a random order
- Print the kth common factor of two numbers
- Count of common multiples of two numbers in a range