Skip to content

Commit 044cca8

Browse files
mahbubcsejumahbubur.rahman
authored and
mahbubur.rahman
committed
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 b159705 commit 044cca8

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

matrix/nth_fibonacci_using_matrix_exponentiation.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
...
1111
...
1212
-> [f(n),f(n-1)] = [[1,1],[1,0]]^(n-1) * [f(1),f(0)]
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.
15-
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.
1615
"""
1716
from __future__ import print_function
1817

@@ -35,11 +34,11 @@ def identity(n):
3534
return [[int(row == column) for column in range(n)] for row in range(n)]
3635

3736

38-
def nth_fibonacci(n):
37+
def nth_fibonacci_matrix(n):
3938
"""
40-
>>> nth_fibonacci(100)
41-
354224848179261915075L
42-
>>> nth_fibonacci(-100)
39+
>>> nth_fibonacci_matrix(100)
40+
354224848179261915075
41+
>>> nth_fibonacci_matrix(-100)
4342
-100
4443
"""
4544
if n <= 1:
@@ -55,7 +54,13 @@ def nth_fibonacci(n):
5554
return res_matrix[0][0]
5655

5756

58-
def nth_fibonacci_test(n):
57+
def nth_fibonacci_bruteforce(n):
58+
"""
59+
>>> nth_fibonacci_bruteforce(100)
60+
354224848179261915075
61+
>>> nth_fibonacci_bruteforce(-100)
62+
-100
63+
"""
5964
if n <= 1:
6065
return n
6166
fib0 = 0
@@ -68,31 +73,31 @@ def nth_fibonacci_test(n):
6873
def main():
6974
print(
7075
"0th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
71-
% (nth_fibonacci(0), nth_fibonacci_test(0))
76+
% (nth_fibonacci_matrix(0), nth_fibonacci_bruteforce(0))
7277
)
7378
print(
7479
"1st fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
75-
% (nth_fibonacci(1), nth_fibonacci_test(1))
80+
% (nth_fibonacci_matrix(1), nth_fibonacci_bruteforce(1))
7681
)
7782
print(
7883
"2nd fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
79-
% (nth_fibonacci(2), nth_fibonacci_test(2))
84+
% (nth_fibonacci_matrix(2), nth_fibonacci_bruteforce(2))
8085
)
8186
print(
8287
"3rd fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
83-
% (nth_fibonacci(3), nth_fibonacci_test(3))
88+
% (nth_fibonacci_matrix(3), nth_fibonacci_bruteforce(3))
8489
)
8590
print(
8691
"10th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
87-
% (nth_fibonacci(10), nth_fibonacci_test(10))
92+
% (nth_fibonacci_matrix(10), nth_fibonacci_bruteforce(10))
8893
)
8994
print(
9095
"100th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
91-
% (nth_fibonacci(100), nth_fibonacci_test(100))
96+
% (nth_fibonacci_matrix(100), nth_fibonacci_bruteforce(100))
9297
)
9398
print(
9499
"1000th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
95-
% (nth_fibonacci(1000), nth_fibonacci_test(1000))
100+
% (nth_fibonacci_matrix(1000), nth_fibonacci_bruteforce(1000))
96101
)
97102

98103

0 commit comments

Comments
 (0)