TITLE: Number of common divisors between two given numbers
QUESTION [11 upvotes]: How can I compute the number of common divisors of two naturals ?
For example,if we consider (12,24) the answer is 6 i.e {1,12,2,6,3,4}.
EDIT : I got an answer here.The solution boils down to finding the number of divisor of the GCD of the two numbers.

REPLY [4 votes]: In response to user2969 :
At first you need to computed the gcd, it is always best to use Euclidean algorithm for this,my C++ implementation for the same :
int E_GCD(int a,int b){
   return b ? gcd(b,a%b):a;
}

Now we need to compute the number of divisors of N = E_GCD(A,B), this can be done efficiently by first computing the factorization using any fast algorithm like Pollard rho integer factorization or ECM then using the standard number theory $\text{trick}^*$ to for finding the number of factors,however in this problem if the values of A and B  are small (say:$0 \lt A,B \le 10^6$),then we don't need really need to use any of such sophisticated algorithms,instead you could build up your own algorithm simply based on the fact the the factors of a number occur in pair that is if say i is a factor of N then so is $N/i$ using this key observation I build up the algorithm,here is my C++ implementation of this idea :
  N = E_GCD(A,B);
  int ans = 0;
  int sqt = (int)sqrt(N);
  for(int i = 1 ; i <= sqt; i++){
      if(N % i == 0){
          ans += 2 ;
          if(i == N/i) ans--;
      }
  }
  printf("%d\n",ans);

Notice the fact that a number cannot a have a divisor which is greater than the square root of the number,rest of the code I believe is self explanatory.
*$\text{If }N = a^p \cdot b^q  \cdot c^r \cdots \text{,a,b,c ... are primes,number of factors of N is :}(p+1) \cdot (q+1)\cdot(r+1)\cdots$