diff --git a/modp.py b/modp.py index 19b15d9..18a626c 100644 --- a/modp.py +++ b/modp.py @@ -24,4 +24,17 @@ class ModP(int): def _inv(self): 'Find multiplicative inverse of self in Z mod p.' - pass \ No newline at end of file + # extended Euclidean algorithm + rcurr = self.p + rnext = int(self) + tcurr = 0 + tnext = 1 + + while rnext: + q = rcurr // rnext + rcurr, rnext = rnext, rcurr - q * rnext + tcurr, tnext = tnext, tcurr - q * tnext + + if rcurr != 1: + raise ValueError("%d not a unit modulo %d" % (self, self.p)) + return ModP(self.p, tcurr) \ No newline at end of file diff --git a/padic.py b/padic.py index a79dd75..bdbbf6e 100644 --- a/padic.py +++ b/padic.py @@ -31,8 +31,6 @@ class PAdic: return int(self.get(32), p) def __str__(self): return self.get(32) - def __repr__(self): - pass # arithmetic operations def __add__(self, other):