代码:
# 最小公倍数 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)))
声明:本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果用户发布的作品侵犯了您的权利,请联系管理员:wupeng@hqwx.com
环球青藤
官方QQ群扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群