python - Is there a way to avoid this memory error? -
i'm working through problems on project euler, , far i've come code problem.
from itertools import combinations import time def findanums(n): l = [] in range(1, n + 1): s = [] j in range(1, i): if % j == 0: s.append(j) if sum(s) > i: l.append(i) return l start = time.time() #start time limit = 28123 anums = findanums(limit + 1) #abundant numbers (1..limit) print "done finding abundants", time.time() - start pairs = combinations(anums, 2) print "done finding combinations", time.time() - start sums = map(lambda x: x[0]+x[1], pairs) print "done finding possible sums", time.time() - start print "start main loop" answer = 0 in range(1,limit+1): if not in sums: answer += print "answer:",answer when run run memoryerror.
the traceback:
file "test.py", line 20, in <module> sums = map(lambda x: x[0]+x[1], pairs) i've tried prevent disabling garbage collection i've been able google no avail. approaching wrong way? in head feels natural way , i'm @ loss @ point.
side note: i'm using pypy 2.0 beta2(python 2.7.4) because faster other python implementation i've used, , scipy/numpy on head i'm still beginning program , don't have engineering or strong math background.
as kevin mention in comments, algorithm might wrong, anyway code not optimized.
when using big lists, common use generators, there famous, great stackoverflow answer explaining concepts of yield , generator - what "yield" keyword in python?
the problem pairs = combinations(anums, 2) doesn't generate generator, generates large object more memory consuming.
i changed code have function, since iterating on collection once, can lazy evaluate values:
def generator_sol(anums1, s): comb in itertools.combinations(anums1, s): yield comb now instead of pairs = combinations(anums, 2) generates large object. use:
pairs = generator_sol(anums, 2) then, instead of using lambda use generator:
sums_sol = (x[0]+x[1] x in pairs) another tip, better @ xrange more suitable, doens't generate list xrange object more efficient in many cases (such here).
Comments
Post a Comment