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.
- 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
- 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
- Find numbers with K odd divisors in a given range
- Number of divisors of product of N numbers
- Count of numbers below N whose sum of prime divisors is K
- Find two numbers whose divisors are given in a random order
- Count the numbers < N which have equal number of divisors as K
- Sum of all proper divisors of natural numbers in an array