第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

尋找完美數(shù)的Python算法

尋找完美數(shù)的Python算法

梵蒂岡之花 2021-07-22 18:15:01
請(qǐng)您幫忙更正此代碼!這是為了找到低于 10,000 的設(shè)定限制的所有完美數(shù)字,我用評(píng)論來(lái)解釋我在做什么。謝謝!#Stores list of factors and first perfect numberfacs = []x = 1#Defines function for finding factorsdef 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 loopwhile 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
查看完整描述

3 回答

?
守著星空守著你

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超8個(gè)贊

在 def 中初始化列表并返回,并且范圍不應(yīng)包括原始num范圍,因此范圍將從 1 到 num,其中包括 1 但不包括原始num范圍,因此它將生成范圍從1到num-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

比從Python 中查找數(shù)字的所有因子的最有效方法是什么生成列表方法要快得多?


#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


查看完整回答
反對(duì) 回復(fù) 2021-07-28
?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

這是您的邏輯的更簡(jiǎn)單實(shí)現(xiàn)。


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


查看完整回答
反對(duì) 回復(fù) 2021-07-28
  • 3 回答
  • 0 關(guān)注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)