Skip to content

Commit 23a5333

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 23a5333

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

matrix/nth_fibonacci_using_matrix_exponentiation.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
-> [f(n),f(n-1)] = [[1,1],[1,0]]^(n-1) * [f(1),f(0)]
1313
So we just need the n times multiplication of the matrix [1,1],[1,0]].
1414
We can decrease the n times multiplication by following the divide and conquer approach.
15-
1615
"""
1716
from __future__ import print_function
1817

@@ -38,7 +37,7 @@ def identity(n):
3837
def nth_fibonacci(n):
3938
"""
4039
>>> nth_fibonacci(100)
41-
354224848179261915075L
40+
354224848179261915075
4241
>>> nth_fibonacci(-100)
4342
-100
4443
"""
@@ -90,11 +89,13 @@ def main():
9089
"100th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
9190
% (nth_fibonacci(100), nth_fibonacci_test(100))
9291
)
93-
print(
94-
"1000th fibonacci number using matrix exponentiation is %s and using bruteforce is %s \n"
95-
% (nth_fibonacci(1000), nth_fibonacci_test(1000))
96-
)
92+
9793

9894

9995
if __name__ == "__main__":
10096
main()
97+
from timeit import timeit
98+
print(timeit("nth_fibonacci(1000000)", "from __main__ import nth_fibonacci", number=5))
99+
print(timeit("nth_fibonacci_test(1000000)", "from __main__ import nth_fibonacci_test", number=5))
100+
2.3342058970001744
101+
57.256506615000035

0 commit comments

Comments
 (0)