1
1
"""
2
2
Implementation of finding nth fibonacci number using matrix exponentiation.
3
- Time Complexity is about O(log(n)*8)
3
+ Time Complexity is about O(log(n)*8), where 8 is the complexity of matrix multiplication of size 2 by 2.
4
+ And on the other hand complexity of bruteforce solution is O(n).
4
5
As we know
5
6
f[n] = f[n-1] + f[n-1]
6
7
Converting to matrix,
9
10
...
10
11
...
11
12
-> [f(n),f(n-1)] = [[1,1],[1,0]]^(n-1) * [f(1),f(0)]
12
-
13
13
So we just need the n times multiplication of the matrix [1,1],[1,0]].
14
- We can decrease the n times multiplication by following the divide and conquer approach
14
+ We can decrease the n times multiplication by following the divide and conquer approach.
15
15
16
16
"""
17
17
from __future__ import print_function
@@ -35,11 +35,13 @@ def identity(n):
35
35
return [[int (row == column ) for column in range (n )] for row in range (n )]
36
36
37
37
38
- def zerro (n ):
39
- return [[int (row == column ) for column in range (n )] for row in range (n )]
40
-
41
-
42
38
def nth_fibonacci (n ):
39
+ """
40
+ >>> nth_fibonacci(100)
41
+ 354224848179261915075L
42
+ >>> nth_fibonacci(-100)
43
+ -100
44
+ """
43
45
if n <= 1 :
44
46
return n
45
47
res_matrix = identity (2 )
@@ -53,7 +55,7 @@ def nth_fibonacci(n):
53
55
return res_matrix [0 ][0 ]
54
56
55
57
56
- def nth_fibonnaci_test (n ):
58
+ def nth_fibonacci_test (n ):
57
59
if n <= 1 :
58
60
return n
59
61
fib0 = 0
@@ -65,32 +67,32 @@ def nth_fibonnaci_test(n):
65
67
66
68
def main ():
67
69
print (
68
- "0th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n "
69
- % (nth_fibonacci (0 ), nth_fibonnaci_test (0 ))
70
+ "0th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
71
+ % (nth_fibonacci (0 ), nth_fibonacci_test (0 ))
70
72
)
71
73
print (
72
- "1st fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n "
73
- % (nth_fibonacci (1 ), nth_fibonnaci_test (1 ))
74
+ "1st fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
75
+ % (nth_fibonacci (1 ), nth_fibonacci_test (1 ))
74
76
)
75
77
print (
76
- "2nd fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n "
77
- % (nth_fibonacci (2 ), nth_fibonnaci_test (2 ))
78
+ "2nd fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
79
+ % (nth_fibonacci (2 ), nth_fibonacci_test (2 ))
78
80
)
79
81
print (
80
- "3rd fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n "
81
- % (nth_fibonacci (3 ), nth_fibonnaci_test (3 ))
82
+ "3rd fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
83
+ % (nth_fibonacci (3 ), nth_fibonacci_test (3 ))
82
84
)
83
85
print (
84
- "10th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n "
85
- % (nth_fibonacci (10 ), nth_fibonnaci_test (10 ))
86
+ "10th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
87
+ % (nth_fibonacci (10 ), nth_fibonacci_test (10 ))
86
88
)
87
89
print (
88
- "100th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n "
89
- % (nth_fibonacci (100 ), nth_fibonnaci_test (100 ))
90
+ "100th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
91
+ % (nth_fibonacci (100 ), nth_fibonacci_test (100 ))
90
92
)
91
93
print (
92
- "1000th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n "
93
- % (nth_fibonacci (1000 ), nth_fibonnaci_test (1000 ))
94
+ "1000th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n "
95
+ % (nth_fibonacci (1000 ), nth_fibonacci_test (1000 ))
94
96
)
95
97
96
98
0 commit comments