最大公约数和最小公倍数的概念大家都很熟悉了,在这里就不多说了,今天这个是因为做题的时候遇到了所以就写下来作为记录,也希望帮到别人,下面是代码:
#!/usr/bin/env python
#coding:utf-8
from fractions import gcd
#非递归实现
def gcd_test_one(a, b):
if a!=0 and b!=0:
if a>b:
a, b=b, a
if b%a==0:
return a
gcd_list=[]
for i in range(1,a):
if b%i==0 and a%i==0:
gcd_list.append(i)
return max(gcd_list)
else:
print 'Number is wrong!!!'
#递归实现
def gcd_test_two(a, b):
if a>b:
a, b=b, a
if b%a==0:
return a
else:
return gcd_test_two(a,b%a)
#python自带的gcd
def gcd_test_three(a, b):
return gcd(a,b)
if __name__ == '__main__':
print gcd_test_one(12,24)
print gcd_test_one(12,8)
print gcd_test_one(6,24)
print gcd_test_one(0,24)
print '----------------------------------------------------------------------------'
print gcd_test_two(12,24)
print gcd_test_two(12,8)
print gcd_test_two(6,32)
print '----------------------------------------------------------------------------'
print gcd_test_three(12,24)
print gcd_test_three(12,8)
结果如下:
12
4
6
Number is wrong!!!
None
----------------------------------------------------------------------------
12
4
2
----------------------------------------------------------------------------
12
4