Skip to content

Commit f5c3e7c

Browse files
Anusha-DeviEAnusha-DeviEpre-commit-ci[bot]MaximSmolskiy
authored
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>
1 parent 049a34d commit f5c3e7c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

maths/modular_division.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ def modular_division(a: int, b: int, n: int) -> int:
2828
4
2929
3030
"""
31-
assert n > 1
32-
assert a > 0
33-
assert greatest_common_divisor(a, n) == 1
31+
if n <= 1:
32+
raise ValueError("Modulus n must be greater than 1")
33+
if a <= 0:
34+
raise ValueError("Divisor a must be a positive integer")
35+
if greatest_common_divisor(a, n) != 1:
36+
raise ValueError("a and n must be coprime (gcd(a, n) = 1)")
37+
3438
(_d, _t, s) = extended_gcd(n, a) # Implemented below
3539
x = (b * s) % n
3640
return x

0 commit comments

Comments
 (0)