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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Common divisors of N numbers
- Sum of common divisors of two numbers A and B
- 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
- 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
- Find numbers with n-divisors in a given range
- Number of divisors of product of N numbers
- Find numbers with K odd divisors in a given range
- Find all divisors of first N natural numbers
- Count of numbers below N whose sum of prime divisors is K
- Sum of all perfect square divisors of numbers from 1 to N
- Count the numbers < N which have equal number of divisors as K
- Find the number of divisors of all numbers in the range [1, n]
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Numbers in range [L, R] such that the count of their divisors is both even and prime