Esempio 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)))
Esempio 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)))
Esempio 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)
Esempio 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))
Esempio 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))
Esempio 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)
Esempio 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)