Python 求完美数的算法

Python algorithm for finding perfect numbers

请您帮忙更正此代码!它用于查找低于设定限制 10,000 的所有完美数字,我使用评论来解释我在做什么。谢谢!

#Stores list of factors and first perfect number
facs = []
x = 1

#Defines function for finding factors
def findfactors(num):

    #Creates for loop to find all factors of num
    for i in range(1, num + 1):
        if num % i == 0:

        #Stores factor in facs
        facs.append(i)


#Activates loop
while x < 10000:

    #Finds factors of x and appends them to list
    findfactors(x)

    #Finds sum of list
    facsum = sum(facs)

    #Makes decision based on  sum of factors and original number
    if facsum == x:

        #Ouputs and increases x
        print(x)
        x += 1

    else:

        #Increases x
        x += 1

初始化 def 和 return 中的列表,范围不应包括原始 num,因此范围将从 1 到 num,其中包括 1 但不包括原始 num,因此它将生成从 1num-1

的范围
#Stores list of factors and first perfect number
x = 1
#Defines function for finding factors
def findfactors(num):
    facs = []
    for i in range(1, num):
        if num % i == 0:
            #Stores factor in facs
            facs.append(i)

    return facs

#Activates loop
while x < 1000:

    #Finds factors of x and appends them to list
    facs=findfactors(x)

    #Finds sum of list
    facsum = sum(facs)

    #Makes decision based on  sum of factors and original number
    if facsum == x:
        #Ouputs and increases x
        print(x)
    x+= 1

比生成列表快得多 来自 What is the most efficient way of finding all the factors of a number in Python?

的方法
#Stores list of factors and first perfect number
x = 1
#Defines function for finding factors
from functools import reduce

def factors(n):
    return set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

#Activates loop
while x < 10000:
    #Finds factors of x and appends them to list
    facs=factors(x)
    facs.remove(x) # remove original number as it is not required further

    #Finds sum of list
    facsum = sum(facs)

    #Makes decision based on  sum of factors and original number
    if facsum == x:
        #Ouputs and increases x
        print(x)
    x+= 1

这是您的逻辑的一个更简单的实现。

x = 1

#Defines function for finding factors
def isperfectnum(num):
sum=0
    #Creates for loop to find all factors of num
    for i in range(1, num ):
        if num % i == 0:
           sum=sum+i
    if sum==num:
        return TRUE
    else 
        return FALSE

#Activates loop

while x < 10000:

    #Finds perfect numbers
    if isperfectnum(x):
       print(x)
    x=x+1

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). The first perfect number is 6. The next perfect number is 28. This is followed by the perfect numbers 496 and 8128. (Wikipedia)

您必须从因素列表中排除数字本身。

此外,对于每个 x,您必须从空的 facs 开始,然后附加到它。您不希望该列表中以前的数字因素。以下代码有效。

x = 1

def findfactors(num):
    facs = []
    for i in range(1, num):
        if num % i == 0:
            facs.append(i)
    return facs


while x < 10000:
    facs = findfactors(x)
    facsum = sum(facs)
    if facsum == x:
        print(x)
        x += 1
    else:
        x += 1

找到问题了!

应该是:

for i in range(1, num - 1)

而不是:

for i in range(1, num + 1)

感谢所有贡献者!