欧拉问题21,为什么我的解决方案中会有多余的数字? [英] Euler problem 21, why are there extra numbers in my solution?
问题描述
以下是我的代码,用于解决 Euler#21,评估所有10000以下的友好数字之和.
The following is my code to solve Euler #21, Evaluate the sum of all the amicable numbers under 10000.
def divisor_sum(x):
divisor_sum=0
for i in range(1,x):
if x%i==0:
divisor_sum = divisor_sum + i
return(divisor_sum)
for a in range (10000):
if divisor_sum(divisor_sum(a)) == a:
print(a)
响应为:
0
6
28
220
284
496
1184
1210
2620
2924
5020
5564
6232
6368
8128
我知道它想要总和,但是为了检查我的代码,我运行了要求和的数字,因为总和是错误的.
I know it wants the sums, but in order to check my code I ran what numbers it was summing since the sum was wrong.
The odd thing is that when I look online all of the numbers that should be here are found except 0, 6, 28, 496, 8128.
这是怎么回事?
推荐答案
好像您的程序正在找到完美数字以及友好数字:0、6、28、496和8128是前五个完美数字.请记住,友好数字是成对出现的.前五个完美数字没有,并且根据定义,不能有匹配的对,这会使它们成为友好的数字.
Looks like your program is finding perfect numbers as well as amicable numbers: 0, 6, 28, 496, and 8128 are the first five perfect numbers. Remember that amicable numbers come in pairs; the first five perfect numbers do not and by definition cannot have a matching pair that would make them an amicable number.
根据定义,一个完美数字的除数将求和回到完美数字,这意味着divisor_sum(divisor_sum(6))
== divisor_sum(6)
By definition, a perfect number's divisors will sum back to the perfect number, meaning divisor_sum(divisor_sum(6))
== divisor_sum(6)
正如评论者在我撰写本文时所指出的那样,您需要类似的内容:
As commenters pointed out as I was writing this, you need something like:
if divisor_sum(a)!=a
这篇关于欧拉问题21,为什么我的解决方案中会有多余的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!