Skip to content

Commit 2a513f0

Browse files
author
mahbubur.rahman
committed
Added matrix exponentiation approach for finding fibonacci number.
* Implemented the way of finding nth fibonacci. * Complexity is about O(log(n)*8)
1 parent f7ac8b5 commit 2a513f0

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
Implementation of finding nth fibonacci number using matrix exponentiation.
3+
Time Complexity is about O(log(n)*8)
4+
As we know
5+
f[n] = f[n-1] + f[n-1]
6+
Converting to matrix,
7+
[f(n),f(n-1)] = [[1,1],[1,0]] * [f(n-1),f(n-2)]
8+
-> [f(n),f(n-1)] = [[1,1],[1,0]]^2 * [f(n-2),f(n-3)]
9+
...
10+
...
11+
-> [f(n),f(n-1)] = [[1,1],[1,0]]^(n-1) * [f(1),f(0)]
12+
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+
16+
"""
17+
from __future__ import print_function
18+
19+
20+
def multiply(matrix_a, matrix_b):
21+
matrix_c = []
22+
n = len(matrix_a)
23+
for i in range(n):
24+
list_1 = []
25+
for j in range(n):
26+
val = 0
27+
for k in range(n):
28+
val = val + matrix_a[i][k] * matrix_b[k][j]
29+
list_1.append(val)
30+
matrix_c.append(list_1)
31+
return matrix_c
32+
33+
34+
def identity(n):
35+
return [[int(row == column) for column in range(n)] for row in range(n)]
36+
37+
38+
def zerro(n):
39+
return [[int(row == column) for column in range(n)] for row in range(n)]
40+
41+
42+
def nth_fibonacci(n):
43+
if n <= 1:
44+
return n
45+
res_matrix = identity(2)
46+
fibonacci_matrix = [[1, 1], [1, 0]]
47+
n = n - 1
48+
while n > 0:
49+
if n % 2 == 1:
50+
res_matrix = multiply(res_matrix, fibonacci_matrix)
51+
fibonacci_matrix = multiply(fibonacci_matrix, fibonacci_matrix)
52+
n = int(n / 2)
53+
return res_matrix[0][0]
54+
55+
56+
def nth_fibonnaci_test(n):
57+
if n <= 1:
58+
return n
59+
fib0 = 0
60+
fib1 = 1
61+
for i in range(2, n + 1):
62+
fib0, fib1 = fib1, fib0 + fib1
63+
return fib1
64+
65+
66+
def main():
67+
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+
)
71+
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+
)
75+
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+
)
79+
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+
)
83+
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+
)
87+
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+
)
91+
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+
)
95+
96+
97+
if __name__ == "__main__":
98+
main()

0 commit comments

Comments
 (0)