day5 找出10000以内的完美数有问题#752 - GitHub

文章推薦指數: 80 %
投票人數:10人

运行后,出现1,但是根据完美数的定义,是除了自身以外的因子的和恰好等于他本身,那么就不能出现1.下面是我写的,就是很耗资源。

Skiptocontent {{message}} jackfrued / Python-100-Days Public Notifications Fork 44.8k Star 116k Code Issues 450 Pullrequests 176 Actions Projects 0 Wiki Security Insights More Code Issues Pullrequests Actions Projects Wiki Security Insights Newissue Haveaquestionaboutthisproject?SignupforafreeGitHubaccounttoopenanissueandcontactitsmaintainersandthecommunity. Pickausername EmailAddress Password SignupforGitHub Byclicking“SignupforGitHub”,youagreetoourtermsofserviceand privacystatement.We’lloccasionallysendyouaccountrelatedemails. AlreadyonGitHub? Signin toyouraccount Jumptobottom day5找出10000以内的完美数有问题 #752 Open ledwin89openedthisissue Feb25,2021 ·3comments Open day5找出10000以内的完美数有问题 #752 ledwin89openedthisissue Feb25,2021 ·3comments Comments Copylink ledwin89 commented Feb25,2021 运行后,出现1,但是根据完美数的定义,是除了自身以外的因子的和恰好等于他本身,那么就不能出现1.下面是我写的,就是很耗资源。







` defperfect_num(): num_list=[] foriinrange(1,10001): i_list=[] forjinrange(1,i): ifi%j==0: i_list.append(j) ifsum(i_list)==i: num_list.append(i) print(num_list)` Thetextwasupdatedsuccessfully,buttheseerrorswereencountered: Copylink kjlkai commented Feb25,2021 • edited 我试了一下,发现是不是你这个 ifsum(i_list)==i: num_list.append(i) 放的不对,应该是在上一个if的循环里面啊? Sorry,somethingwentwrong. Copylink Author ledwin89 commented Feb26,2021 额,我的意思是作者code里的示例有问题--!作者code中的day05,perfect.py执行后,打印出来的数字是1、6、28、496、8128,但是根据完美数的定义“完美数是除自身外其他所有因子的和正好等于这个数本身的数,例如:6=1+2+3,28=1+2+4+7+14”,那么1应该不是完美数吧,所以作者的code里有点瑕疵。

上面我写的那个是可以跑出来的,打印后应该是正确的== Sorry,somethingwentwrong. Copylink taseikyo commented Apr28,2021 运行后,出现1,但是根据完美数的定义,是除了自身以外的因子的和恰好等于他本身,那么就不能出现1.下面是我写的,就是很耗资源。







` defperfect_num(): num_list=[] foriinrange(1,10001): i_list=[] forjinrange(1,i): ifi%j==0: i_list.append(j) ifsum(i_list)==i: num_list.append(i) print(num_list)` 在作者给出的代码基础上加个判断就完了没必要再加个i_list列表增加内存占用吧 fornuminrange(1,10000): result=0 forfactorinrange(1,int(math.sqrt(num))+1): ifnum%factor==0andnum!=factor: result+=factor iffactor>1andnum//factor!=factor: result+=num//factor ifresult==num: print(num) Sorry,somethingwentwrong. Signupforfree tojointhisconversationonGitHub. Alreadyhaveanaccount? Signintocomment Assignees Nooneassigned Labels Noneyet Projects Noneyet Milestone Nomilestone Linkedpullrequests Successfullymergingapullrequestmayclosethisissue. Noneyet 3participants Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?