我已经编写了这个 python 代码。当我运行它时,最后会出现索引错误;我不知道为什么。我想知道是否有人可以帮助我。
问题是找到能被 1 到 20 的所有数字整除的最小正数。我的分析是正确的,但只是最后的代码让我很难过。
def leastCommonDenominator(num1, num2):
'''
Returns the least common denominator of two numbers
'''
num2List = range(1,num2+1)
if num1 < num2:
for i in num2List:
if (num1*num2List[i]) % num2 == 0:
return num1*num2List[i]
num1List = range(1, num1+1)
if num2 < num1:
for i in num1List:
if (num2*num1List[i]) % num1 == 0:
return num2*num1List[i]
else:
return num1
def leastNum():
'''
Prints the least number divisible
'''
myList = range(1,4)
print myList
num = 1
for i in myList:
num = leastCommonDenominator(num, myList[i])
print num
def main():
leastNum()