From 8851473c6e951337498d683d65062972c001bb1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ata=20Deniz=20Ayd=C4=B1n?= Date: Sun, 14 May 2017 19:55:09 +0300 Subject: [PATCH] --- modp.py | 15 ++++++++++++++- padic.py | 2 -- 2 files changed, 14 insertions(+), 3 deletions(-) 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):