-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfib.py
86 lines (57 loc) · 1.29 KB
/
fib.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
def simple_fib(n):
a = 0
b = 1
for i in range(n):
c = a + b
a = b
b = c
return c
def iter_fib(n):
a, b = 0, 1
for i in range(n-1):
a, b = b, a+b
return b
def fib_series(n):
a, b, = 0, 1
series = [0, 1]
if n == 0:
return [0]
for i in range(n-1):
a, b = b, a+b
series.append(b)
return series
def recursive_fib(n):
if n in (0, 1): return n
return recursive_fib(n-1) + recursive_fib(n-2)
stored = {
'0' : 0,
'1' : 1
}
def memoized_fib1(n):
if str(n) in stored: return n
if str(n-1) in stored:
a = stored[str(n-1)]
else:
a = memoized_fib(n-1)
stored[str(n-1)] = a
b = stored[str(n-2)]
return a + b
stored = [0,1]
def memoized_fib2(n):
if n < len(stored): return stored[n]
if n-1 <= len(stored):
a = stored[n-1]
else:
a = memoized_fib2(n-1)
stored.insert[n-1, a]
b = stored[n-2]
return a + b
if __name__ == "__main__":
n = 60
print "n =", n
print fib_series(n)
#print "Simple fib:", simple_fib(n)
print "Explicit iteration:", iter_fib(n)
#print "Recursive fib:", recursive_fib(n)
print "Memoized fib:", memoized_fib2(n)