def resolve_indication(a, b, l, m): # find inverse and recount a if get_inverse_of(m, b) is None: raise RuntimeError('No solution!') a = int(a * get_inverse_of(m, b) % m) # find index and recount a a = index = get_index_for(m, a) # find betta as euler(m) betta = get_euler(m)[0] # check possible nod = get_nod(l, betta) if not is_solution_possible(index, nod): raise RuntimeError("No solution, because (d,index) !=0") # cut the equation m = int(betta / nod) a = int(a / nod) b = int(b / nod) l = int(l / nod) # final cut b = int(b * get_inverse_of(m, a) % m) print(f'x = {find_root_and_system(m, False)[0]}^({b} + {m}k)') return None
def find_adjacent_classes(self, m): result = [] if self.type == OperationType.MULTIPLE: count_anjacent_classes = euler(m)[0] / len(self.elements) - 1 for i in range(1, m): if count_anjacent_classes == 0: break if get_nod(i, m) != 1 or i in self.elements: continue tmp = [] for each in self.elements: tmp.append(each * i % m) count_anjacent_classes -= 1 result.append(tmp) elif self.type == OperationType.ADDITION: if euler(m)[0] == m - 1: return [] for i in range(1, int(m / len(self.elements))): tmp = [] for each in self.elements: tmp.append(int((each + i) % m)) result.append(tmp) return result
def get_params(equation_str: str): equations = [] # get parameters for equation_str in equation_str.split(';'): b = 0 m = 0 tmp_b = re.search(r'=[1-9][0-9]*\(', equation_str) tmp_m = re.search(r'mod[1-9][0-9]*', equation_str) # get parameter - b if tmp_b is None: raise RuntimeError(f'Ooops... Can not find parameter b.') else: b = tmp_b.group(0)[1:-1] # get parameter - m if tmp_m is None: raise RuntimeError(f'Ooops... Can not find parameter m.') else: m = tmp_m.group(0)[3:] equations.append(ChineseEquation(b, m, equation_str)) # validate nod for i in range(len(equations) - 1): nod_m1_m2 = nod_script_1_2.get_nod(equations[i].m, equations[i + 1].m) if nod_m1_m2 != 1: raise RuntimeError(f'Ooops... Equation has no solution because nod(' f'{equations[i].m},{equations[i + 1].m}) = {nod_m1_m2}.') return equations
def compresParam(a, b, m, printSolution: bool): # 74x=69(mod7) => 4x=6(mod7) # 21x=35(mod14) => 7x=7x(mod14) if abs(a) > m: a = int(a % m) if abs(b) > m: b = int(b % m) if nod_script_1_2.get_nod(m, a) == nod_script_1_2.get_nod(m, b): tmp_m = int(m / nod_script_1_2.get_nod(m, a)) a = int(a / nod_script_1_2.get_nod(m, a)) b = int(b / nod_script_1_2.get_nod(m, b)) m = tmp_m if printSolution: print(f'compress: {a}x={b}(mod{m})') return a, b, m
def order_addition_group(m: int): elem_basis = [] if isPrimary(m): return list(map(lambda a: f'ord({a})={m}', range(1, m + 1))) for a in range(1, m + 1): if get_nod(a, m) == 1: elem_basis.append(f'ord({a})={m}') else: for k in range(1, m): if a * k % m == 0: elem_basis.append(f'ord({a})={k}') break return elem_basis
def find_root_and_system(m: int, print_solution: bool): eul = euler_script_5.euler(m)[0] canonic_number = canonic_view_script_4.to_list_prime(canonic_view_script_4.prime_factors_list(eul)) if print_solution: print(f'Euler: φ({m}) = {eul}') print(f'Canon view: {eul} = {canonic_view_script_4.pretty_str(canonic_view_script_4.prime_factors_list(eul))}') a = 2 while a < eul: isRoot = True for each in canonic_number: if isRoot: degree = int(eul / each) if (a ** degree) % m == 1: isRoot = False else: break if isRoot and nod_script_1_2.get_nod(a, m) == 1: system = get_system(a, canonic_view_script_4.get_all_prime(m), m) return a, system a += 1 return -1, []
def resolve_degrees(a, b, m): a = int(get_index_for(m, a)) b = int(get_index_for(m, b)) if a == 0 or b == 0: raise RuntimeError('No solution!') # find betta as euler(m) betta = get_euler(m)[0] # check possible nod = get_nod(b, betta) if not is_solution_possible(a, nod): raise RuntimeError("No solution, because (a,index) !=0") a = int(a / nod) b = int(b / nod) m = int(betta / nod) # final cut a = int(a * get_inverse_of(m, b) % m) print(f'x = {a} + {m}k ')
def isPrimeNumber(numOne, numTwo, printSolution: bool = False): nod_am = nod_script_1_2.get_nod(numOne, numTwo) if printSolution: print(f'nod({numOne},{numTwo}) = {nod_am}') return nod_am == 1