-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfibonacci.py
60 lines (43 loc) · 1.15 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
"""
fibonacci
functions to compute fibonacci numbers
Complete problems 2 and 3 in this file.
"""
import time # to compute runtimes
from tqdm import tqdm # progress bar
# Question 2
def fibonacci_recursive(n):
pass
# Question 2
def fibonacci_iter(n):
pass
# Question 3
def fibonacci_power(n):
pass
if __name__ == '__main__':
"""
this section of the code only executes when
this file is run as a script.
"""
def get_runtimes(ns, f):
"""
get runtimes for fibonacci(n)
e.g.
trecursive = get_runtimes(range(30), fibonacci_recusive)
will get the time to compute each fibonacci number up to 29
using fibonacci_recursive
"""
ts = []
for n in tqdm(ns):
t0 = time.time()
fn = f(n)
t1 = time.time()
ts.append(t1 - t0)
return ts
nrecursive = range(35)
trecursive = get_runtimes(nrecursive, fibonacci_recursive)
niter = range(10000)
titer = get_runtimes(niter, fibonacci_iter)
npower = range(10000)
tpower = get_runtimes(npower, fibonacci_power)
## write your code for problem 4 below...