diff --git a/chain_fraction.py b/chain_fraction.py index 5beb5af..9a78959 100644 --- a/chain_fraction.py +++ b/chain_fraction.py @@ -23,28 +23,24 @@ def get_p_n(n, q_list): def get_x0(a, b, m): q_list = get_q_list(m, a) n = len(q_list) - 1 - p = get_p_n(n-1, q_list) - x = ((-1)**n*p*b)%m - return x, f'x={x}mod{m}' + p = get_p_n(n-1, get_q_list(m, a)) + x0 = ((-1)**n*p*b)%m + return x0 def solve(task): f = re.fullmatch(r'(\d+)x=(\d+)mod(\d+)', task) if f == None: return "Usage Example: py chain_fraction.py '7x=8mod13'" a, b, m = int(f.groups()[0]), int(f.groups()[1]), int(f.groups()[2]) - d = gcd(a, m) # Находим НОД - if d == 1: # нод = 1 => 1 решение - return get_x0(a, b, m)[1] + d = gcd(a, m) if d > 1 and b % d != 0: return 'No solutions' - else: - d = gcd(gcd(a, b), m) - a1, b1, m1 = a // d, b // d, m // d - x0 = get_x0(a1, b1, m1)[0] - s = "" - for k in range(d): - s += f'x={(x0+m1*k)%m}mod{m}\n' - return s[:-1] + a1, b1, m1 = a // d, b // d, m // d + x0 = get_x0(a1, b1, m1) + s = "" + for i in range(d): + s += f'x={(x0+m//d*i)%m}mod{m}\n' + return s[:-1] if __name__ == "__main__": if len(sys.argv) != 2: diff --git a/euclid.py b/euclid.py index a4fb877..e1933fe 100644 --- a/euclid.py +++ b/euclid.py @@ -17,8 +17,7 @@ def solve(task): if d > 1 and b % d != 0: return 'No solutions' a1, b1, m1 = a // d, b // d, m // d - u, _, _ = gcd(a1, m1) - x0 = b1*u + x0 = b1*gcd(a1, m1)[0] s = "" for i in range(0, d): s += f'x={(x0+m//d*i)%m}mod{m}\n'