mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Replace assert-based validation with explicit errors in modular_division (#14204)
* Improve documentation for linear search algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Replace asserts with explicit validation in modular_division * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update linear_search.py --------- Co-authored-by: Anusha-DeviE <itzanushadevi@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
@@ -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