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

相关推荐
程序员龙叔9 小时前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户83562907805112 小时前
使用 Python 操作 Word 内容控件
后端·python
码云骑士14 小时前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
闵孚龙14 小时前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python
goldenrolan14 小时前
A公司物料替代测试系统 v1.7:从需求到 exe/apk 的 AI 辅助全链路实践
android·自动化测试·软件测试·python·ai
菜板春14 小时前
jupyter入门-手册-特征探索
python·jupyter
Metaphor69215 小时前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
极光代码工作室15 小时前
基于数据仓库的电商数据分析平台
大数据·hadoop·python·spark·数据可视化
开发小能手-roy15 小时前
StringBuilder vs StringBuffer:2024年还需要线程安全字符串吗?
开发语言·python·安全