Saataa andagii !

This commit is contained in:
Lukian 2025-01-23 19:06:47 +01:00
parent 43a72c3fb7
commit 75d0a97501
6 changed files with 10 additions and 8 deletions

View file

@ -10,8 +10,10 @@ def modpow(a: int, b: int, m: int) -> int:
return result
def gcd(a: int, b: int) -> int:
# if b = 0 return a
if b == 0:
return a
# return gcd(b, a % b) because gcd(a, b) = gcd(b, rem(a, b))
return gcd(b, a % b)
def mod_inverse(A: int, M: int) -> int:
@ -21,18 +23,13 @@ def mod_inverse(A: int, M: int) -> int:
if (M == 1):
return 0
while (A > 1):
# q is quotient
q = A // M
t = M
# m is remainder now, process
# same as Euclid's algo
M = A % M
A = t
t = y
# Update x and y
y = x - q * y
x = t
# Make x positive
if (x < 0):
x = x + m0
return x