[Tutor] Memory error - how to manage large data sets?
Chris Fuller
cfuller084 at thinkingplanet.net
Tue Jul 29 02:18:48 CEST 2008
There's no need to keep any lists. The sum can be done on the fly, which is
perhaps a bit slower, but takes a constant amount of ram. Even storing every
other element (or every third, which is what he's trying to do: the elements
that are even numbers, not every other element.. See his example code, or
Project Euler, problem two, which seems to be the original source) will still
take a lot of ram.
Something like this:
a = 0
b = 1
total = 0
while True:
c = a+b
a = b
b = c
if c>1e6:
break
if c%2 == 0:
total += c
print total
By the way, working through those problems will really exercise your
programming and math skills. It's a great way to get started, if you enjoy
those kind of puzzles.
Can you see why every third element must be even?
Cheers
More information about the Tutor
mailing list