# Program to count number of common divisors of two numbers in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have two numbers a and b. We have to find how many positive integers are there, that are divisors to both a and b.

So, if the input is like a = 288 b = 240, then the output will be 10 because the common divisors are [1,2,3,4,6,8,12,16,24,48].

To solve this, we will follow these steps −

• res := 0
• for i in range 1 to gcd(a, b) + 1, do
• if (a mod i) is 0 and (b mod i) is 0, then
• res := res + 1
• return res

## Example

Let us see the following implementation to get better understanding −

from math import gcd

def solve(a, b):
res = 0
for i in range(1, gcd(a,b)+1):
if (a % i) == 0 and (b % i) == 0:
res += 1
return res

a, b = 288, 240
print(solve(a, b))

## Input

288, 240

## Output

10
Updated on 12-Oct-2021 14:04:06