-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci.py
65 lines (50 loc) · 1.4 KB
/
Fibonacci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Rabbit Production using recursion
# Fibonacci sequence is as follows:
# 1 + 1 = 2
# 1 + 2 = 3
# 2 + 3 = 5
# 3 + 5 = 8
# and so on
# Function must be 1. Fast 2. Clean
def fib(n):
if n == 1:
return 1
elif n == 2:
return 1
elif n > 2:
return fib(n - 1) + fib(n - 2)
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Dictionary Cache
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Since this is a recursive call
# fib(5) -> fib(4) + fib(3)
# fib(4) -> fib(3) + fib(2)
# fib(3) -> fib(2) + fib(1)
#
# And hence same function call is called repeatedly.
# This can be optimized using `Memorization`.
# -> Save the current result
fibonacii_cache = dict()
def fib_mem(n):
# If the value is already present then return it
if n in fibonacii_cache:
return fibonacii_cache[n]
# Compute the Nth term
if n == 1 or n == 2:
value = 1
elif n > 2:
value = fib_mem(n - 1) + fib_mem(n - 2)
# Cache the value and return it
fibonacii_cache[n] = value
return value
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
# LRU cache - Least Recently used cache
# - - - - - - - - - - - - - - - - - - - - - - - - - - -
from functools import lru_cache
@lru_cache(maxsize=1000)
def fib_lru(n):
if n == 1 or n == 2:
return 1
return fib_lru(n - 1) + fib_lru(n - 2)
for i in range(1, 1001):
print(i, " : ", fib(i))