Skip to content

Commit 348b81d

Browse files
authored
fix this py is to slow Problem
1. Added matrix_power_lucas_number function: · Implements matrix exponentiation algorithm for O(log n) time complexity · Maintains the same interface as existing functions · Includes comprehensive doctests matching the existing patterns 2. Key features: · Uses 2x2 matrix multiplication with fast exponentiation · Handles base cases (n=0, n=1) directly · Proper type annotations for all internal functions · Maintains backward compatibility
1 parent a71618f commit 348b81d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

maths/lucas_series.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,58 @@ def dynamic_lucas_number(n_th_number: int) -> int:
5555
return a
5656

5757

58+
def matrix_power_lucas_number(n_th_number: int) -> int:
59+
"""
60+
Returns the nth lucas number using matrix exponentiation
61+
>>> matrix_power_lucas_number(1)
62+
1
63+
>>> matrix_power_lucas_number(20)
64+
15127
65+
>>> matrix_power_lucas_number(0)
66+
2
67+
>>> matrix_power_lucas_number(25)
68+
167761
69+
>>> matrix_power_lucas_number(-1.5)
70+
Traceback (most recent call last):
71+
...
72+
TypeError: matrix_power_lucas_number accepts only integer arguments.
73+
"""
74+
if not isinstance(n_th_number, int):
75+
raise TypeError("matrix_power_lucas_number accepts only integer arguments.")
76+
77+
if n_th_number == 0:
78+
return 2
79+
if n_th_number == 1:
80+
return 1
81+
82+
def matrix_mult(a: list[list[int]], b: list[list[int]]) -> list[list[int]]:
83+
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]],
86+
]
87+
88+
def matrix_power(matrix: list[list[int]], power: int) -> list[list[int]]:
89+
# Start with identity matrix
90+
result: list[list[int]] = [[1, 0], [0, 1]]
91+
base = matrix
92+
93+
while power > 0:
94+
if power % 2 == 1:
95+
result = matrix_mult(result, base)
96+
base = matrix_mult(base, base)
97+
power //= 2
98+
99+
return result
100+
101+
# Lucas number matrix form: [[1, 1], [1, 0]]
102+
base_matrix = [[1, 1], [1, 0]]
103+
powered_matrix = matrix_power(base_matrix, n_th_number - 1)
104+
105+
# L(n) = powered_matrix[0][0] * L(1) + powered_matrix[0][1] * L(0)
106+
# Where L(1) = 1, L(0) = 2
107+
return powered_matrix[0][0] * 1 + powered_matrix[0][1] * 2
108+
109+
58110
if __name__ == "__main__":
59111
from doctest import testmod
60112

@@ -64,3 +116,5 @@ def dynamic_lucas_number(n_th_number: int) -> int:
64116
print(" ".join(str(recursive_lucas_number(i)) for i in range(n)))
65117
print("\nUsing dynamic function to calculate lucas series:")
66118
print(" ".join(str(dynamic_lucas_number(i)) for i in range(n)))
119+
print("\nUsing matrix exponentiation to calculate lucas series:")
120+
print(" ".join(str(matrix_power_lucas_number(i)) for i in range(n)))

0 commit comments

Comments
 (0)