Common Divisors of Two Numbers

Given two integer numbers, the task is to find count of all common divisors of given numbers?

Examples :

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.



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of program
#include<bits/stdc++.h>
using namespace std;
  
// Function to calculate gcd of two numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b%a, a);
}
  
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
int commDiv(int a,int b)
{
    // find gcd of a,b
    int n = gcd(a, b);
  
    // Count divisors of n.
    int result = 0;
    for (int i=1; i<=sqrt(n); i++)
    {
        // if 'i' is factor of n
        if (n%i==0)
        {
            // check if divisors are equal
            if (n/i == i)
                result += 1;
            else
                result += 2;
        }
    }
    return result;
}
  
// Driver program to run the case
int main()
{
    int a = 12, b = 24;
    cout << commDiv(a, b);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of program
  
class Test
{
    // method to calculate gcd of two numbers
    static int gcd(int a, int b)
    {
        if (a == 0
            return b;
          
        return gcd(b%a,a);
    }
    // method to calculate all common divisors
    // of two given numbers
    // a, b --> input integer numbers
    static int commDiv(int a,int b)
    {
        // find gcd of a,b
        int n = gcd(a, b);
       
        // Count divisors of n.
        int result = 0;
        for (int i=1; i<=Math.sqrt(n); i++)
        {
            // if 'i' is factor of n
            if (n%i==0)
            {
                // check if divisors are equal
                if (n/i == i)
                    result += 1;
                else
                    result += 2;
            }
        }
        return result;
    }
  
    // Driver method
    public static void main(String args[])
    {
        int a = 12, b = 24;
        System.out.println(commDiv(a, b));
    }
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python Program to find 
# Common Divisors of Two Numbers
  
a = 12
b = 24
n = 0
  
for i in range(1, min(a, b)+1):
    if a%i==b%i==0:
        n+=1
      
print(n)
  
# Code contributed by Mohit Gupta_OMG

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of program
using System;
  
class GFG {
      
    // method to calculate gcd
    // of two numbers
    static int gcd(int a, int b)
    {
        if (a == 0) 
            return b;
          
        return gcd(b % a, a);
    }
      
    // method to calculate all
    // common divisors of two 
    // given numbers a, b --> 
    // input integer numbers
    static int commDiv(int a, int b)
    {
          
        // find gcd of a,b
        int n = gcd(a, b);
      
        // Count divisors of n.
        int result = 0;
        for (int i = 1; i <= Math.Sqrt(n); i++)
        {
              
            // if 'i' is factor of n
            if (n % i == 0)
            {
                  
                // check if divisors are equal
                if (n / i == i)
                    result += 1;
                else
                    result += 2;
            }
        }
          
        return result;
    }
  
    // Driver method
    public static void Main(String []args)
    {
          
        int a = 12, b = 24;
          
        Console.Write(commDiv(a, b));
    }
}
  
// This code contributed by parashar.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of program
  
// Function to calculate 
// gcd of two numbers
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
  
// Function to calculate all common 
// divisors of two given numbers
// a, b --> input integer numbers
function commDiv($a, $b)
{
    // find gcd of a,b
    $n = gcd($a, $b);
  
    // Count divisors of n.
    $result = 0;
    for ($i = 1; $i <= sqrt($n); 
                 $i++)
    {
        // if 'i' is factor of n
        if ($n % $i == 0)
        {
            // check if divisors 
            // are equal
            if ($n / $i == $i)
                $result += 1;
            else
                $result += 2;
        }
    }
    return $result;
}
  
// Driver Code
$a = 12; $b = 24;
echo(commDiv($a, $b));
  
// This code is contributed by Ajit.
?>

chevron_right



Output :

6

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 contribute@geeksforgeeks.org. 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.




My Personal Notes arrow_drop_up

Improved By : parashar, jit_t