简体   繁体   English

项目Euler#23-无法找出代码中的错误

[英]Project Euler #23 - Unable to figure out error in code

Link : http://projecteuler.net/problem=23 链接: http//projecteuler.net/problem=23

This is a warning do not look further if you do not want to know the answer. 这是一个警告,如果您不想知道答案,请不要再看了。

I have been stuck for quite some time.I know there are solutions online. 我被困了很长时间,我知道网上有解决方案。 But I am just not able to figure out the problem in my code. 但是我只是无法在我的代码中找出问题所在。 I have attached the code for the code and the factors module that I have created. 我已经附加了我创建的代码和factors模块的代码。 Any help appreciated. 任何帮助表示赞赏。

Actual answer = 4179871. My answer = 4190404 实际答案=4179871。我的答案= 4190404


from math import *
from time import *
from prime import *
from factors import *

abundant = list(n for n in xrange(12,28124) if n < sum_of_factors(n))

sums = {}

for i in abundant: for j in abundant: if (i+j) > 28123: break else: sums[i+j] = 1

non_abun = [i for i in range(1,28124)] print sum(non_abun) - sum(sums)

Code for factors - 因子代码-


from time import *
from math import *

def factors(num): factors_array = [1,]

n = num
for k in range(2,int(ceil(sqrt(num)))):
    if(n%k == 0):
        if(k not in factors_array):
            factors_array.append(k)
            factors_array.append(n/k)

return factors_array

def sum_of_factors(num): return sum(factors(num))

factors(4) should give [1,2] , but yield [1] . factors(4)应该给出[1,2] ,但是产量[1] Fix your factors function. 修正您的factors功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM