python 最小公倍数

时间:2022-06-02 09:19:02
最小公倍数

    求解两个整数(不能是负数)的最小公倍数
方法一:穷举法
 def LCM(m, n):

     if m*n == 0:
return 0
if m > n:
lcm = m
else:
lcm = n while lcm%m or lcm%n:
lcm += 1 return lcm

方式二:公式lcm = a*b/gcd(a, b)

 def gcd(m,n):

     if not n:
return m
else:
return gcd(n, m%n) def LCM(m, n): if m*n == 0:
return 0
return int(m*n/gcd(m, n)) if __name__ == '__main__':
a = int(input('Please input the first integers : '))
b = int(input('Please input the second integers : '))
result = LCM(a, b)
print('lcm = ', result)