Ejemplo n.º 1
0
 def __add__(self, other):
     if isinstance(other, self.__class__):
         if self.modulus != other.modulus:
             raise ValueError('Numbers with different modules cannot be added')
         val = ModularArithmetics.add(self.value, other.value, self.modulus)
         return Modular(val, self.modulus)
     else:
         val = ModularArithmetics.add(self.value, other, self.modulus)
         return Modular(val, self.modulus)
def chinese_remainder_theorem():
    y = [int(x) for x in input("\n\tSpecify the sequence of integers:").split()]
    n = [int(x) for x in input("\n\tSpecify positive integers that are pairwise co-prime:").split()]
    result = ModularArithmetics.chinese_remainder_theorem(y, n)
    print("\n\tResult:")
    print("\t", result)
    press_any_key()
Ejemplo n.º 3
0
 def test4_primitives_of_1021(self):
     self.assertEqual(ModularArithmetics.primitives(1021), [
         10, 22, 30, 31, 34, 35, 37, 40, 43, 46, 50, 53, 59, 65, 66, 76, 77,
         82, 90, 93, 94, 95, 102, 103, 105, 109, 111, 119, 120, 122, 124,
         127, 129, 134, 137, 138, 140, 143, 150, 159, 160, 161, 166, 172,
         175, 177, 178, 184, 185, 195, 198, 209, 211, 212, 221, 228, 231,
         232, 236, 239, 241, 246, 260, 263, 266, 270, 279, 281, 282, 283,
         285, 287, 298, 299, 304, 306, 309, 313, 315, 325, 326, 327, 329,
         330, 333, 337, 352, 357, 358, 366, 372, 377, 380, 381, 385, 386,
         387, 388, 394, 398, 402, 410, 411, 419, 420, 427, 428, 429, 434,
         436, 440, 449, 450, 461, 466, 469, 473, 477, 480, 483, 485, 487,
         490, 494, 496, 498, 502, 505, 516, 519, 523, 525, 527, 531, 534,
         536, 538, 541, 544, 548, 552, 555, 560, 571, 572, 581, 585, 587,
         592, 593, 594, 601, 602, 610, 611, 619, 623, 627, 633, 634, 635,
         636, 640, 641, 644, 649, 655, 663, 664, 669, 684, 688, 691, 692,
         694, 695, 696, 706, 708, 712, 715, 717, 722, 723, 734, 736, 738,
         739, 740, 742, 751, 755, 758, 761, 775, 780, 782, 785, 789, 790,
         793, 800, 809, 810, 812, 823, 826, 836, 837, 843, 844, 846, 849,
         855, 860, 861, 862, 871, 878, 881, 883, 884, 887, 892, 894, 897,
         899, 901, 902, 910, 912, 916, 918, 919, 926, 927, 928, 931, 939,
         944, 945, 955, 956, 962, 968, 971, 975, 978, 981, 984, 986, 987,
         990, 991, 999, 1011])
 def test_multiplying_numbers_8_3_mod3(self):
     self.assertEqual(ModularArithmetics.multiply(8, 3, 3), 0)
 def test_adding_numbers_5_4_mod7(self):
     self.assertEqual(ModularArithmetics.add(5, 4, 7), 2)
 def test_multiplying_numbers_6_5_mod8(self):
     self.assertEqual(ModularArithmetics.multiply(6, 5, 8), 6)
 def test_mod_inv_of_2_mod_8(self):
     # Modular inverse should not exist
     self.assertEqual(ModularArithmetics.modular_inverse(2, 8), None)
 def test_adding_numbers_3_4_mod11(self):
     self.assertEqual(ModularArithmetics.add(3, 4, 11), 7)
Ejemplo n.º 9
0
 def test3_chinese_remainder_theorem(self):
     self.assertEqual(
         ModularArithmetics.chinese_remainder_theorem([2, 3], [3, 5, 7]),
         None)
Ejemplo n.º 10
0
 def modular_inverse(self):
     val = ModularArithmetics.modular_inverse(self.value, self.modulus)
     return Modular(val, self.modulus)
def primitives():
    p = int(input("\n\tSpecify the base: "))
    result = ModularArithmetics.primitives(p)
    print("\n\tResult:")
    print("\t", result)
    press_any_key()
Ejemplo n.º 12
0
 def test_mod_inv_of_3_mod_7(self):
     self.assertEqual(ModularArithmetics.modular_inverse(3, 7), 5)
Ejemplo n.º 13
0
 def test3_primitives_of_71(self):
     self.assertEqual(ModularArithmetics.primitives(71), [
         7, 11, 13, 21, 22, 28, 31, 33, 35, 42, 44, 47, 52, 53, 55, 56, 59,
         61, 62, 63, 65, 67, 68, 69])
Ejemplo n.º 14
0
 def test2_primitives_of_17(self):
     self.assertEqual(ModularArithmetics.primitives(17), [
         3, 5, 6, 7, 10, 11, 12, 14])
Ejemplo n.º 15
0
 def test1_primitives_of_11(self):
     self.assertEqual(ModularArithmetics.primitives(11), [2, 6, 7, 8])
 def test_gcd_of_16_20(self):
     self.assertEqual(ModularArithmetics.gcd(16, 20), 4)
 def test_gcd_of_3_12(self):
     self.assertEqual(ModularArithmetics.gcd(3, 12), 3)
Ejemplo n.º 18
0
 def test1_chinese_remainder_theorem(self):
     self.assertEqual(
         ModularArithmetics.chinese_remainder_theorem([3, 4, 1], [4, 5, 7]),
         99)
Ejemplo n.º 19
0
 def test_mod_inv_of_13_mod_17(self):
     self.assertEqual(ModularArithmetics.modular_inverse(13, 17), 4)