Easy Tutorial
❮ Python Reg Expressions Ref Math Isqrt ❯

Python math.gcd() Method

Python math Module

The math.gcd() method in Python returns the greatest common divisor (GCD) of the given integer arguments.

gcd(0,0) returns 0.

Python version: 3.5

Changed in version 3.9: Added support for an arbitrary number of arguments; previous versions only supported two arguments.

Syntax

The syntax for the math.gcd() method is as follows:

math.gcd(*integers)

Parameter Description:

Return Value

Returns an integer int, representing the greatest common divisor (GCD) of two or more integers.

Example

The following example returns the greatest common divisor of numbers:

Example

# Import math package
import math

# Output the greatest common divisor
print(math.gcd(3, 6))
print(math.gcd(6, 12))
print(math.gcd(12, 36))
print(math.gcd(-12, -36))
print(math.gcd(5, 12))
print(math.gcd(10, 0))
print(math.gcd(0, 34))
print(math.gcd(0, 0))

Output result:

3
6
12
12
1
10
34
0

Python math Module

❮ Python Reg Expressions Ref Math Isqrt ❯