Skip to content

Commit 3136833

Browse files
committed
Improving coverage for matrix_exponentiation.py
1 parent 2cdda8a commit 3136833

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

maths/matrix_exponentiation.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ def modular_exponentiation(a, b):
3939

4040

4141
def fibonacci_with_matrix_exponentiation(n, f1, f2):
42+
"""
43+
Returns the n number of the Fibonacci sequence that
44+
start with f1 and f2
45+
Use the matrix exponentiation
46+
>>> fibonacci_with_matrix_exponentiation(1, 5, 6)
47+
5
48+
>>> fibonacci_with_matrix_exponentiation(2, 10, 11)
49+
11
50+
>>> fibonacci_with_matrix_exponentiation(13, 0, 1)
51+
144
52+
>>> fibonacci_with_matrix_exponentiation(10, 5, 9)
53+
411
54+
>>> fibonacci_with_matrix_exponentiation(9, 2, 3)
55+
89
56+
"""
4257
# Trivial Cases
4358
if n == 1:
4459
return f1
@@ -50,6 +65,21 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2):
5065

5166

5267
def simple_fibonacci(n, f1, f2):
68+
"""
69+
Returns the n number of the Fibonacci sequence that
70+
start with f1 and f2
71+
Use the definition
72+
>>> simple_fibonacci(1, 5, 6)
73+
5
74+
>>> simple_fibonacci(2, 10, 11)
75+
11
76+
>>> simple_fibonacci(13, 0, 1)
77+
144
78+
>>> simple_fibonacci(10, 5, 9)
79+
411
80+
>>> simple_fibonacci(9, 2, 3)
81+
89
82+
"""
5383
# Trivial Cases
5484
if n == 1:
5585
return f1
@@ -61,10 +91,10 @@ def simple_fibonacci(n, f1, f2):
6191
n -= 2
6292

6393
while n > 0:
64-
fn_1, fn_2 = fn_1 + fn_2, fn_1
94+
fn_2, fn_1 = fn_1 + fn_2, fn_2
6595
n -= 1
6696

67-
return fn_1
97+
return fn_2
6898

6999

70100
def matrix_exponentiation_time():

0 commit comments

Comments
 (0)