gcd函数python(gcc)

gcd函数python(gcc)

python

gcd()方法

gcd(最大公约数)是找到最大数的数学表达式,该方法可以将必须找到gcd的两个数相除,而所得余数为零.

Python在math模块中具有内置的gcd函数,可以实现它.

gcd()方法:

它接受两个整数作为参数,并返回作为gcd值的整数。

语法:

gcd(x,y) #其中x和y是正整数。

下面,我们直接来用代码实现一下:

import mathprint ("GCD of 75 and 30 is ",math.gcd(75, 30)) #求最大公约数print ("GCD of 0 and 12 is ",math.gcd(0, 12))print ("GCD of 0 and 0 is ",math.gcd(0, 0))print ("GCD of -24 and -18 is ",math.gcd(-24, -18))

输出:

GCD of 75 and 30 is 15GCD of 0 and 12 is 12GCD of 0 and 0 is 0GCD of -24 and -18 is 6

另外,我们可以把里面的数字改变一下,看会得出什么结果,

快动手试试吧!

你学会了吗?

欢迎大家在留言区留言,一起讨论学习,

谢谢关注!

本文内容源自网友投稿,多成号仅提供信息存储服务不拥有所有权。如有侵权,请联系站长删除。qq97伍4伍0叁11