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

Chris Fuller cfuller084 at thinkingplanet.net
Mon Jul 28 19:27:58 CEST 2008


On Monday 28 July 2008 10:56, Karthik wrote:
> Hi,
>
>
>
> I am new to Python programming, I was trying to work out a few problems in
> order to grasp the knowledge gained after going through the basic chapters
> on Python programming. I got stuck with a memory error.
>
>
>
> Following is what I did,
>
>
>
> 1.     I need to find the sum of all numbers at even positions in the
> Fibonacci series upto 2 million.
>
> 2.     I have used lists to achieve this.
>
> 3.     My program works good with smaller ranges. Say till 10,000 or even
> 100,000. However when I compute the sum for bigger ranges it gives me the
> memory error.
>
> 4.     Also could someone tell me how to get the result in the form of an
> exponent. For instance, I would prefer 10^5 rather  100000.
>
>
>
> Thanks in advance,
>
> Karthik

It sounds like you are storing all the fibonacci numbers as you generate them.  
Why?  You only need the previous two to find the next in the sequence.  The 
sum is a single number that you can add every other element in the sequence 
to.  You only need to store three numbers in memory.  Storing millions is 
wasteful, and doesn't scale very well.

To find an exponent, use the "**" operator.  For instance, 2**3 is 8, and 3**2 
is 9.

Cheers


More information about the Tutor mailing list