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

Alan Gauld alan.gauld at btinternet.com
Tue Jul 29 02:18:53 CEST 2008


"Alan Gauld" <alan.gauld at btinternet.com> wrote

> were infinite using floats! So you need to calculate the
> total as you go without saving the values
> 

I got curious so wrote the following function:

>>> def fibtot(N):
...   f0,f1,tot = 0,1,1
...   for n in range(N):
...     f = f0 + f1
...     f0,f1 = f1,f
...     if n % 2 == 0: tot += f
...   return tot

and here are the results...

>>> len(str(fibtot(10000)))
2090
>>> len(str(fibtot(100000)))
20899
>>> len(str(fibtot(200000)))
41798
>>> len(str(fibtot(400000)))
83595
>>> len(str(fibtot(500000)))
104494
>>> len(str(fibtot(600000)))
125393
>>> len(str(fibtot(800000)))
167190
>>> len(str(fibtot(1000000)))
208988
>>>

So the final total contains nearly 209 thousand digits!
Thats a very, very big number... and it took 5 minutes 
to compute on my PC (Dual core 2.8GHz with 2G RAM)

Now I really must go to bed :-)

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080729/e87468ad/attachment.htm>


More information about the Tutor mailing list