-
Notifications
You must be signed in to change notification settings - Fork 9
/
fibo.py
executable file
·75 lines (53 loc) · 1.52 KB
/
fibo.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
#! usr/bin/python3
#pylint: disable=invalid-name
''' Different implementations of fibonacci sequence
have different performances. '''
import time
def fib1(n):
if n <= 1:
return n
else:
return fib1(n - 1) + fib1(n - 2)
known = {0: 0, 1: 1}
def fib2(n):
if n in known.keys():
return known[n]
res = fib2(n - 1) + fib2(n - 2)
known[n] = res
return res
def fib3(n):
return n < 2 and 1 or fib3(n - 1) + fib3(n - 2)
fib4 = lambda n: n if n < 2 else fib4(n - 1) + fib4(n - 2)
def fib5(n):
x, y = 0, 1
while n:
x, y, n = y, x + y, n - 1
return x
def fib6(n):
def fib_iter(n, x, y):
if 0 == n: return x
else: return fib_iter(n - 1, y, x + y)
return fib_iter(n, 0, 1)
fib7 = lambda n, x=0, y=1: x if not n else fib7(n - 1, y, x + y)
def test_fib(func, para):
now = time.clock()
result = func(para)
print('Elapsed time:', time.clock() - now)
return result
'''print the first n items.'''
def fib_show(n):
x, y = 0, 1
count = 2
print(x, y, end=' ')
while count < n:
x, y = y, x + y
count += 1
print(y, end=' ')
print('\n')
if __name__ == '__main__':
num = 32
print('fib1(%d) = %d\n' % (num, test_fib(fib1, num)))
print('fib2(%d) = %d\n' % (num, test_fib(fib2, num)))
print('fib5(%d) = %d\n' % (num, test_fib(fib5, num)))
print('fib7(%d) = %d\n' % (num, test_fib(fib7, num)))
fib_show(num + 1)