From d100a1f9b1b80ddc4c96b0e010733184e8d202f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 6 Jun 2018 20:04:45 +0000 Subject: [PATCH 1/9] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw2.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 hw2.py diff --git a/hw2.py b/hw2.py new file mode 100644 index 0000000..c15a1df --- /dev/null +++ b/hw2.py @@ -0,0 +1,121 @@ +import sys +import ast +import operator +from fractions import gcd + +class Polynomial(): + def __init__(self, lst, mod): + super().__init__() + self.poly = list(map(lambda x: x % mod, lst)) + self.mod = mod + self.normalize() + def normalize(self): + while self.poly and self.poly[-1] == 0: + self.poly.pop() + + def arithm(self, p1, p2, op): + len_p1, len_p2= len(p1.poly), len(p2.poly) + res = [0] * max(len_p1, len_p2) + if len_p1 > len_p2: + for _ in range(len_p1-len_p2): + p2.poly.append(0) + else: + for _ in range(len_p2-len_p1): + p1.poly.append(0) + + for i in range(len(res)): + res[i] = op(p1.poly[i], p2.poly[i]) % self.mod + return Polynomial(res, self.mod) + + def __add__(self, p2): + return self.arithm(self, p2, operator.add) + + def __sub__(self, p2): + return self.arithm(self, p2, operator.sub) + + def __mul__(self, p2): + res = [0]*(len(self.poly)+len(p2.poly)-1) + for i, x1 in enumerate(self.poly): + for j, x2 in enumerate(p2.poly): + res[i+j] += x1 * x2 % self.mod + return Polynomial(res, self.mod) + + def __truediv__(self, p2): + p1 = self + m = self.mod + + if len(p1.poly) < len(p2.poly): + return p1 + + if len(p2.poly) == 0: + raise ZeroDivisionError + + divisor_coeff = p2.poly[-1] + divisor_exp = len(p2.poly) - 1 + while len(p1.poly) >= len(p2.poly): + max_coeff_p1 = p1.poly[-1] #wspolczynnik przy najwyzszej potedze + try: + tmp_coeff = modDiv(max_coeff_p1, divisor_coeff, m) + except ZeroDivisionError as e: + raise e + + tmp_exp = len(p1.poly)-1 - divisor_exp + '''tmp to pomocniczy wielomian o reprezentacji [0, 0, .., c^n], gdzie c^n to + mnożnik w danym kroku algorytmu dzielenia w słupku. + Następnie mnożymy go z wielomianem-dzielnikiem (p2) i + odejmujemy (sub) od wielomianu-dzielnej (p1)''' + tmp = [] + for i in range(tmp_exp): + tmp.append(0) + tmp.append(tmp_coeff) + sub = Polynomial(tmp, m) * p2 + p1 = p1 - sub + p1.normalize() #obcinamy zbędne zera dopisane do wielomianu podczas odejmowania + return Polynomial(p1.poly, m) + + def poly_gcd(self, p2): + p1 = self + try: + divisible = p2 + except ZeroDivisionError as e: + raise e + if p2.poly == []: + return p1 + return p2.poly_gcd(p1 / p2) + +def modDiv(a, b, m): # a*b^-1 (mod m) + if gcd(b, m) != 1: + raise ZeroDivisionError + else: + return (a * modinv(b, m)) % m + +#rozszerzony algorytm euklidesa +def egcd(a, b): + if a == 0: + return (b, 0, 1) + else: + g, y, x = egcd(b % a, a) + return (g, x - (b // a) * y, y) + +def modinv(a, m): + g, x, y = egcd(a, m) + return x % m + +def main(): + n, p1, p2 = int(sys.argv[1]), ast.literal_eval(sys.argv[2]), ast.literal_eval(sys.argv[3]) + P1 = Polynomial(p1, n) + P2 = Polynomial(p2, n) + mul = (P1 * P2).poly + try: + div = (P1 / P2).poly + except ZeroDivisionError as e: + div = e + try: + gcd = P1.poly_gcd(P2).poly + except ZeroDivisionError as e: + gcd = e + print([mul, div, gcd]) + + +if __name__ == '__main__': + main() \ No newline at end of file -- 2.20.1 From 27221a47c829dbf5da6ad04951fc02b1a7488c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 6 Jun 2018 20:10:27 +0000 Subject: [PATCH 2/9] Dodaj 'README.md' --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..b21da80 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +Program przyjmuje input z konsoli (argv) i korzysta z funkcji eval działającej poprawnie w obecności spacji między elementami listy. +Przykładowe uruchomienie: python hw2.py 2 [1,1,1,0,1] [0,1,1] \ No newline at end of file -- 2.20.1 From 128cac8f556adff821cab6c35a59fac1c9fdd254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 6 Jun 2018 20:11:03 +0000 Subject: [PATCH 3/9] Zaktualizuj 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b21da80..310f6c4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -Program przyjmuje input z konsoli (argv) i korzysta z funkcji eval działającej poprawnie w obecności spacji między elementami listy. +Program przyjmuje input z konsoli (argv) i korzysta z funkcji eval niedziałającej poprawnie w obecności spacji między elementami listy. Przykładowe uruchomienie: python hw2.py 2 [1,1,1,0,1] [0,1,1] \ No newline at end of file -- 2.20.1 From 3ff8beeed6e787ec435f2799cb064efbfc54318d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 27 Jun 2018 17:24:40 +0000 Subject: [PATCH 4/9] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw4.py | 84 +++++++++++++++++++++++++++++++++++++++++ poly.py | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 hw4.py create mode 100644 poly.py diff --git a/hw4.py b/hw4.py new file mode 100644 index 0000000..11758db --- /dev/null +++ b/hw4.py @@ -0,0 +1,84 @@ +from poly import Polynomial +from sys import argv +from ast import literal_eval +from fractions import gcd + +class QuotientRing(): + def __init__(self, f, m): + self.f = Polynomial(f, m) + self.m = m + self.remainders = self.remainders() + self.reversibles = self.reversibles() + self.zero_divisors = self.zero_divisors() + self.idempotent = self.idempotent() + self.nilpotent = self.nilpotent() + + def remainders(self): #n - exponent + rems = [] #lista reszt + m = self.m + t = [0] + i = 0 + while len(t) < len(self.f.poly): + rems.append(Polynomial(t, m)) + i = (i + 1) % m + t[0] = i + if i == 0: + if len(t) == 1: + t.append(1) + else: + t[1] += 1 + for j in range(1, len(t)): + if t[j] == 0 or t[j] % m != 0: + break + temp = t[j] % m + t[j] = 0 + if temp == 0: + if (j + 1) < len(t): + t[j+1] += 1 + else: + t.append(1) + return rems + + def reversibles(self): + return [ rem for rem in self.remainders if len(rem.poly_gcd(self.f).poly) == 1 ] + + #dopelnienie elementow odwracalnych + def zero_divisors(self): + return [ rem for rem in self.remainders if rem not in self.reversibles ] + + def idempotent(self): + idems = [] + for rem in self.remainders: + if (rem * rem / self.f) == (rem / self.f): + idems.append(rem) + try: + if idems[0].poly == []: #implementacja wielomianow ucina zera + idems[0].poly = [0] + except IndexError: + return idems + return idems + + def nilpotent(self): + nils = [] + phi = len([ i for i in range(1, self.m) if gcd(i, self.m) == 1 ]) + for zero_div in self.zero_divisors: + for i in range(self.m): + if len((zero_div ** i / self.f).poly) == 0: + nils.append(zero_div) + break + return nils + +def main(): + m = int(argv[1]) + f = literal_eval(argv[2]) + qr = QuotientRing(f, m) + out = [ + [ rev.poly for rev in qr.reversibles ], + [ zero_div.poly for zero_div in qr.zero_divisors ], + [ nil.poly for nil in qr.nilpotent ], + [ idem.poly for idem in qr.idempotent ] + ] + print(*out, sep='\n') + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/poly.py b/poly.py new file mode 100644 index 0000000..28473bd --- /dev/null +++ b/poly.py @@ -0,0 +1,115 @@ +from fractions import gcd +class Polynomial(): + def __init__(self, lst, mod): + self.poly = list(map(lambda x: x % mod, lst)) + self.mod = mod + self.normalize() + def normalize(self): + while self.poly and self.poly[-1] == 0: + self.poly.pop() + + #zwraca jednomian stopnia n + @staticmethod + def Monomial(n, c, mod): + zeros = [0]*n + + zeros.append(c) + return Polynomial(zeros, mod) + + def __add__(self, p2): + p1 = self + len_p1, len_p2= len(p1.poly), len(p2.poly) + res = [0] * max(len_p1, len_p2) + if len_p1 > len_p2: + for _ in range(len_p1-len_p2): + p2.poly.append(0) + else: + for _ in range(len_p2-len_p1): + p1.poly.append(0) + + for i in range(len(res)): + res[i] = (p1.poly[i] + p2.poly[i]) % self.mod + return Polynomial(res, self.mod) + + def __sub__(self, p2): + p1 = self + res = [] + len_p2 = len(p2.poly) + for i in range(len(p1.poly)): + if i < len_p2: + res.append(p1.poly[i] - p2.poly[i] % self.mod) + else: + res.append(p1.poly[i]) + return Polynomial(res, self.mod) + + def __mul__(self, p2): + res = [0]*(len(self.poly)+len(p2.poly)-1) + for i, x1 in enumerate(self.poly): + for j, x2 in enumerate(p2.poly): + res[i+j] += x1 * x2 % self.mod + return Polynomial(res, self.mod) + + def __eq__(self, p2): + p1 = self + return p1.poly == p2.poly and p1.mod == p2.mod + + def __pow__(self, n): + p1 = self + for i in range(n): + p1 = p1 * p1 + return p1 + + def __truediv__(self, p2): + p1 = self + m = self.mod + + if len(p1.poly) < len(p2.poly): + return p1 + + if len(p2.poly) == 0: + raise ZeroDivisionError + + divisor_coeff = p2.poly[-1] + divisor_exp = len(p2.poly) - 1 + while len(p1.poly) >= len(p2.poly): + max_coeff_p1 = p1.poly[-1] #wspolczynnik przy najwyzszej potedze + try: + tmp_coeff = modDiv(max_coeff_p1, divisor_coeff, m) + except ZeroDivisionError as e: + raise e + + tmp_exp = len(p1.poly)-1 - divisor_exp + tmp = [0] * tmp_exp + tmp.append(tmp_coeff) + sub = Polynomial(tmp, m) * p2 + p1 = p1 - sub + p1.normalize() + return Polynomial(p1.poly, m) + + def poly_gcd(self, p2): + p1 = self + try: + divisible = p2 + except ZeroDivisionError as e: + raise e + if p2.poly == []: + return p1 + + return p2.poly_gcd(p1 / p2) + +def modDiv(a, b, m): # a*b^-1 (mod m) + if gcd(b, m) != 1: + raise ZeroDivisionError + else: + return (a * modinv(b, m)) % m +#rozszerzony algorytm euklidesa +def egcd(a, b): + if a == 0: + return (b, 0, 1) + else: + g, y, x = egcd(b % a, a) + return (g, x - (b // a) * y, y) + +def modinv(a, m): + g, x, y = egcd(a, m) + return x % m \ No newline at end of file -- 2.20.1 From e4d33d8aae77a0e8a3d79afbef9ed4f9191f50ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 27 Jun 2018 17:25:24 +0000 Subject: [PATCH 5/9] =?UTF-8?q?Usu=C5=84=20'README.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 310f6c4..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -Program przyjmuje input z konsoli (argv) i korzysta z funkcji eval niedziałającej poprawnie w obecności spacji między elementami listy. -Przykładowe uruchomienie: python hw2.py 2 [1,1,1,0,1] [0,1,1] \ No newline at end of file -- 2.20.1 From f4885f813b15d9a8395bd82c084dce71bd12b714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 27 Jun 2018 17:25:37 +0000 Subject: [PATCH 6/9] =?UTF-8?q?Usu=C5=84=20'hw2.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw2.py | 121 --------------------------------------------------------- 1 file changed, 121 deletions(-) delete mode 100644 hw2.py diff --git a/hw2.py b/hw2.py deleted file mode 100644 index c15a1df..0000000 --- a/hw2.py +++ /dev/null @@ -1,121 +0,0 @@ -import sys -import ast -import operator -from fractions import gcd - -class Polynomial(): - def __init__(self, lst, mod): - super().__init__() - self.poly = list(map(lambda x: x % mod, lst)) - self.mod = mod - self.normalize() - def normalize(self): - while self.poly and self.poly[-1] == 0: - self.poly.pop() - - def arithm(self, p1, p2, op): - len_p1, len_p2= len(p1.poly), len(p2.poly) - res = [0] * max(len_p1, len_p2) - if len_p1 > len_p2: - for _ in range(len_p1-len_p2): - p2.poly.append(0) - else: - for _ in range(len_p2-len_p1): - p1.poly.append(0) - - for i in range(len(res)): - res[i] = op(p1.poly[i], p2.poly[i]) % self.mod - return Polynomial(res, self.mod) - - def __add__(self, p2): - return self.arithm(self, p2, operator.add) - - def __sub__(self, p2): - return self.arithm(self, p2, operator.sub) - - def __mul__(self, p2): - res = [0]*(len(self.poly)+len(p2.poly)-1) - for i, x1 in enumerate(self.poly): - for j, x2 in enumerate(p2.poly): - res[i+j] += x1 * x2 % self.mod - return Polynomial(res, self.mod) - - def __truediv__(self, p2): - p1 = self - m = self.mod - - if len(p1.poly) < len(p2.poly): - return p1 - - if len(p2.poly) == 0: - raise ZeroDivisionError - - divisor_coeff = p2.poly[-1] - divisor_exp = len(p2.poly) - 1 - while len(p1.poly) >= len(p2.poly): - max_coeff_p1 = p1.poly[-1] #wspolczynnik przy najwyzszej potedze - try: - tmp_coeff = modDiv(max_coeff_p1, divisor_coeff, m) - except ZeroDivisionError as e: - raise e - - tmp_exp = len(p1.poly)-1 - divisor_exp - '''tmp to pomocniczy wielomian o reprezentacji [0, 0, .., c^n], gdzie c^n to - mnożnik w danym kroku algorytmu dzielenia w słupku. - Następnie mnożymy go z wielomianem-dzielnikiem (p2) i - odejmujemy (sub) od wielomianu-dzielnej (p1)''' - tmp = [] - for i in range(tmp_exp): - tmp.append(0) - tmp.append(tmp_coeff) - sub = Polynomial(tmp, m) * p2 - p1 = p1 - sub - p1.normalize() #obcinamy zbędne zera dopisane do wielomianu podczas odejmowania - return Polynomial(p1.poly, m) - - def poly_gcd(self, p2): - p1 = self - try: - divisible = p2 - except ZeroDivisionError as e: - raise e - if p2.poly == []: - return p1 - return p2.poly_gcd(p1 / p2) - -def modDiv(a, b, m): # a*b^-1 (mod m) - if gcd(b, m) != 1: - raise ZeroDivisionError - else: - return (a * modinv(b, m)) % m - -#rozszerzony algorytm euklidesa -def egcd(a, b): - if a == 0: - return (b, 0, 1) - else: - g, y, x = egcd(b % a, a) - return (g, x - (b // a) * y, y) - -def modinv(a, m): - g, x, y = egcd(a, m) - return x % m - -def main(): - n, p1, p2 = int(sys.argv[1]), ast.literal_eval(sys.argv[2]), ast.literal_eval(sys.argv[3]) - P1 = Polynomial(p1, n) - P2 = Polynomial(p2, n) - mul = (P1 * P2).poly - try: - div = (P1 / P2).poly - except ZeroDivisionError as e: - div = e - try: - gcd = P1.poly_gcd(P2).poly - except ZeroDivisionError as e: - gcd = e - print([mul, div, gcd]) - - -if __name__ == '__main__': - main() \ No newline at end of file -- 2.20.1 From d2187f2dbb8062a1bdf8bc1c12c8d3cd1b28e057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 27 Jun 2018 17:48:31 +0000 Subject: [PATCH 7/9] __truediv__ to __mod__ --- poly.py | 228 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 114 insertions(+), 114 deletions(-) diff --git a/poly.py b/poly.py index 28473bd..5b1d7d8 100644 --- a/poly.py +++ b/poly.py @@ -1,115 +1,115 @@ -from fractions import gcd -class Polynomial(): - def __init__(self, lst, mod): - self.poly = list(map(lambda x: x % mod, lst)) - self.mod = mod - self.normalize() - def normalize(self): - while self.poly and self.poly[-1] == 0: - self.poly.pop() - - #zwraca jednomian stopnia n - @staticmethod - def Monomial(n, c, mod): - zeros = [0]*n - - zeros.append(c) - return Polynomial(zeros, mod) - - def __add__(self, p2): - p1 = self - len_p1, len_p2= len(p1.poly), len(p2.poly) - res = [0] * max(len_p1, len_p2) - if len_p1 > len_p2: - for _ in range(len_p1-len_p2): - p2.poly.append(0) - else: - for _ in range(len_p2-len_p1): - p1.poly.append(0) - - for i in range(len(res)): - res[i] = (p1.poly[i] + p2.poly[i]) % self.mod - return Polynomial(res, self.mod) - - def __sub__(self, p2): - p1 = self - res = [] - len_p2 = len(p2.poly) - for i in range(len(p1.poly)): - if i < len_p2: - res.append(p1.poly[i] - p2.poly[i] % self.mod) - else: - res.append(p1.poly[i]) - return Polynomial(res, self.mod) - - def __mul__(self, p2): - res = [0]*(len(self.poly)+len(p2.poly)-1) - for i, x1 in enumerate(self.poly): - for j, x2 in enumerate(p2.poly): - res[i+j] += x1 * x2 % self.mod - return Polynomial(res, self.mod) - - def __eq__(self, p2): - p1 = self - return p1.poly == p2.poly and p1.mod == p2.mod - - def __pow__(self, n): - p1 = self - for i in range(n): - p1 = p1 * p1 - return p1 - - def __truediv__(self, p2): - p1 = self - m = self.mod - - if len(p1.poly) < len(p2.poly): - return p1 - - if len(p2.poly) == 0: - raise ZeroDivisionError - - divisor_coeff = p2.poly[-1] - divisor_exp = len(p2.poly) - 1 - while len(p1.poly) >= len(p2.poly): - max_coeff_p1 = p1.poly[-1] #wspolczynnik przy najwyzszej potedze - try: - tmp_coeff = modDiv(max_coeff_p1, divisor_coeff, m) - except ZeroDivisionError as e: - raise e - - tmp_exp = len(p1.poly)-1 - divisor_exp - tmp = [0] * tmp_exp - tmp.append(tmp_coeff) - sub = Polynomial(tmp, m) * p2 - p1 = p1 - sub - p1.normalize() - return Polynomial(p1.poly, m) - - def poly_gcd(self, p2): - p1 = self - try: - divisible = p2 - except ZeroDivisionError as e: - raise e - if p2.poly == []: - return p1 - - return p2.poly_gcd(p1 / p2) - -def modDiv(a, b, m): # a*b^-1 (mod m) - if gcd(b, m) != 1: - raise ZeroDivisionError - else: - return (a * modinv(b, m)) % m -#rozszerzony algorytm euklidesa -def egcd(a, b): - if a == 0: - return (b, 0, 1) - else: - g, y, x = egcd(b % a, a) - return (g, x - (b // a) * y, y) - -def modinv(a, m): - g, x, y = egcd(a, m) +from fractions import gcd +class Polynomial(): + def __init__(self, lst, mod): + self.poly = list(map(lambda x: x % mod, lst)) + self.mod = mod + self.normalize() + def normalize(self): + while self.poly and self.poly[-1] == 0: + self.poly.pop() + + #zwraca jednomian stopnia n + @staticmethod + def Monomial(n, c, mod): + zeros = [0]*n + + zeros.append(c) + return Polynomial(zeros, mod) + + def __add__(self, p2): + p1 = self + len_p1, len_p2= len(p1.poly), len(p2.poly) + res = [0] * max(len_p1, len_p2) + if len_p1 > len_p2: + for _ in range(len_p1-len_p2): + p2.poly.append(0) + else: + for _ in range(len_p2-len_p1): + p1.poly.append(0) + + for i in range(len(res)): + res[i] = (p1.poly[i] + p2.poly[i]) % self.mod + return Polynomial(res, self.mod) + + def __sub__(self, p2): + p1 = self + res = [] + len_p2 = len(p2.poly) + for i in range(len(p1.poly)): + if i < len_p2: + res.append(p1.poly[i] - p2.poly[i] % self.mod) + else: + res.append(p1.poly[i]) + return Polynomial(res, self.mod) + + def __mul__(self, p2): + res = [0]*(len(self.poly)+len(p2.poly)-1) + for i, x1 in enumerate(self.poly): + for j, x2 in enumerate(p2.poly): + res[i+j] += x1 * x2 % self.mod + return Polynomial(res, self.mod) + + def __eq__(self, p2): + p1 = self + return p1.poly == p2.poly and p1.mod == p2.mod + + def __pow__(self, n): + p1 = self + for i in range(n): + p1 = p1 * p1 + return p1 + + def __mod__(self, p2): + p1 = self + m = self.mod + + if len(p1.poly) < len(p2.poly): + return p1 + + if len(p2.poly) == 0: + raise ZeroDivisionError + + divisor_coeff = p2.poly[-1] + divisor_exp = len(p2.poly) - 1 + while len(p1.poly) >= len(p2.poly): + max_coeff_p1 = p1.poly[-1] #wspolczynnik przy najwyzszej potedze + try: + tmp_coeff = modDiv(max_coeff_p1, divisor_coeff, m) + except ZeroDivisionError as e: + raise e + + tmp_exp = len(p1.poly)-1 - divisor_exp + tmp = [0] * tmp_exp + tmp.append(tmp_coeff) + sub = Polynomial(tmp, m) * p2 + p1 = p1 - sub + p1.normalize() + return Polynomial(p1.poly, m) + + def poly_gcd(self, p2): + p1 = self + try: + divisible = p2 + except ZeroDivisionError as e: + raise e + if p2.poly == []: + return p1 + + return p2.poly_gcd(p1 % p2) + +def modDiv(a, b, m): # a*b^-1 (mod m) + if gcd(b, m) != 1: + raise ZeroDivisionError + else: + return (a * modinv(b, m)) % m +#rozszerzony algorytm euklidesa +def egcd(a, b): + if a == 0: + return (b, 0, 1) + else: + g, y, x = egcd(b % a, a) + return (g, x - (b // a) * y, y) + +def modinv(a, m): + g, x, y = egcd(a, m) return x % m \ No newline at end of file -- 2.20.1 From b34b9a42fbf0737d09759f91f54e068f7cf7b4da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 27 Jun 2018 17:48:59 +0000 Subject: [PATCH 8/9] / to % --- hw4.py | 166 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/hw4.py b/hw4.py index 11758db..ce11219 100644 --- a/hw4.py +++ b/hw4.py @@ -1,84 +1,84 @@ -from poly import Polynomial -from sys import argv -from ast import literal_eval -from fractions import gcd - -class QuotientRing(): - def __init__(self, f, m): - self.f = Polynomial(f, m) - self.m = m - self.remainders = self.remainders() - self.reversibles = self.reversibles() - self.zero_divisors = self.zero_divisors() - self.idempotent = self.idempotent() - self.nilpotent = self.nilpotent() - - def remainders(self): #n - exponent - rems = [] #lista reszt - m = self.m - t = [0] - i = 0 - while len(t) < len(self.f.poly): - rems.append(Polynomial(t, m)) - i = (i + 1) % m - t[0] = i - if i == 0: - if len(t) == 1: - t.append(1) - else: - t[1] += 1 - for j in range(1, len(t)): - if t[j] == 0 or t[j] % m != 0: - break - temp = t[j] % m - t[j] = 0 - if temp == 0: - if (j + 1) < len(t): - t[j+1] += 1 - else: - t.append(1) - return rems - - def reversibles(self): - return [ rem for rem in self.remainders if len(rem.poly_gcd(self.f).poly) == 1 ] - - #dopelnienie elementow odwracalnych - def zero_divisors(self): - return [ rem for rem in self.remainders if rem not in self.reversibles ] - - def idempotent(self): - idems = [] - for rem in self.remainders: - if (rem * rem / self.f) == (rem / self.f): - idems.append(rem) - try: - if idems[0].poly == []: #implementacja wielomianow ucina zera - idems[0].poly = [0] - except IndexError: - return idems - return idems - - def nilpotent(self): - nils = [] - phi = len([ i for i in range(1, self.m) if gcd(i, self.m) == 1 ]) - for zero_div in self.zero_divisors: - for i in range(self.m): - if len((zero_div ** i / self.f).poly) == 0: - nils.append(zero_div) - break - return nils - -def main(): - m = int(argv[1]) - f = literal_eval(argv[2]) - qr = QuotientRing(f, m) - out = [ - [ rev.poly for rev in qr.reversibles ], - [ zero_div.poly for zero_div in qr.zero_divisors ], - [ nil.poly for nil in qr.nilpotent ], - [ idem.poly for idem in qr.idempotent ] - ] - print(*out, sep='\n') - -if __name__ == '__main__': +from poly import Polynomial +from sys import argv +from ast import literal_eval +from fractions import gcd + +class QuotientRing(): + def __init__(self, f, m): + self.f = Polynomial(f, m) + self.m = m + self.remainders = self.remainders() + self.reversibles = self.reversibles() + self.zero_divisors = self.zero_divisors() + self.idempotent = self.idempotent() + self.nilpotent = self.nilpotent() + + def remainders(self): #n - exponent + rems = [] #lista reszt + m = self.m + t = [0] + i = 0 + while len(t) < len(self.f.poly): + rems.append(Polynomial(t, m)) + i = (i + 1) % m + t[0] = i + if i == 0: + if len(t) == 1: + t.append(1) + else: + t[1] += 1 + for j in range(1, len(t)): + if t[j] == 0 or t[j] % m != 0: + break + temp = t[j] % m + t[j] = 0 + if temp == 0: + if (j + 1) < len(t): + t[j+1] += 1 + else: + t.append(1) + return rems + + def reversibles(self): + return [ rem for rem in self.remainders if len(rem.poly_gcd(self.f).poly) == 1 ] + + #dopelnienie elementow odwracalnych + def zero_divisors(self): + return [ rem for rem in self.remainders if rem not in self.reversibles ] + + def idempotent(self): + idems = [] + for rem in self.remainders: + if (rem * rem % self.f) == (rem % self.f): + idems.append(rem) + try: + if idems[0].poly == []: #implementacja wielomianow ucina zera + idems[0].poly = [0] + except IndexError: + return idems + return idems + + def nilpotent(self): + nils = [] + phi = len([ i for i in range(1, self.m) if gcd(i, self.m) == 1 ]) + for zero_div in self.zero_divisors: + for i in range(self.m): + if len((zero_div ** i % self.f).poly) == 0: + nils.append(zero_div) + break + return nils + +def main(): + m = int(argv[1]) + f = literal_eval(argv[2]) + qr = QuotientRing(f, m) + out = [ + [ rev.poly for rev in qr.reversibles ], + [ zero_div.poly for zero_div in qr.zero_divisors ], + [ nil.poly for nil in qr.nilpotent ], + [ idem.poly for idem in qr.idempotent ] + ] + print(*out, sep='\n') + +if __name__ == '__main__': main() \ No newline at end of file -- 2.20.1 From d918bd989eeb953e9b2c1302280ab95c8add8519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksy=20Wr=C3=B3blewski?= Date: Wed, 27 Jun 2018 17:57:28 +0000 Subject: [PATCH 9/9] Zaktualizuj 'hw4.py' --- hw4.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hw4.py b/hw4.py index ce11219..f8c83ed 100644 --- a/hw4.py +++ b/hw4.py @@ -62,7 +62,7 @@ class QuotientRing(): nils = [] phi = len([ i for i in range(1, self.m) if gcd(i, self.m) == 1 ]) for zero_div in self.zero_divisors: - for i in range(self.m): + for i in range(phi+1): if len((zero_div ** i % self.f).poly) == 0: nils.append(zero_div) break -- 2.20.1