mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 14:13:44 +00:00
Improve test coverage for matrix exponentiation (#12388)
* #9943 : Adding coverage test for basic_graphs.py * #9943 : Adding coverage test for basic_graphs.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Solve problem of line too long * Improving coverage for matrix_exponentiation.py * fix more than one file * Update matrix_exponentiation.py * Update matrix_exponentiation.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
parent
5bef6ac929
commit
8bbe8caa25
|
@ -39,6 +39,21 @@ def modular_exponentiation(a, b):
|
||||||
|
|
||||||
|
|
||||||
def fibonacci_with_matrix_exponentiation(n, f1, f2):
|
def fibonacci_with_matrix_exponentiation(n, f1, f2):
|
||||||
|
"""
|
||||||
|
Returns the nth number of the Fibonacci sequence that
|
||||||
|
starts with f1 and f2
|
||||||
|
Uses the matrix exponentiation
|
||||||
|
>>> fibonacci_with_matrix_exponentiation(1, 5, 6)
|
||||||
|
5
|
||||||
|
>>> fibonacci_with_matrix_exponentiation(2, 10, 11)
|
||||||
|
11
|
||||||
|
>>> fibonacci_with_matrix_exponentiation(13, 0, 1)
|
||||||
|
144
|
||||||
|
>>> fibonacci_with_matrix_exponentiation(10, 5, 9)
|
||||||
|
411
|
||||||
|
>>> fibonacci_with_matrix_exponentiation(9, 2, 3)
|
||||||
|
89
|
||||||
|
"""
|
||||||
# Trivial Cases
|
# Trivial Cases
|
||||||
if n == 1:
|
if n == 1:
|
||||||
return f1
|
return f1
|
||||||
|
@ -50,21 +65,34 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2):
|
||||||
|
|
||||||
|
|
||||||
def simple_fibonacci(n, f1, f2):
|
def simple_fibonacci(n, f1, f2):
|
||||||
|
"""
|
||||||
|
Returns the nth number of the Fibonacci sequence that
|
||||||
|
starts with f1 and f2
|
||||||
|
Uses the definition
|
||||||
|
>>> simple_fibonacci(1, 5, 6)
|
||||||
|
5
|
||||||
|
>>> simple_fibonacci(2, 10, 11)
|
||||||
|
11
|
||||||
|
>>> simple_fibonacci(13, 0, 1)
|
||||||
|
144
|
||||||
|
>>> simple_fibonacci(10, 5, 9)
|
||||||
|
411
|
||||||
|
>>> simple_fibonacci(9, 2, 3)
|
||||||
|
89
|
||||||
|
"""
|
||||||
# Trivial Cases
|
# Trivial Cases
|
||||||
if n == 1:
|
if n == 1:
|
||||||
return f1
|
return f1
|
||||||
elif n == 2:
|
elif n == 2:
|
||||||
return f2
|
return f2
|
||||||
|
|
||||||
fn_1 = f1
|
|
||||||
fn_2 = f2
|
|
||||||
n -= 2
|
n -= 2
|
||||||
|
|
||||||
while n > 0:
|
while n > 0:
|
||||||
fn_1, fn_2 = fn_1 + fn_2, fn_1
|
f2, f1 = f1 + f2, f2
|
||||||
n -= 1
|
n -= 1
|
||||||
|
|
||||||
return fn_1
|
return f2
|
||||||
|
|
||||||
|
|
||||||
def matrix_exponentiation_time():
|
def matrix_exponentiation_time():
|
||||||
|
|
Loading…
Reference in New Issue
Block a user