此功能是我开发尝试解决Project Euler Problem 21的问题。 评估10000以下所有友好数字的总和。 我正在使用一个模块list_divisors(),我做了一个位于另一个文件中,应该可以正常工作。它应该对出现问题没有影响。
def ami():
storedSums = [0, 0]
amiables = [0, 0]
for i in range(2, 10000):
som = sum(k for k in list_divisors(i))
storedSums.append(som) if som < 10000 else 0
for j in range(2, i - 1):
#this if statement is the test. It does not do anything except print what is happening.
if (i > 5037 and j % 1000 == 0) or i < 10 or (i > 5038 and j > 5020):
print("i = {}, j = {}, storedSums[i] = {}, storedSums[j] = {}, \
len(storedSum) = {}".format(i, j, storedSums[i], storedSums[j], len(storedSums)))
#The code freezes here at 5040
if storedSums[i] == j and storedSums[j] == i:
amiables.extend((i, j))
return sum(i for i in amiables)
print(ami())
我的测试结果:
i = 4, j = 2, storedSums[i] = 3, storedSums[j] = 3, len(storedSum) = 5
i = 5, j = 2, storedSums[i] = 6, storedSums[j] = 3, len(storedSum) = 6
i = 5, j = 3, storedSums[i] = 6, storedSums[j] = 4, len(storedSum) = 6
i = 6, j = 2, storedSums[i] = 6, storedSums[j] = 3, len(storedSum) = 7
i = 6, j = 3, storedSums[i] = 6, storedSums[j] = 4, len(storedSum) = 7
i = 6, j = 4, storedSums[i] = 6, storedSums[j] = 3, len(storedSum) = 7
i = 7, j = 2, storedSums[i] = 8, storedSums[j] = 3, len(storedSum) = 8
i = 7, j = 3, storedSums[i] = 8, storedSums[j] = 4, len(storedSum) = 8
i = 7, j = 4, storedSums[i] = 8, storedSums[j] = 3, len(storedSum) = 8
i = 7, j = 5, storedSums[i] = 8, storedSums[j] = 6, len(storedSum) = 8
i = 8, j = 2, storedSums[i] = 11, storedSums[j] = 3, len(storedSum) = 9
i = 8, j = 3, storedSums[i] = 11, storedSums[j] = 4, len(storedSum) = 9
i = 8, j = 4, storedSums[i] = 11, storedSums[j] = 3, len(storedSum) = 9
i = 8, j = 5, storedSums[i] = 11, storedSums[j] = 6, len(storedSum) = 9
i = 8, j = 6, storedSums[i] = 11, storedSums[j] = 6, len(storedSum) = 9
i = 9, j = 2, storedSums[i] = 4, storedSums[j] = 3, len(storedSum) = 10
i = 9, j = 3, storedSums[i] = 4, storedSums[j] = 4, len(storedSum) = 10
i = 9, j = 4, storedSums[i] = 4, storedSums[j] = 3, len(storedSum) = 10
i = 9, j = 5, storedSums[i] = 4, storedSums[j] = 6, len(storedSum) = 10
i = 9, j = 6, storedSums[i] = 4, storedSums[j] = 6, len(storedSum) = 10
i = 9, j = 7, storedSums[i] = 4, storedSums[j] = 8, len(storedSum) = 10
i = 5038, j = 1000, storedSums[i] = 5052, storedSums[j] = 1140, len(storedSum) = 5039
i = 5038, j = 2000, storedSums[i] = 5052, storedSums[j] = 2236, len(storedSum) = 5039
i = 5038, j = 3000, storedSums[i] = 5052, storedSums[j] = 3860, len(storedSum) = 5039
i = 5038, j = 4000, storedSums[i] = 5052, storedSums[j] = 4268, len(storedSum) = 5039
i = 5038, j = 5000, storedSums[i] = 5052, storedSums[j] = 5140, len(storedSum) = 5039
i = 5039, j = 1000, storedSums[i] = 5040, storedSums[j] = 1140, len(storedSum) = 5040
i = 5039, j = 2000, storedSums[i] = 5040, storedSums[j] = 2236, len(storedSum) = 5040
i = 5039, j = 3000, storedSums[i] = 5040, storedSums[j] = 3860, len(storedSum) = 5040
i = 5039, j = 4000, storedSums[i] = 5040, storedSums[j] = 4268, len(storedSum) = 5040
i = 5039, j = 5000, storedSums[i] = 5040, storedSums[j] = 5140, len(storedSum) = 5040
i = 5039, j = 5021, storedSums[i] = 5040, storedSums[j] = 5022, len(storedSum) = 5040
i = 5039, j = 5022, storedSums[i] = 5040, storedSums[j] = 5110, len(storedSum) = 5040
i = 5039, j = 5023, storedSums[i] = 5040, storedSums[j] = 5024, len(storedSum) = 5040
i = 5039, j = 5024, storedSums[i] = 5040, storedSums[j] = 5071, len(storedSum) = 5040
i = 5039, j = 5025, storedSums[i] = 5040, storedSums[j] = 166, len(storedSum) = 5040
i = 5039, j = 5026, storedSums[i] = 5040, storedSums[j] = 5036, len(storedSum) = 5040
i = 5039, j = 5027, storedSums[i] = 5040, storedSums[j] = 5039, len(storedSum) = 5040
i = 5039, j = 5028, storedSums[i] = 5040, storedSums[j] = 5040, len(storedSum) = 5040
i = 5039, j = 5029, storedSums[i] = 5040, storedSums[j] = 5077, len(storedSum) = 5040
i = 5039, j = 5030, storedSums[i] = 5040, storedSums[j] = 5038, len(storedSum) = 5040
i = 5039, j = 5031, storedSums[i] = 5040, storedSums[j] = 5091, len(storedSum) = 5040
i = 5039, j = 5032, storedSums[i] = 5040, storedSums[j] = 5101, len(storedSum) = 5040
i = 5039, j = 5033, storedSums[i] = 5040, storedSums[j] = 5041, len(storedSum) = 5040
i = 5039, j = 5034, storedSums[i] = 5040, storedSums[j] = 5040, len(storedSum) = 5040
i = 5039, j = 5035, storedSums[i] = 5040, storedSums[j] = 5113, len(storedSum) = 5040
i = 5039, j = 5036, storedSums[i] = 5040, storedSums[j] = 5039, len(storedSum) = 5040
i = 5039, j = 5037, storedSums[i] = 5040, storedSums[j] = 5064, len(storedSum) = 5040
Traceback (most recent call last):
File "<string>", line 420, in run_nodebug
File "F:\Programming\python\project euler\Amicable numbers.py", line 18, in <module>
print(ami())
File "F:\Programming\python\project euler\Amicable numbers.py", line 14, in ami
if storedSums[i] == j and storedSums[j] == i:
IndexError: list index out of range
当我变为5040时,索引超出范围错误来自任何尝试访问storedSums [i]。
答案 0 :(得分:3)
我认为问题在于:
storedSums.append(som) if som < 10000 else 0
如果som是&lt;这只会在列表中添加一些东西。 10000,否则它将无效地执行任何操作,因此您对索引长度的假设是关闭的。
参见此测试程序:
test_list = []
for i in range(10):
test_list.append(i) if i < 5 else 0
print test_list
输出:
[0, 1, 2, 3, 4]
我想你想要这个:
if som < 10000:
storedSums.append(som)
else:
storedSums.append(0)