First commit

This commit is contained in:
Lukian 2025-01-23 17:57:15 +01:00
commit 43a72c3fb7
12 changed files with 213 additions and 0 deletions

39
lib/arithmetics.py Normal file
View file

@ -0,0 +1,39 @@
# Arithmetics functions implementaton
def modpow(a: int, b: int, m: int) -> int:
result = 1
while b > 0:
if (b & 1) > 0:
result = (result * a) % m
b = b >> 1
a = (a * a) % m
return result
def gcd(a: int, b: int) -> int:
if b == 0:
return a
return gcd(b, a % b)
def mod_inverse(A: int, M: int) -> int:
m0 = M
y = 0
x = 1
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