Skip to content

Commit c32742c

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 87630a2 commit c32742c

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

maths/lucas_series.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def recursive_lucas_number(n_th_number: int) -> int:
2525
"""
2626
if not isinstance(n_th_number, int):
2727
raise TypeError("recursive_lucas_number accepts only integer arguments.")
28-
28+
2929
# Use memoization to cache results and avoid redundant calculations
3030
@functools.lru_cache(maxsize=None)
3131
def _recursive_lucas(n: int) -> int:
@@ -34,7 +34,7 @@ def _recursive_lucas(n: int) -> int:
3434
if n == 1:
3535
return 1
3636
return _recursive_lucas(n - 1) + _recursive_lucas(n - 2)
37-
37+
3838
return _recursive_lucas(n_th_number)
3939

4040

@@ -56,16 +56,16 @@ def dynamic_lucas_number(n_th_number: int) -> int:
5656
"""
5757
if not isinstance(n_th_number, int):
5858
raise TypeError("dynamic_lucas_number accepts only integer arguments.")
59-
59+
6060
if n_th_number == 0:
6161
return 2
6262
if n_th_number == 1:
6363
return 1
64-
64+
6565
a, b = 2, 1
6666
for _ in range(2, n_th_number + 1):
6767
a, b = b, a + b
68-
68+
6969
return b
7070

7171

@@ -87,35 +87,41 @@ def matrix_power_lucas_number(n_th_number: int) -> int:
8787
"""
8888
if not isinstance(n_th_number, int):
8989
raise TypeError("matrix_power_lucas_number accepts only integer arguments.")
90-
90+
9191
if n_th_number == 0:
9292
return 2
9393
if n_th_number == 1:
9494
return 1
95-
95+
9696
def matrix_mult(a: list[list[int]], b: list[list[int]]) -> list[list[int]]:
9797
return [
98-
[a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]],
99-
[a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]],
98+
[
99+
a[0][0] * b[0][0] + a[0][1] * b[1][0],
100+
a[0][0] * b[0][1] + a[0][1] * b[1][1],
101+
],
102+
[
103+
a[1][0] * b[0][0] + a[1][1] * b[1][0],
104+
a[1][0] * b[0][1] + a[1][1] * b[1][1],
105+
],
100106
]
101-
107+
102108
def matrix_power(matrix: list[list[int]], power: int) -> list[list[int]]:
103109
# Start with identity matrix
104110
result: list[list[int]] = [[1, 0], [0, 1]]
105111
base = matrix
106-
112+
107113
while power > 0:
108114
if power % 2 == 1:
109115
result = matrix_mult(result, base)
110116
base = matrix_mult(base, base)
111117
power //= 2
112-
118+
113119
return result
114-
120+
115121
# Lucas number matrix form: [[1, 1], [1, 0]]
116122
base_matrix = [[1, 1], [1, 0]]
117123
powered_matrix = matrix_power(base_matrix, n_th_number - 1)
118-
124+
119125
# L(n) = powered_matrix[0][0] * L(1) + powered_matrix[0][1] * L(0)
120126
# Where L(1) = 1, L(0) = 2
121127
return powered_matrix[0][0] * 1 + powered_matrix[0][1] * 2
@@ -139,17 +145,17 @@ def closed_form_lucas_number(n_th_number: int) -> int:
139145
"""
140146
if not isinstance(n_th_number, int):
141147
raise TypeError("closed_form_lucas_number accepts only integer arguments.")
142-
148+
143149
if n_th_number == 0:
144150
return 2
145151
if n_th_number == 1:
146152
return 1
147-
153+
148154
# Golden ratio
149155
phi = (1 + math.sqrt(5)) / 2
150156
# Conjugate of golden ratio
151157
psi = (1 - math.sqrt(5)) / 2
152-
158+
153159
# Lucas number closed form: L(n) = phi^n + psi^n
154160
return round(phi**n_th_number + psi**n_th_number)
155161

@@ -158,7 +164,6 @@ def closed_form_lucas_number(n_th_number: int) -> int:
158164
_lucas_cache: Dict[int, int] = {0: 2, 1: 1}
159165

160166

161-
162167
def cached_lucas_number(n_th_number: int) -> int:
163168
"""
164169
Returns the nth lucas number using a cached approach for optimal performance
@@ -177,16 +182,16 @@ def cached_lucas_number(n_th_number: int) -> int:
177182
"""
178183
if not isinstance(n_th_number, int):
179184
raise TypeError("cached_lucas_number accepts only integer arguments.")
180-
185+
181186
if n_th_number in _lucas_cache:
182187
return _lucas_cache[n_th_number]
183-
188+
184189
# Calculate using the fastest method for uncached values
185190
if n_th_number < 70: # For smaller values, closed form is efficient
186191
result = closed_form_lucas_number(n_th_number)
187192
else: # For larger values, matrix exponentiation is more stable
188193
result = matrix_power_lucas_number(n_th_number)
189-
194+
190195
_lucas_cache[n_th_number] = result
191196
return result
192197

@@ -205,4 +210,4 @@ def cached_lucas_number(n_th_number: int) -> int:
205210
print("\nUsing closed-form formula to calculate lucas series:")
206211
print(" ".join(str(closed_form_lucas_number(i)) for i in range(n)))
207212
print("\nUsing cached function to calculate lucas series:")
208-
print(" ".join(str(cached_lucas_number(i)) for i in range(n)))
213+
print(" ".join(str(cached_lucas_number(i)) for i in range(n)))

0 commit comments

Comments
 (0)