Пример #1
0
 def test_mul(self):
     for _ in range(self.TESTS_COUNT):
         x = randint(self.MIN, self.MAX)
         y = randint(self.MIN, self.MAX)
         big_x = BigInt(str(x))
         big_y = BigInt(str(y))
         self.assertEqual(x * y, big_x * big_y)
Пример #2
0
 def test_div(self):
     for _ in range(self.TESTS_COUNT):
         x = randint(self.MIN, self.MAX)
         y = randint(self.MIN, self.MAX)
         big_x = BigInt(str(x))
         big_y = BigInt(str(y))
         self.assertEqual(int(x / y), big_x / big_y)
Пример #3
0
 def test_root(self):
     for _ in range(100):
         x = randint(0, 100)
         y = randint(1, 100)
         big_x = BigInt(str(x))
         big_y = BigInt(str(y))
         self.assertEqual(int(x ** (1 / y)), BigInt.root(big_x, big_y))
Пример #4
0
 def test_pow(self):
     for _ in range(100):
         x = randint(-100, 100)
         y = randint(0, 100)
         big_x = BigInt(str(x))
         big_y = BigInt(str(y))
         self.assertEqual(x ** y, big_x ** big_y)
Пример #5
0
 def test_ring_inv(self):
     values = [
         ('873372847093', str(10 ** 12), '94559444997'),
         ('3', '6', None),
     ]
     for *args, x in values:
         args = list(map(BigInt, args))
         if isinstance(x, str):
             x = BigInt(x)
         self.assertEqual(BigInt.ring_inv(*args[:2]), x)
Пример #6
0
 def test_lsbgcd(self):
     for _ in range(1000):
         x = randint(0, 10 ** 20)
         y = randint(0, 10 ** 20)
         big_x = BigInt(str(x))
         big_y = BigInt(str(y))
         d, big_u, big_v = BigInt.lsbgcd(big_x, big_y)
         u = int(('-' if big_u.is_neg else '') + big_u.value)
         v = int(('-' if big_v.is_neg else '') + big_v.value)
         self.assertEqual(str(math.gcd(x, y)), d.value)
         self.assertEqual(str(u*x + v*y), d.value)
Пример #7
0
 def test_ring_pow(self):
     values = [
         ('3', '4', '5', '1'),
         ('18', '50', '873372847093', '194798095869'),
         ('12345678', '0', '987', '1'),
         ('0', '12345678', '987', '0'),
         ('999', '999', '999', '0')
     ]
     for args in values:
         args = list(map(BigInt, args))
         self.assertEqual(BigInt.ring_pow(*args[:3]), args[3])
Пример #8
0
 def test_ring_mul(self):
     values = [
         ('3', '4', '5', '2'),
         ('12345678', '87654321', '123', '3'),
         ('12345678', '0', '987', '0'),
         ('0', '12345678', '987', '0'),
         ('9999999999', '9999999999', '9999999999', '0')
     ]
     for args in values:
         args = list(map(BigInt, args))
         self.assertEqual(BigInt.ring_mul(*args[:3]), args[3])
Пример #9
0
from bigint import BigInt

x1 = BigInt('99999999999999999999')
y1 = BigInt('11111111111111111111')
x2 = BigInt('99')
y2 = BigInt('11')
N = BigInt('12345678998765432')

print('x1 =', x1)
print('y1 =', y1)
print('x2 =', x2)
print('y2 =', y2)
print('N =', N)

print('x1 + y1 =', x1 + y1)
print('x1 - y1 =', x1 - y1)
print('x1 * y1 =', x1 * y1)
print('x1 / y1 =', x1 / y1)
print('x1 mod y1 =', x1 % y1)
print('x2 ^ y2 =', x2 ** y2)
print('Корень из x2 степени y2 = ', BigInt.root(x2, y2))
print('НОД(x1, y1) =', BigInt.gcd(x1, y1)[0])
print('(x1 + y1) mod N =', BigInt.ring_add(x1, y1, N))
print('(x1 - y1) mod N =', BigInt.ring_sub(x1, y1, N))
print('(x1 * y1) mod N =', BigInt.ring_mul(x1, y1, N))
print('x1^(-1) mod N =', BigInt.ring_inv(x1, N))
print('x2^y2 mod N =', BigInt.ring_pow(x2, y2, N))