【发布时间】:2019-02-02 03:49:33
【问题描述】:
我被要求在 6 之后找到三个连续的完美数字(即因数(包括 1 和排除自身)相加为自身的数字)。 这是我的尝试:
# Find three consecutive perfect numbers after 6
def f(x):
"Find the sum of all factors."
factors = []
for i in range (1,x-1):
if x%i == 0:
factors.append (i)
else:
pass
return sum(factors)
counts = 0
perfect_numbers = []
x = 6
while counts <= 2:
x += 1
if x == f(x):
perfect_numbers.append (x)
counts += 1
else:
pass
print(perfect_numbers)
当我运行它时,什么都没有出现。 我知道可能会有一个非常微不足道的错误,但是我花了一整天的时间寻找它,却一无所获。请帮忙。
【问题讨论】:
-
这看起来是学习使用调试器的绝佳机会。
-
我运行了你的代码,几秒钟后它给出了这个:[28, 496, 8128]
-
我真的不知道该说什么,执行代码只是给了我接下来的3个完美数字
-
代码正在运行并提供输出
-
那么看起来你的问题与你如何运行代码有关。你能描述一下你是怎么做的吗?