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

Kepala Pening kepalapening at gmail.com
Thu Jul 31 04:19:56 CEST 2008


def sumEvenFibonacci( limit ):
	a, b = 1, 1  # don't waste with a = 0
	sum = 0
	while b < limit:
		if b%2 == 0: sum += b
		a, b = b, a + b
	return sum

print sumEvenFibonacci( 2000000 )



----- Original Message -----
From: Chris Fuller <cfuller084 at thinkingplanet.net>
To: tutor at python.org
Date: Mon, 28 Jul 2008 12:27:58 -0500
Subject: Re: [Tutor] Memory error - how to manage large data sets?

> 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