所以我有这个程序:
def gcd(x, y):
while y % x != 0:
y = y % x
x = x % y
return y
def lcm(x, y):
(x * y) / gcd(x, y)
a = lcm(1, 2)
b = lcm(3, a)
c = lcm(b, 4)
d = lcm(5, c)
e = lcm(6, d)
f = lcm(e, 7)
g = lcm(f, 8)
h = lcm(9, g)
i = lcm(h, 10)
j = lcm(11, i)
k = lcm(12, j)
l = lcm(k, 13)
m = lcm(l, 14)
n = lcm(15, m)
o = lcm(n, 16)
p = lcm(17, o)
q = lcm(18, p)
r = lcm(q, 19)
print(lcm(r, 20))
好的,由于这篇文章的答案,我现在已经解决了这个问题,但为什么会这样:
def gcd(x, y):
while y % x != 0:
y = y % x
x = x % y
return y
def lcm(x, y):
return (x * y) / gcd(x, y)
a = lcm(1, 2)
b = lcm(3, a)
c = lcm(b, 4)
d = lcm(5, c)
e = lcm(6, d)
f = lcm(e, 7)
g = lcm(f, 8)
h = lcm(9, g)
i = lcm(h, 10)
j = lcm(11, i)
k = lcm(12, j)
l = lcm(k, 13)
m = lcm(l, 14)
n = lcm(15, m)
o = lcm(n, 16)
p = lcm(17, o)
q = lcm(18, p)
r = lcm(q, 19)
print(lcm(r, 20))
返回这个:
ZeroDivisonError: float modulo
抱怨第二行,我想我可能需要某种if
声明来按顺序排列这两个数字。有人知道为什么会这样吗?
谢谢。