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

相关推荐
LongJ_Sir23 分钟前
视觉识别算法demo
python
智算菩萨29 分钟前
【论文复现】ML-MLM:基于PyTorch的多标签极小学习机完整复现教程(附GPT-5.4辅助科研提示词工程)
人工智能·pytorch·python·gpt·深度学习·论文笔记
Vertira35 分钟前
torch.eq、torch.ne、torch.gt、torch.lt、torch.ge、torch.le 等函数详解
pytorch·python·深度学习
高频交易dragon1 小时前
claude实现缠论(买卖点)
大数据·python
Hello.Reader1 小时前
Spark 4.0 新特性Python Data Source API 快速上手
python·ajax·spark
王小义笔记1 小时前
大模型微调步骤与精髓总结
python·大模型·llm
源码之家2 小时前
计算机毕业设计:Python汽车销量数据采集分析可视化系统 Flask框架 requests爬虫 可视化 车辆 大数据 机器学习 hadoop(建议收藏)✅
大数据·爬虫·python·django·flask·课程设计·美食
Roselind_Yi2 小时前
【吴恩达2026 Agentic AI】面试向+项目实战(含面试题+项目案例)-2
人工智能·python·机器学习·面试·职场和发展·langchain·agent
2401_827499992 小时前
python核心语法01-数据存储与运算
java·数据结构·python
一直会游泳的小猫2 小时前
ClaudeCode完整学习指南
python·ai编程·claude code·claude code指南