网站首页 > 基础教程 正文
gcd()方法
gcd(最大公约数)是找到最大数的数学表达式,该方法可以将必须找到gcd的两个数相除,而所得余数为零.
Python在math模块中具有内置的gcd函数,可以实现它.
gcd()方法:
它接受两个整数作为参数,并返回作为gcd值的整数。
语法:
gcd(x,y) #其中x和y是正整数。
下面,我们直接来用代码实现一下:
import math
print ("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 15
GCD of 0 and 12 is 12
GCD of 0 and 0 is 0
GCD of -24 and -18 is 6
另外,我们可以把里面的数字改变一下,看会得出什么结果,
快动手试试吧!
你学会了吗?
欢迎大家在留言区留言,一起讨论学习,
谢谢关注!
猜你喜欢
- 2024-10-30 30 个 Python常用的语法汇总! python常用方法大全
- 2024-10-30 Python解小学奥数的数学题 python算数学题
- 2024-10-30 浅学Python | 案列分析(下) python案例分析
- 2024-10-30 30个常用Python实现,新手必备! python的妙用
- 2024-10-30 Python基础教程:Day04-循环结构 python循环结构有哪些
- 2024-10-30 结了30个Python常用基础语法,希望对你们有帮助!
- 2024-10-30 「LeetCode算法精讲」计算小于n的质数数量(Python)
- 2024-10-30 Python基础教程:Day06-函数和模块的使用
- 2024-10-30 Python 100天从新手到大师 | Day 6 函数和模块使用
- 2024-10-30 初学Python最简易入门之十六小程序集锦
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)