寻找完美数的Python算法

请您帮忙更正此代码!这是为了找到低于 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


梵蒂冈之花
浏览 275回答 3
3回答

守着星空守着你

在 def 中初始化列表并返回,并且范围不应包括原始num范围,因此范围将从 1 到 num,其中包括 1 但不包括原始num范围,因此它将生成范围从1到num-1#Stores list of factors and first perfect numberx = 1#Defines function for finding factorsdef findfactors(num):&nbsp; &nbsp; facs = []&nbsp; &nbsp; for i in range(1, num):&nbsp; &nbsp; &nbsp; &nbsp; if num % i == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #Stores factor in facs&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; facs.append(i)&nbsp; &nbsp; return facs#Activates loopwhile x < 1000:&nbsp; &nbsp; #Finds factors of x and appends them to list&nbsp; &nbsp; facs=findfactors(x)&nbsp; &nbsp; #Finds sum of list&nbsp; &nbsp; facsum = sum(facs)&nbsp; &nbsp; #Makes decision based on&nbsp; sum of factors and original number&nbsp; &nbsp; if facsum == x:&nbsp; &nbsp; &nbsp; &nbsp; #Ouputs and increases x&nbsp; &nbsp; &nbsp; &nbsp; print(x)&nbsp; &nbsp; x+= 1比从Python 中查找数字的所有因子的最有效方法是什么生成列表方法要快得多?#Stores list of factors and first perfect numberx = 1#Defines function for finding factorsfrom functools import reducedef factors(n):&nbsp; &nbsp; return set(reduce(list.__add__,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))#Activates loopwhile x < 10000:&nbsp; &nbsp; #Finds factors of x and appends them to list&nbsp; &nbsp; facs=factors(x)&nbsp; &nbsp; facs.remove(x) # remove original number as it is not required further&nbsp; &nbsp; #Finds sum of list&nbsp; &nbsp; facsum = sum(facs)&nbsp; &nbsp; #Makes decision based on&nbsp; sum of factors and original number&nbsp; &nbsp; if facsum == x:&nbsp; &nbsp; &nbsp; &nbsp; #Ouputs and increases x&nbsp; &nbsp; &nbsp; &nbsp; print(x)&nbsp; &nbsp; x+= 1

长风秋雁

这是您的逻辑的更简单实现。x = 1#Defines function for finding factorsdef isperfectnum(num):sum=0&nbsp; &nbsp; #Creates for loop to find all factors of num&nbsp; &nbsp; for i in range(1, num ):&nbsp; &nbsp; &nbsp; &nbsp; if num % i == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sum=sum+i&nbsp; &nbsp; if sum==num:&nbsp; &nbsp; &nbsp; &nbsp; return TRUE&nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return FALSE#Activates loopwhile x < 10000:&nbsp; &nbsp; #Finds perfect numbers&nbsp; &nbsp; if isperfectnum(x):&nbsp; &nbsp; &nbsp; &nbsp;print(x)&nbsp; &nbsp; x=x+1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python