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.
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(?n)
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.
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
- Common divisors of N 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
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- 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
- Numbers with exactly 3 divisors
- Check if sum of divisors of two numbers are same
- Number of divisors of product of N numbers
- Find all divisors of first N natural numbers
- Find numbers with n-divisors in a given range
- Sum of all perfect square divisors of numbers from 1 to N
- Find numbers with K odd divisors in a given range