Skip to content

Commit 2e27079

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 348b81d commit 2e27079

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

maths/lucas_series.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,41 @@ def matrix_power_lucas_number(n_th_number: int) -> int:
7373
"""
7474
if not isinstance(n_th_number, int):
7575
raise TypeError("matrix_power_lucas_number accepts only integer arguments.")
76-
76+
7777
if n_th_number == 0:
7878
return 2
7979
if n_th_number == 1:
8080
return 1
81-
81+
8282
def matrix_mult(a: list[list[int]], b: list[list[int]]) -> list[list[int]]:
8383
return [
84-
[a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]],
85-
[a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]],
84+
[
85+
a[0][0] * b[0][0] + a[0][1] * b[1][0],
86+
a[0][0] * b[0][1] + a[0][1] * b[1][1],
87+
],
88+
[
89+
a[1][0] * b[0][0] + a[1][1] * b[1][0],
90+
a[1][0] * b[0][1] + a[1][1] * b[1][1],
91+
],
8692
]
87-
93+
8894
def matrix_power(matrix: list[list[int]], power: int) -> list[list[int]]:
8995
# Start with identity matrix
9096
result: list[list[int]] = [[1, 0], [0, 1]]
9197
base = matrix
92-
98+
9399
while power > 0:
94100
if power % 2 == 1:
95101
result = matrix_mult(result, base)
96102
base = matrix_mult(base, base)
97103
power //= 2
98-
104+
99105
return result
100-
106+
101107
# Lucas number matrix form: [[1, 1], [1, 0]]
102108
base_matrix = [[1, 1], [1, 0]]
103109
powered_matrix = matrix_power(base_matrix, n_th_number - 1)
104-
110+
105111
# L(n) = powered_matrix[0][0] * L(1) + powered_matrix[0][1] * L(0)
106112
# Where L(1) = 1, L(0) = 2
107113
return powered_matrix[0][0] * 1 + powered_matrix[0][1] * 2

0 commit comments

Comments
 (0)