This is an algorithm that I'm trying to write to get the nth fibonacii number using bottom-up dictionary:.How can I make this better/more eficcient?
memo={1:1,2:1}
f=0
def Fib(n):
for i in range(3,n+1):
memo[i]=memo[i-1]+memo[i-2]
if n in memo:f=memo[n]
print(f)
f=memo[n]
return
print(f)
Fib(15)
This is error I get:
Traceback (most recent call last):
File "c:\users*****\documents\visual studio 2015\Projects\HelloPython\HelloPython\HelloPython.py", line 13, in
Fib(15)
File "c:\users(...)\HelloPython.py", line 9, in Fib
f=memo[n]
KeyError: 15
When I print(f) inside de Fib Function it doesn´t throw any error... I'm fairly new to the python language, I came from c#... Is using a list better? How so?