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

相关推荐
梁正雄14 分钟前
2、Python流程控制
开发语言·python
Eric.Lee20211 小时前
ubuntu 安装 Miniconda
linux·运维·python·ubuntu·miniconda
无心水1 小时前
【Python实战进阶】1、Python高手养成指南:四阶段突破法从入门到架构师
开发语言·python·django·matplotlib·gil·python实战进阶·python工程化实战进阶
李剑一2 小时前
Python学习笔记1
python
Salt_07284 小时前
DAY 19 数组的常见操作和形状
人工智能·python·机器学习
无心水4 小时前
【Python实战进阶】2、Jupyter Notebook终极指南:为什么说不会Jupyter就等于不会Python?
python·jupyter·信息可视化·binder·google colab·python实战进阶·python工程化实战进阶
上班日常摸鱼5 小时前
Shell脚本基础教程:变量、条件判断、循环、函数实战(附案例)
python
无心水6 小时前
【Python实战进阶】5、Python字符串终极指南:从基础到高性能处理的完整秘籍
开发语言·网络·python·字符串·unicode·python实战进阶·python工业化实战进阶
2301_807583236 小时前
了解python,并编写第一个程序,常见的bug
linux·python
小白学大数据6 小时前
构建混合爬虫:何时使用Requests,何时切换至Selenium处理请求头?
爬虫·python·selenium·测试工具