Пример #1
0
 def test_mod_inv(self):
     for i in range(TESTS):
         a = randint(1, 10000000)
         b = randint(1, 10000000)
         g = nt.gcd(a, b)
         # ensure a, b are relatively prime
         a, b = a / g, b / g
         ainv = nt.modinv(a, b)
         self.assertEqual((a * ainv) % b, 1)
Пример #2
0
def pollard_rho(n):
    def f(x):
        ret = pow(x, 2, n) + 1
        return 0 if ret == n else ret

    x = 2
    y = 1
    cnt = 10000
    while x != y:  # and cnt > 0:
        cnt -= 1
        x = f(x)
        y = f(y)
        g = nt.gcd(n, abs(x - y))
        #print('x = {}, y = {}, g = {}'.format(x, y, g))
        if 1 < g and g < n:
            #print('iteration: {}'.format(10000-cnt))
            return g
        x = f(x)
    #print('iteration: {}'.format(10000-cnt))
    if cnt == 0:
        return 'max iteration exceeded'
    return 'not found, maybe prime'
Пример #3
0
 def test_gcd(self):
     self.assertEqual(nt.gcd(15, 6), 3)
Пример #4
0
 def test_gcd_rand(self):
     for i in range(TESTS):
         a = randint(1, 10000000)
         b = randint(1, 10000000)
         g = nt.gcd(a, b)
         self.assertEqual(nt.gcd(a / g, b / g), 1)
Пример #5
0
 def test_gcd_one(self):
     for i in range(TESTS):
         a = randint(1, 10000000)
         self.assertEqual(nt.gcd(a, 1), 1)
Пример #6
0
 def test_gcd_rev(self):
     for i in range(TESTS):
         a = randint(1, 10000000)
         b = randint(1, 10000000)
         self.assertEqual(nt.gcd(a, b), nt.gcd(b, a))