Ejemplo n.º 1
0
 def extgcd(self, a, b):
     """
     Return a tuple (u, v, d); they are the greatest common divisor
     d of two given integers x and y and u, v such that
     d = x * u + y * v.
     """
     return tuple(map(Integer, gcd.extgcd(a, b)))
Ejemplo n.º 2
0
 def extgcd(self, a, b):
     """
     Return a tuple (u, v, d); they are the greatest common divisor
     d of two given integers x and y and u, v such that
     d = x * u + y * v.
     """
     return tuple(map(Integer, gcd.extgcd(a, b)))
Ejemplo n.º 3
0
 def testExtgcd(self):
     u, v, d = gcd.extgcd(8, 11)
     self.assertEqual(1, abs(d))
     self.assertEqual(d, 8 * u + 11 * v)
     #sf.bug 1924839
     u, v, d = gcd.extgcd(-8, 11)
     self.assertEqual(1, abs(d))
     self.assertEqual(d, -8 * u + 11 * v)
     u, v, d = gcd.extgcd(8, -11)
     self.assertEqual(1, abs(d))
     self.assertEqual(d, 8 * u - 11 * v)
     u, v, d = gcd.extgcd(-8, -11)
     self.assertEqual(1, abs(d))
     self.assertEqual(d, -8 * u - 11 * v)
     import nzmath.rational as rational
     u, v, d = gcd.extgcd(rational.Integer(8), 11)
     self.assertEqual(1, abs(d))
     self.assertEqual(d, 8 * u + 11 * v)
Ejemplo n.º 4
0
def inverse(x, p):
    """
    This function returns inverse of x for modulo p.
    """
    x = x % p
    y = gcd.extgcd(p, x)
    if y[2] == 1:
        if y[1] < 0:
            r = p + y[1]
            return r
        else:
            return y[1]
    raise ZeroDivisionError("There is no inverse for %d modulo %d." % (x, p))
Ejemplo n.º 5
0
def inverse(x, n):
    """
    This function returns inverse of x for modulo n.
    """
    x = x % n
    y = gcd.extgcd(n, x)
    if y[2] == 1:
        if y[1] < 0:
            r = n + y[1]
            return r
        else:
            return y[1]
    raise ZeroDivisionError("There is no inverse for %d modulo %d." % (x, n))
Ejemplo n.º 6
0
 def inverse(self):
     t = gcd.extgcd(self.n, self.m)
     if t[2] != 1:
         raise ZeroDivisionError("No inverse of %s." % self)
     return self.__class__(t[0], self.m)
Ejemplo n.º 7
0
 def inverse(self):
     t = gcd.extgcd(self.n, self.m)
     if t[2] != 1:
         raise ZeroDivisionError("No inverse of %s." % self)
     return self.__class__(t[0], self.m)