我被要求在 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)
当我运行它时,什么都没有出现。我知道可能会有一个非常微不足道的错误,但是我花了一整天的时间寻找它,却一无所获。请帮忙。