Strommer
Strommer

Reputation: 363

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

Link : 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. Any help appreciated.

Actual answer = 4179871. My answer = 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))

Upvotes: 2

Views: 431

Answers (1)

falsetru
falsetru

Reputation: 369064

factors(4) should give [1,2], but yield [1]. Fix your factors function.

Upvotes: 4

Related Questions