Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python math.gcd() Method

Python math.gcd() Method

Python math Module 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; earlier versions supported only two arguments.

Syntax

The syntax of 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 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 Module Python math Module