Skip to content

Commit 23131c4

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 23131c4

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
tem = fib1 + fib0
63+
fib0 = fib1
64+
fib1 = tem
65+
return fib1
66+
67+
68+
def main():
69+
print(
70+
"0th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n"
71+
% (nth_fibonacci(0), nth_fibonnaci_test(0))
72+
)
73+
print(
74+
"1st fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n"
75+
% (nth_fibonacci(1), nth_fibonnaci_test(1))
76+
)
77+
print(
78+
"2nd fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n"
79+
% (nth_fibonacci(2), nth_fibonnaci_test(2))
80+
)
81+
print(
82+
"3rd fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n"
83+
% (nth_fibonacci(3), nth_fibonnaci_test(3))
84+
)
85+
print(
86+
"10th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n"
87+
% (nth_fibonacci(10), nth_fibonnaci_test(10))
88+
)
89+
print(
90+
"100th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n"
91+
% (nth_fibonacci(100), nth_fibonnaci_test(100))
92+
)
93+
print(
94+
"1000th fibonnacsi number using matrix exponentiation is %s and using bruteforce is %s \n"
95+
% (nth_fibonacci(1000), nth_fibonnaci_test(1000))
96+
)
97+
98+
99+
if __name__ == "__main__":
100+
main()

0 commit comments

Comments
 (0)