2024-10-20 21:36:32 +02:00
|
|
|
from gmpy2 import mpz
|
2024-10-19 20:16:04 +02:00
|
|
|
|
2024-10-20 21:36:32 +02:00
|
|
|
def extended_gcd(a, b):
|
2024-10-19 20:16:04 +02:00
|
|
|
if b == 0:
|
2024-10-20 21:36:32 +02:00
|
|
|
return (a, mpz(1), mpz(0))
|
|
|
|
else:
|
|
|
|
gcd, x1, y1 = extended_gcd(b, a % b)
|
|
|
|
x = y1
|
|
|
|
y = x1 - (a // b) * y1
|
|
|
|
return (gcd, x, y)
|
2024-10-19 20:16:04 +02:00
|
|
|
|
2024-10-20 21:36:32 +02:00
|
|
|
def modinv(b, n):
|
|
|
|
gcd, x, y = extended_gcd(b, n)
|
|
|
|
if gcd != 1:
|
|
|
|
raise ValueError(f"Odwrotność modulo nie istnieje, ponieważ gcd({b}, {n}) = {gcd} ≠ 1.")
|
|
|
|
else:
|
|
|
|
return x % n
|
|
|
|
|
|
|
|
n = mpz(input("Podaj n: "))
|
|
|
|
b = mpz(input("Podaj b: "))
|
|
|
|
|
|
|
|
try:
|
|
|
|
inverse = modinv(b, n)
|
|
|
|
print(f"Odwrotność {b} modulo {n} to {inverse}.")
|
|
|
|
if (b * inverse) % n == 1:
|
|
|
|
print(f"Sprawdzenie: ({b} * {inverse}) mod {n} = 1\n")
|
|
|
|
else:
|
|
|
|
print("Błąd w obliczeniach.\n")
|
|
|
|
except ValueError as e:
|
|
|
|
print(f"Dla b = {b}, n = {n}: {e}\n")
|