python中最小公倍数怎么求?
816次观看
标签:
最小公倍数
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 {}'.format(*case, lcm(*case)))
def lcm(a, b): for i in range(2, min(a,b)+1): if a % i == 0 and b % i == 0: return i * lcm(a//i, b//i) else: return a*b test_cases = [(4, 8), (5, 7), (24, 16), (35, 42)] for case in test_cases: print('lcm of {} & {} is {}'.format(*case, lcm(*case)))
©本文版权归环球青藤所有,任何形式转载请联系我们。
免费直播
精选课程
相关推荐