求教,腾讯笔试最小公倍数,为什么会超时。。
from functools import reduce def gcd(a, b): r = a % b if r: return gcd(b, r) else: return b def lcm(a, b): return a * b // gcd(a, b) def solve(n): m = n + 1 res1 = 1 res2 = reduce(lcm, [i for i in range(1, m+1)]) while True: if lcm(res1, m) == lcm(res2, m): return m res1 = lcm(res1, m) res2 = lcm(res2, m) m += 1 n=int(input()) print(solve(n))
#腾讯##笔试题目#