mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Merge branch 'master' into pre-commit-ci-update-config
This commit is contained in:
@@ -91,15 +91,15 @@ def fib_iterative(n: int) -> list[int]:
|
||||
def fib_recursive(n: int) -> list[int]:
|
||||
"""
|
||||
Calculates the first n (0-indexed) Fibonacci numbers using recursion
|
||||
>>> fib_iterative(0)
|
||||
>>> fib_recursive(0)
|
||||
[0]
|
||||
>>> fib_iterative(1)
|
||||
>>> fib_recursive(1)
|
||||
[0, 1]
|
||||
>>> fib_iterative(5)
|
||||
>>> fib_recursive(5)
|
||||
[0, 1, 1, 2, 3, 5]
|
||||
>>> fib_iterative(10)
|
||||
>>> fib_recursive(10)
|
||||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
||||
>>> fib_iterative(-1)
|
||||
>>> fib_recursive(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: n is negative
|
||||
@@ -119,7 +119,7 @@ def fib_recursive(n: int) -> list[int]:
|
||||
>>> fib_recursive_term(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
Exception: n is negative
|
||||
ValueError: n is negative
|
||||
"""
|
||||
if i < 0:
|
||||
raise ValueError("n is negative")
|
||||
@@ -135,15 +135,15 @@ def fib_recursive(n: int) -> list[int]:
|
||||
def fib_recursive_cached(n: int) -> list[int]:
|
||||
"""
|
||||
Calculates the first n (0-indexed) Fibonacci numbers using recursion
|
||||
>>> fib_iterative(0)
|
||||
>>> fib_recursive_cached(0)
|
||||
[0]
|
||||
>>> fib_iterative(1)
|
||||
>>> fib_recursive_cached(1)
|
||||
[0, 1]
|
||||
>>> fib_iterative(5)
|
||||
>>> fib_recursive_cached(5)
|
||||
[0, 1, 1, 2, 3, 5]
|
||||
>>> fib_iterative(10)
|
||||
>>> fib_recursive_cached(10)
|
||||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
||||
>>> fib_iterative(-1)
|
||||
>>> fib_recursive_cached(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: n is negative
|
||||
@@ -176,7 +176,7 @@ def fib_memoization(n: int) -> list[int]:
|
||||
[0, 1, 1, 2, 3, 5]
|
||||
>>> fib_memoization(10)
|
||||
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
||||
>>> fib_iterative(-1)
|
||||
>>> fib_memoization(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: n is negative
|
||||
|
||||
@@ -30,6 +30,8 @@ def greatest_common_divisor(a: int, b: int) -> int:
|
||||
3
|
||||
>>> greatest_common_divisor(-3, -9)
|
||||
3
|
||||
>>> greatest_common_divisor(0, 0)
|
||||
0
|
||||
"""
|
||||
return abs(b) if a == 0 else greatest_common_divisor(b % a, a)
|
||||
|
||||
@@ -50,6 +52,8 @@ def gcd_by_iterative(x: int, y: int) -> int:
|
||||
1
|
||||
>>> gcd_by_iterative(11, 37)
|
||||
1
|
||||
>>> gcd_by_iterative(0, 0)
|
||||
0
|
||||
"""
|
||||
while y: # --> when y=0 then loop will terminate and return x as final GCD.
|
||||
x, y = y, x % y
|
||||
|
||||
@@ -11,7 +11,7 @@ https://www.hackerearth.com/practice/notes/matrix-exponentiation-1/
|
||||
|
||||
|
||||
class Matrix:
|
||||
def __init__(self, arg):
|
||||
def __init__(self, arg: list[list] | int) -> None:
|
||||
if isinstance(arg, list): # Initializes a matrix identical to the one provided.
|
||||
self.t = arg
|
||||
self.n = len(arg)
|
||||
@@ -19,7 +19,7 @@ class Matrix:
|
||||
self.n = arg
|
||||
self.t = [[0 for _ in range(self.n)] for _ in range(self.n)]
|
||||
|
||||
def __mul__(self, b):
|
||||
def __mul__(self, b: Matrix) -> Matrix:
|
||||
matrix = Matrix(self.n)
|
||||
for i in range(self.n):
|
||||
for j in range(self.n):
|
||||
@@ -28,7 +28,7 @@ class Matrix:
|
||||
return matrix
|
||||
|
||||
|
||||
def modular_exponentiation(a, b):
|
||||
def modular_exponentiation(a: Matrix, b: int) -> Matrix:
|
||||
matrix = Matrix([[1, 0], [0, 1]])
|
||||
while b > 0:
|
||||
if b & 1:
|
||||
@@ -38,7 +38,7 @@ def modular_exponentiation(a, b):
|
||||
return matrix
|
||||
|
||||
|
||||
def fibonacci_with_matrix_exponentiation(n, f1, f2):
|
||||
def fibonacci_with_matrix_exponentiation(n: int, f1: int, f2: int) -> int:
|
||||
"""
|
||||
Returns the nth number of the Fibonacci sequence that
|
||||
starts with f1 and f2
|
||||
@@ -64,7 +64,7 @@ def fibonacci_with_matrix_exponentiation(n, f1, f2):
|
||||
return f2 * matrix.t[0][0] + f1 * matrix.t[0][1]
|
||||
|
||||
|
||||
def simple_fibonacci(n, f1, f2):
|
||||
def simple_fibonacci(n: int, f1: int, f2: int) -> int:
|
||||
"""
|
||||
Returns the nth number of the Fibonacci sequence that
|
||||
starts with f1 and f2
|
||||
@@ -95,7 +95,7 @@ def simple_fibonacci(n, f1, f2):
|
||||
return f2
|
||||
|
||||
|
||||
def matrix_exponentiation_time():
|
||||
def matrix_exponentiation_time() -> float:
|
||||
setup = """
|
||||
from random import randint
|
||||
from __main__ import fibonacci_with_matrix_exponentiation
|
||||
@@ -106,7 +106,7 @@ from __main__ import fibonacci_with_matrix_exponentiation
|
||||
return exec_time
|
||||
|
||||
|
||||
def simple_fibonacci_time():
|
||||
def simple_fibonacci_time() -> float:
|
||||
setup = """
|
||||
from random import randint
|
||||
from __main__ import simple_fibonacci
|
||||
@@ -119,7 +119,7 @@ from __main__ import simple_fibonacci
|
||||
return exec_time
|
||||
|
||||
|
||||
def main():
|
||||
def main() -> None:
|
||||
matrix_exponentiation_time()
|
||||
simple_fibonacci_time()
|
||||
|
||||
|
||||
@@ -28,9 +28,13 @@ def modular_division(a: int, b: int, n: int) -> int:
|
||||
4
|
||||
|
||||
"""
|
||||
assert n > 1
|
||||
assert a > 0
|
||||
assert greatest_common_divisor(a, n) == 1
|
||||
if n <= 1:
|
||||
raise ValueError("Modulus n must be greater than 1")
|
||||
if a <= 0:
|
||||
raise ValueError("Divisor a must be a positive integer")
|
||||
if greatest_common_divisor(a, n) != 1:
|
||||
raise ValueError("a and n must be coprime (gcd(a, n) = 1)")
|
||||
|
||||
(_d, _t, s) = extended_gcd(n, a) # Implemented below
|
||||
x = (b * s) % n
|
||||
return x
|
||||
|
||||
Reference in New Issue
Block a user