Home >>Python math Module >Python math.gcd() method

Python math.gcd() method

Python math.gcd() method

Python math.gcd() method in python is used to returns the greatest common divisor (the largest positive integer that divides both of the numbers) and it accepts two integer number. This method is also known as the highest common factor (HCF).

Syntax:
math.gcd(a,b)

Parameter Values

Parameter Description
a It is required the first integer to find the GCD
b It is required the second integer to find the GCD
Here is an Example of Python math.gcd() Method:

import math
print (math.gcd(12, 6))
print (math.gcd(8, 16))
print (math.gcd(2, 15))
print (math.gcd(0, 14))
print (math.gcd(0, 0))

Output:
8
1
14
0
Example 2:

import math
x = 23
y = 19
print("First gcd  = ", math.gcd(x,y))
x = 5
y = 12
print("Second gcd = ", math.gcd(x,y))
x = 0
y = -2
print("Third gcd = ", math.gcd(x,y))

Output:
First gcd = 1
Second gcd = 1
Third gcd = 2

No Sidebar ads