Fibonacci Sequence and Long numbers.

Will Ware wware at world.std.com
Thu Oct 5 23:23:46 EDT 2000


Neil Macneale (macneale at cats.ucsc.edu) wrote:
> I am using MacPython 1.5.2 on a G3 powerbook,  and I am getting a 
> strange result from a fibonachi function.

You might have better luck with a dictionary than a list, something
like this:

cache = { }
cache[0] = 0L
cache[1] = 1L

def fib(i):
    try: return cache[i]
    except KeyError: pass
    cache[i] = fib(i-1) + fib(i-2)
    return cache[i]

print fib(1000)

Oddly, when I try fib(10000) on a Linux box (Red Hat 6.2), I get a
segmentation fault and core dump. That's a pretty rare thing to get
from Python, it might suggest a fault in the long-integer arithmetic
module. Never having looked at it, and having no idea whose toes I'm
stepping on by suggesting this possibility, I'll now duck back into
the woodwork.

-- 
# - - - - - - - - - - - - - - - - - - - - - - - -
# Resistance is futile. Capacitance is efficacious.
# Will Ware	email:    wware @ world.std.com



More information about the Python-list mailing list