Python math模块中定义了一些数学函数。由于这个模块属于编译系统自带,因此它可以被无条件调用。该模块还提供了与用标准C定义的数学函数的接口。本文主要介绍Python math.gcd() 方法的使用,以及相关示例代码。

Python math.gcd() 方法

Python Math方法

例如:

求两个整数的最大公约数:

#Import math Library
import math

#求这两个整数的最大公约数
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))

1、定义和用法

math.gcd()方法返回两个整数int1int2的最大公约数。

GCD是最大的除数,可除以余数。

GCD也被称为最高公因子(HCF)。

提示:gcd(0,0)返回0。

2、调用语法

math.gcd(int1,int2)

3、参数说明

参数

描述

int1

必需的参数, 找到GCD的第一个整数

int2

必需的参数, 查找GCD的第二个整数

4、方法说明

返回值:

int值,表示两个整数的最大公约数(GCD)

Python Version:

3.5

Python Math方法

推荐文档