-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.py
More file actions
57 lines (46 loc) · 1.33 KB
/
Copy pathfibonacci.py
File metadata and controls
57 lines (46 loc) · 1.33 KB
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : fibonacci.py
@Contact : 9824373@qq.com
@License : (C)Copyright 2017-2018, Zhan
@Desc :
@Modify Time @Author @Version @Desciption
------------ ------- -------- -----------
2019/6/1 23:06 zhan 1.0 None
'''
import time
import numpy as np
def DP_Fibonacci(n):
rst = [0 for i in range(n + 1)]
rst[0] = 0
rst[1] = 1
for i in range(2, n + 1):
rst[i] = rst[i - 2] + rst[i - 1]
return rst[n]
def DIV_Fibonacci(n):
def matrix_multiply(A, n):
rst = np.eye(2,dtype=np.int64)
tmp = A
while n > 0:
if n & 1:
rst = np.dot(rst, tmp)
tmp = np.dot(tmp, tmp)
n = (n >> 1)
return rst
A = np.array([[1, 1], [1, 0]],dtype=np.int64)
B = matrix_multiply(A, n - 1)
return B[0, 0]
if __name__ == '__main__':
start = time.clock()
rst = DP_Fibonacci(100000)
end = time.clock()
print(
'DP_Fibonacci Running time: %s Seconds and result is %d ' %
((end - start), rst))
start = time.clock()
rst = DIV_Fibonacci(100000)
end = time.clock()
print(
'DIV_Fibonacci Running time: %s Seconds and result is %d ' %
((end - start), rst))