龙空技术网

一日一技:Python中的gcd()方法

Python编程之美 161

前言:

目前你们对“python最大公约数函数gcd”都比较关切,大家都需要剖析一些“python最大公约数函数gcd”的相关知识。那么小编在网摘上收集了一些有关“python最大公约数函数gcd””的相关文章,希望同学们能喜欢,朋友们一起来了解一下吧!

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

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

快动手试试吧!

你学会了吗?

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

谢谢关注!

标签: #python最大公约数函数gcd #python mathgcd