From 0be2be0a92f992bf8ee9eff701cb19658a1e7544 Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Thu, 29 Dec 2022 15:20:32 +0400 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B?= =?UTF-8?q?=20=D0=BB=D0=B0=D0=B1=D1=8B=208-15?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lab15/lab15.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 lab15/lab15.py (limited to 'lab15/lab15.py') diff --git a/lab15/lab15.py b/lab15/lab15.py new file mode 100644 index 0000000..fa0a3ef --- /dev/null +++ b/lab15/lab15.py @@ -0,0 +1,81 @@ +def get_all_divisors_brute(n): + n = n if n > 0 else -n + for i in range(1, int(n / 2) + 1): + if n % i == 0: + yield i + yield n + + +def get_poly_value(i, cf, x): + if i == 0: + return cf[i] + else: + value = get_poly_value(i - 1, cf, x) + return cf[i] + x * value + + +def divide(n, cfs): + new_cfs = [cfs[0]] + current = cfs[0] + + for i in cfs[1::]: + current = current * n + i + new_cfs.append(current) + + if new_cfs[-1] == 0: + return True, new_cfs[:-1] + else: + return False, cfs + + +def horner_schema(poly_cf, dividers={}): + divisors = get_all_divisors_brute(poly_cf[-1]) + for i in divisors: + res, cfs = divide(i, poly_cf) + + if res: + poly_cf = cfs + if i not in dividers.keys(): + dividers[i] = 1 + else: + dividers[i] += 1 + return horner_schema(poly_cf, dividers) + else: + another_res, another_new_cfs = divide(-i, poly_cf) + if another_res: + poly_cf = another_new_cfs + if -i not in dividers.keys(): + dividers[-i] = 1 + else: + dividers[-i] += 1 + return horner_schema(poly_cf, dividers) + + return poly_cf, dividers + + +def get_roots_from_dict(roots): + roots = list(roots.keys()) + if roots == []: + print("Многочлен неприводим") + else: + print("Список корней многочлена:", roots) + + +if __name__ == "__main__": + print('Введите коэффициенты многочлена: ') + cf = list(map(int, input().split())) + n = len(cf) - 1 + + print("================================") + print("== Вычисление корней полинома ==") + print("================================") + _, root_dict = horner_schema(cf) + get_roots_from_dict(root_dict) + + print() + print("==================================") + print("== Вычисление значения полинома ==") + print("==================================") + x = int(input("Введите x: ")) + y = get_poly_value(n, cf, x) + print(f"Значение полинома в точке {x} = {y}") -- cgit v1.2.3