where the function has problem? n = 900 is OK , but n = 1000 is ERROR
jc
chenjii at gmail.com
Mon Aug 1 11:11:16 CEST 2011
# Get Fibonacci Value
# Fibonacci(N) = Fibonacci(N-1) + Fibonacci(N-2)
#
# n = 900 is OK
# n = 1000 is ERROR , Why
#
# What Wrong?
#
cache = []
def fibo( n ):
try:
if cache[n] != -1:
return cache[n]
else:
if 0 == n:
r = 0
elif 1 == n:
r = 1
else:
r = fibo(n-1) + fibo(n-2)
cache[n] = r
return r
except:
print "EXCEPT: " + str(n)
if __name__ == '__main__':
# This n = 900 is OK
# But n = 1000 is ERROR
n = 900
cache = range(0 , n + 1 , 1)
for i in cache:
cache[i] = -1
print "Fibo(" + str(n) + ") = " + str(fibo(n))
print "\n"
print "\n"
More information about the Python-list
mailing list