本文实例讲述了Python基于递归算法求最小公倍数和最大公约数。分享给大家供大家参考,具体如下:
# 最小公倍数
def lcm(a, b, c=1):
if a * c % b != 0:
return lcm(a, b, c+1)
else:
return a*c
test_cases = [(4, 8), (35, 42), (5, 7), (20, 10)]
for case in test_cases:
print('lcm of {} & {} is {
C++ 实现求最大公约数和最小公倍数
最大公约数
辗转相除法:
int maxDivisor(int a, int b)
{
int c = b;
while (a%b != 0)
{
c = a%b;
a = b;
b = c;
}
return c;
}
辗转相减法:
int maxDivisor(int a, int b)
{
while (a != b)
{
if (a>b) a = a - b;
else