[Tutor] Memory error - how to manage large data sets?

Daniel Sarmiento dsarmientos at gmail.com
Tue Jul 29 05:48:35 CEST 2008


>(the solution, of course, is to avoid storing all those numbers in the
>first place)

I tried this:

fib = {0:0,1:1}
sum = 0

for j in xrange (2,1000000):
    i = fib[j-1] + fib[j-2]
    if i % 2 == 0:
        sum += i
    fib = {j-1:fib[j-1], j:i}

print sum

I guess it should come up with the right answer (i compared sum to the
total value for small values in the range and they were the same)

Just storing the value of the sum doesn't use that much memory and
prints the sum for (2, 1 million) but it takes a long time to compute.
I know there has to be a way better solution.


More information about the Tutor mailing list