Python math.gcd() Method

Python math 模块Python math Module

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

gcd(0,0) returns 0.

Python version: 3.5

Changed in version 3.9: Support for an arbitrary number of arguments was added; previous versions only supported two arguments.

Syntax

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

math.gcd(*integers)

Parameter description:

  • *integers-- Required. A number. If x is not a number, returns TypeError.

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 the 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 模块Python math Module

Other extensions