|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 猪猪虾 于 2020-3-17 13:31 编辑
#编写一个函数,利用欧几里得算法求最大公约数,
#例如gcd(x, y)返回值为参数x和参数y的最大公约数。
def judge(m,n):
if m>n:
yu_shu=m % n #确保用较大的数除以较小的数
else:
yushu=n % m
if yu_shu==0:
return(m)
if yu_shu!=0:
time=0
whlie yu_shu_new==0 or time>=10: #设置循环10次是因为一般情况下,如果没有公约数,到不了10次就能判断出来,我手算了好几个例子发现的
yu_shu_new=n%yu_shu
if yu_shu_new==0:
return(yu_shu)
break
else:
n=yu_shu #赋值保证循环进行,直到找到余数为0
yu_shu=yu_shu_new
time+=1
return(n)
number1=int(input('enter a number:'))
number1=int(input('enter a number:'))
result=judge(number1,number1)
print('result=',result)
看你的代码:
1,第3行和第5行,一个有下划线,一个没有
2,while拼写错误
|
|