Python实用代码之:如何找两个数的最大公因数?

文章目录

前言

大家好,我是BoBo仔吖,欢迎来看我的文章!这节课,我教大家如何用两种方法输出最大公因数------简单版以及函数版

1.简单版

python 复制代码
a = int(input('Enter a number:'))
b = int(input('Enter a number:'))
t = a % b
while t != 0:
    a = b
    b = t
    t = a % b
print(b)

2.函数封装版

python 复制代码
def Factor(a,b):
	t = a % b
	while t != 0:
    	a = b
    	b = t
    	t = a % b
	return b
a = int(input('Enter a number:'))
b = int(input('Enter a number:'))
print(Factor(a,b))

OK,这就是两个版本的最大公因数输出方式了。

如果想了解更多,欢迎阅读我的文章!链接如下:

https://editor.csdn.net/md/?articleId=136095045#1Commonab_115

相关推荐
老师好,我是刘同学23 分钟前
Python执行命令并保存输出到文件
python
啵啵鱼爱吃小猫咪2 小时前
机械臂阻抗控制github项目-mujoco仿真
开发语言·人工智能·python·机器人
MaximusCoder2 小时前
等保测评命令——Centos Linux
linux·运维·经验分享·python·安全·centos
yunyun321232 小时前
用Python生成艺术:分形与算法绘图
jvm·数据库·python
m0_662577972 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
songyuc3 小时前
【PyTorch】感觉`CrossEntropyLoss`和`BCELoss`很类似,为什么它们接收labels的shape常常不一样呢?
人工智能·pytorch·python
ℳ๓₯㎕.空城旧梦3 小时前
Python单元测试(unittest)实战指南
jvm·数据库·python
浩子智控4 小时前
python程序打包的文件地址处理
开发语言·python·pyqt
Jackey_Song_Odd4 小时前
Part 1:Python语言核心 - 序列与容器
开发语言·windows·python
m0_662577974 小时前
Python迭代器(Iterator)揭秘:for循环背后的故事
jvm·数据库·python