Faster Recursive Fibonacci Numbers

Gregory Ewing greg.ewing at canterbury.ac.nz
Sat May 21 04:49:33 EDT 2011


Chris Angelico wrote:
> It seems
> strange to smoothly slide from native integer to long integer and just
> keep on going, and yet to be unable to do the same if there's a
> fractional part on it.

The trouble is that if you always compute exact results
by default, the number of digits required can blow up
very quickly.

There's a story that ABC (the predecessor to Python)
calculated everything using rationals. People found that
sometimes a calculation would take an unexpectedly long
time, and it turned out to be because internally it was
creating fractions with huge numerators and denominators.

As a consequence, Guido decided that Python would *not*
use rationals by default.

The same problem doesn't arise with ints, because the
only time you get an int with a large number of digits
is when you genuinely need a large int. So expanding
ints automatically to however many digits is needed
doesn't do any harm.

In Python 2.6 and later, there's a fractions module for
when you need it.

-- 
Greg



More information about the Python-list mailing list