In [1]:
def fib(n):
    if n == 1 or n == 2:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)
In [2]:
fib(1), fib(2), fib(3), fib(4), fib(5), fib(10)
Out[2]:
(1, 1, 2, 3, 5, 55)
In [3]:
for i in (1, 2, 3, 4, 5, 10):
    print(i, fib(i))
1 1
2 1
3 2
4 3
5 5
10 55
In [4]:
for i in range(1, 11):
    print(i, fib(i))
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
9 34
10 55
In [5]:
range(1, 11)
Out[5]:
range(1, 11)
In [6]:
tuple(range(1, 11))
Out[6]:
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
In [7]:
%time fib(10)
CPU times: user 51 µs, sys: 8 µs, total: 59 µs
Wall time: 76.1 µs
Out[7]:
55
In [8]:
%time fib(20)
CPU times: user 3.7 ms, sys: 269 µs, total: 3.97 ms
Wall time: 3.86 ms
Out[8]:
6765
In [9]:
%time fib(30)
CPU times: user 388 ms, sys: 12 ms, total: 400 ms
Wall time: 445 ms
Out[9]:
832040
In [10]:
memo = {1 : 1, 2 : 1}
def fib_memo(n):
    if n not in memo:
        memo[n] = fib_memo(n - 1) + fib_memo(n - 2)
    return memo[n]
%time fib_memo(30)
CPU times: user 24 µs, sys: 0 ns, total: 24 µs
Wall time: 27.2 µs
Out[10]:
832040
In [11]:
%prun fib(10)