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

文章目录

  • 前言
  • 1.简单版
  • 2.函数封装版

前言

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

1.简单版

a = int(input('Enter a number:'))
b = int(input('Enter a number:'))
t = a % b
while t != 0:a = bb = tt = a % b
print(b)

2.函数封装版

def Factor(a,b):t = a % bwhile t != 0:a = bb = tt = a % breturn 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