python math.gcd()方法,获取最大公约数

math.gcd()方法

python math module中的gcd()方法,可以获取参数指定的两个数的最大公约数。


gcd()语法

gcd(x, y, /)

python源码中对gcd()方法的介绍:

    greatest common divisor of x and y

参数

参数描述
xpython int类型
ypython int类型

返回值

参数x和y的最大公约数,python int整数类型值。


math.gcd()实例代码

>>> import math
>>> math.gcd(6,8)
2
>>> math.gcd(15,25)
5
>>> math.gcd(1.5,2.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object cannot be interpreted as an integer
>>> math.gcd('30','60')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer

全栈后端 / Python库 :









Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.