[Algorithms(Python)] modular_inverse Author: Slay Date: August 11, 2022 17:05:00 Category: Algorithms(Python) Code1234567891011121314151617181920212223def egcd(a, m): if a == 0: return (m, 0, 1) else: j, y, x = egcd(m%a, a) return (j, x - (m//a)*y, y)def MyExMod(a,n,m): return (a**n)%mdef MyInvMod(a,m): j, x, y = egcd(a, m) if j != 1: raise Exception("Not inverse modular") else: return x%ma = int(input("a : "))n = int(input("n : "))m = int(input("m : "))print("a^n mod m : ", MyExMod(a, n, m))print("a^-1 mod m : ", MyInvMod(a, m)) Author: Slay Permalink: http://sean-baek.github.io/2022/08/11/2022-08-11-modular-inverse/ License: Copyright (c) 2021 CC-BY-NC-4.0 LICENSE Slogan: Do you believe in DESTINY? Tag(s): back · home [Note] useful Linux commands [Algorithms(C)] array_rotate_right