-1

此功能是我解决 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

当 i 变为 5040 时,任何尝试访问 storedSums[i] 都会导致索引超出范围错误。

4

1 回答 1

3

我认为问题出在这里:

storedSums.append(som) if som < 10000 else 0

如果 som < 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)
于 2013-02-22T13:04:59.867 回答