I need to find the total number of divisors which divide both the numbers lets say N and M.
Actually I tried to think about it a lot and searched at a lot of places for a possible answer. All I could think of was finding the prime factors and generating the actual divisors from the factors ?
However this would take a lot of computation,therefore I need a much faster method performing much fewer operations?
Is there any well known algorithm or formula for this problem or any derivable formula?
I think the answer should be the number of divisors of the gcd of the 2 numbers.

2 Answers
2

It is the number of divisors of the greatest common divisor (GCD) of N and M. You can find the GCD by the Euclidean algorithm, which does not require factoring. This is generally a smaller number than N or M, but then you need to factor it. You don't need to generate all the divisors, you can use the divisor function as the product of one more than the power of each prime dividing the GCD.