简体   繁体   English

将 0 添加到 Project Euler 问题 23 “Non-Abundant Sums” 的列表中会改变答案

[英]Adding a 0 to the list for Project Euler Problem 23 “Non-Abundant Sums” changes the answer

I'm a beginner in programming, and I'm trying to practice Project Euler questions.我是编程初学者,我正在尝试练习 Project Euler 问题。 Question 23:问题 23:

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number.完美数是一个数,其真因数之和正好等于该数。 For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.例如,28 的适当因数之和为 1 + 2 + 4 + 7 + 14 = 28,这意味着 28 是一个完美数。

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.一个数 n 如果其真因数之和小于 n 则称为不足数,如果该数之和超过 n 则称为丰富数。

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers.由于 12 是最小的丰度数,1 + 2 + 3 + 4 + 6 = 16,所以可以写成两个丰度数之和的最小数是 24。通过数学分析可以证明,所有大于28123 可以写成两个丰富数之和。 However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.但是,即使已知不能表示为两个丰富数之和的最大数小于此上限,也无法通过分析进一步降低此上限。

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.找出所有不能写成两个丰富数之和的正整数之和。

I got the correct answer by the following code:我通过以下代码得到了正确答案:

list1=[]
list2=[]
for num in range(1,28123):
  list1.append(num)
for num in list1:
  sum1=1
  for i in range (2,int(num**0.5)+1):
    if not num%i:
      sum1+=(i+(num/i))
  if int(num**0.5)==(num**0.5):
    sum1-=(num**0.5)
  if sum1>num:
    list2.append(num)
for i in range(len(list2)):
  for j in range(i,len(list2)):
    check=list2[i]+list2[j]
    if check<28123:
      list1[check-1]=0
print(sum(list1))

I tried to make a small modification to this code, by changing the definition of list1 by adding a '0' by the following code:我试图通过以下代码添加“0”来更改 list1 的定义,从而对此代码进行小幅修改:

list1=[x for x in range (28123)]
list2=[]
for num in list1:
  sum1=1
  for factor in range (2,int(num**0.5)+1):
    if not num%factor:
      sum1+=(factor+(num/factor))
  if int(num**0.5)==(num**0.5):
    sum1-=(num**0.5)
  if sum1>num:
    list2.append(num)
for i in range(len(list2)):
  for j in range(i,len(list2)):
    check=list2[i]+list2[j]
    if check<28123:
      list1[check]=0
print(sum(list1))

As you can see, I also changed the second last line according the new indexes.如您所见,我还根据新索引更改了倒数第二行。 But this changed my answer from 4179871 to 4178876. I can't understand why.但这将我的答案从 4179871 更改为 4178876。我不明白为什么。 The former is the correct answer.前者是正确答案。 I know my code is not very efficient as I'm a beginner, trying to improve.我知道我的代码效率不高,因为我是初学者,正在努力改进。 Help will be much appreciated!帮助将不胜感激!

Edit:编辑:

I tried to check if 0 is getting added into the abundant number's list2, but the following code gave me [] as an output, hence it wasn't.我试图检查 0 是否被添加到丰富数字的 list2 中,但下面的代码给了我 [] 作为 output,因此它不是。

num = 0
list2 = []
sum1 = 1
for i in range (2, int (num ** 0.5) + 1):
    if not num % i:
        sum1 += (i+ (num / i))
    if int (num ** 0.5) == (num ** 0.5):
        sum1 -= (num ** 0.5)
    if sum1 > num:
        list2.append(num)
print(list2)

You inserted 0;你插入了 0; your code recognizes this as an abundant number.您的代码认为这是一个丰富的数字。 Therefore, any abundant number that was previously not the sum of two others... is suddenly the sum of itself and 0. It's excluded from the list.因此,任何以前不是其他两个之和的丰富数字……突然变成了它自己和 0 的和。它被排除在列表之外。

I strongly recommend that you learn to perform basic logical debugging.我强烈建议您学习执行基本的逻辑调试。 I determined this with a simple insertion.我通过简单的插入确定了这一点。 I pasted both of your algorithms into a file.我将您的两种算法都粘贴到了一个文件中。 In between, I saved the results of the first algorithm:在这两者之间,我保存了第一个算法的结果:

...
print(sum(list1))
keep1 = list1
keep2 = list2

Then, after the second algorithm, I simply found the differences between the lists you formed in the two algorithms:然后,在第二种算法之后,我简单地发现了你在两种算法中形成的列表之间的差异:

print("acquired abundant numbers", set(list2) - set(keep2))
print("lost summed numbers", set(keep1) - set(list1))

Output: Output:

4179871
4178876
acquired abundant numbers {0}
lost summed numbers {945, 18, 12, 20}

... and there is the telling evidence: three numbers less than 24, which is stated as the lowest number qualifying for the summation. ...并且有明显的证据:三个小于 24 的数字,这被称为符合求和条件的最低数字。 Each of these is abundant, and the first added output line shows that your code mis-allocated 0 as abundant.这些中的每一个都很丰富,第一个添加的 output 行显示您的代码错误分配了 0 作为丰富。

This is one of the most basic debugging techniques: when you have a solid difference in expected and actual output, print or otherwise compare the intermediate values to find the point of difference.这是最基本的调试技术之一:当您的预期和实际 output 存在明显差异时,打印或以其他方式比较中间值以找到差异点。

You note that I'm not tracking this into your code: you didn't make it easy to read.您注意到我没有将其跟踪到您的代码中:您没有使其易于阅读。 See the PEP-8 guidelines for suggestions.有关建议,请参阅 PEP-8指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM