-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.py
55 lines (42 loc) · 1.1 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
from typing import Optional, List
# Recursive function
def fib_rec(n: int) -> int:
# base case
if n == 0 or n == 1:
return n
return fib_rec(n - 1) + fib_rec(n - 2)
# Dynamic function
def fib_dyn(n: int) -> int:
# base case
if n == 0 or n == 1:
return n
# Instantiate Cache information
cache_size = 10
cache: List[Optional[int]]
cache = [None] * (cache_size + 1)
cache[0] = 0
cache[1] = 1
current_num = 2
while current_num < n:
cache[current_num % cache_size] = (
cache[(current_num - 1) % cache_size]
+ cache[(current_num - 2) % cache_size]
)
current_num += 1
return cache[(n - 1) % cache_size] + cache[(n - 2) % cache_size]
# Iterative function
def fib_iter(n: int) -> int:
# base case
if n == 0 or n == 1:
return n
phi = (1 + 5 ** 0.5) / 2
psi = 1 - phi
return round((phi ** n - psi ** n) / 5 ** 0.5)
def fib_iter2(n: int) -> int:
# Set starting point
a = 0
b = 1
# Follow algorithm
for _ in range(n):
a, b = b, a + b
return a