Skip to content

Commit b159705

Browse files
authored
Updated the matrix exponentiation approach of finding nth fibonacci.
- Removed some extra spaces - Added the complexity of bruteforce algorithm - Removed unused function called zerro() - Added some docktest based on request
1 parent 2a513f0 commit b159705

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

matrix/nth_fibonnacci_using_matrix_exponentiation.py renamed to matrix/nth_fibonacci_using_matrix_exponentiation.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
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).
45
As we know
56
f[n] = f[n-1] + f[n-1]
67
Converting to matrix,
@@ -9,9 +10,8 @@
910
...
1011
...
1112
-> [f(n),f(n-1)] = [[1,1],[1,0]]^(n-1) * [f(1),f(0)]
12-
1313
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.
1515
1616
"""
1717
from __future__ import print_function
@@ -35,11 +35,13 @@ def identity(n):
3535
return [[int(row == column) for column in range(n)] for row in range(n)]
3636

3737

38-
def zerro(n):
39-
return [[int(row == column) for column in range(n)] for row in range(n)]
40-
41-
4238
def nth_fibonacci(n):
39+
"""
40+
>>> nth_fibonacci(100)
41+
354224848179261915075L
42+
>>> nth_fibonacci(-100)
43+
-100
44+
"""
4345
if n <= 1:
4446
return n
4547
res_matrix = identity(2)
@@ -53,7 +55,7 @@ def nth_fibonacci(n):
5355
return res_matrix[0][0]
5456

5557

56-
def nth_fibonnaci_test(n):
58+
def nth_fibonacci_test(n):
5759
if n <= 1:
5860
return n
5961
fib0 = 0
@@ -65,32 +67,32 @@ def nth_fibonnaci_test(n):
6567

6668
def main():
6769
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))
7072
)
7173
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))
7476
)
7577
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))
7880
)
7981
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))
8284
)
8385
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))
8688
)
8789
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))
9092
)
9193
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))
9496
)
9597

9698

0 commit comments

Comments
 (0)