Saataa andagii !
This commit is contained in:
parent
43a72c3fb7
commit
75d0a97501
6 changed files with 10 additions and 8 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue