where the function has problem? n = 900 is OK , but n = 1000 is ERROR
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Aug 1 05:19:30 EDT 2011
jc wrote:
> # Get Fibonacci Value
> # Fibonacci(N) = Fibonacci(N-1) + Fibonacci(N-2)
> #
> # n = 900 is OK
> # n = 1000 is ERROR , Why
How should we know? Please tell us what the error is, don't expect us to
guess.
[...]
> if __name__ == '__main__':
> # This n = 900 is OK
> # But n = 1000 is ERROR
Hint: how big is the cache? If the cache has 900 items, what makes you think
you can look up the 1000th item?
> n = 900
> cache = range(0 , n + 1 , 1)
> for i in cache:
> cache[i] = -1
This is a waste of time. Better to write:
cache = [-1]*900
Even better is to use a dict instead of a list.
--
Steven
More information about the Python-list
mailing list