From 821100b148dd0faee8bdb22d1db11ec17a847c80 Mon Sep 17 00:00:00 2001 From: Varmen8 Date: Wed, 30 May 2018 19:33:42 +0200 Subject: [PATCH] Add task 1 solution --- README-Zad1.txt | 11 ++++++ modulo.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 README-Zad1.txt create mode 100644 modulo.py diff --git a/README-Zad1.txt b/README-Zad1.txt new file mode 100644 index 0000000..b00f0f3 --- /dev/null +++ b/README-Zad1.txt @@ -0,0 +1,11 @@ +Program jest napisany w Python 3. + +Po wprowadzeniu liczby program nie czeka na kolejne dane. + +Co za tym idzie, okno zostanie zamknięte zaraz po wyświetleniu listy. + +Rozwiązaniem jest rzecz jasna wejście do folderu w emulatorze konsoli i wywołanie interpretera Pythona: + Na systemie Windows: + python modulo.py + Na systemach uniksopodobnych: + python3 modulo.py \ No newline at end of file diff --git a/modulo.py b/modulo.py new file mode 100644 index 0000000..98e22f4 --- /dev/null +++ b/modulo.py @@ -0,0 +1,94 @@ +import math + +def radical(n): + if n == 0: + return 0 + + factors = set() + i = 1 + + while n > 1: + if math.gcd(i, n) != 1: + n = n // i + factors.add(i) + else: + i = i + 1 + + rad = 1 + for factor in factors: + rad = rad * factor + + return rad + +class ModuloRing: + + def __init__(self, number): + self.number = number + self.elements = [x for x in range(number)] + self.radical = radical(number) + + def inverse_elements(self): + if self.number == 0: + return [] + + ring_inverses = [] + + for element in self.elements: + if math.gcd(element, self.number) == 1: + ring_inverses.append(element) + + return ring_inverses + + + def zero_divisor_elements(self): + if self.number == 0: + return [] + + ring_zero_divisors = set() + ring_zero_divisors.add(0) + + for element in self.elements: + if element != 0: + for second_element in self.elements: + if second_element != 0: + if (element * second_element) % self.number == 0: + ring_zero_divisors.add(element) + + return list(ring_zero_divisors) + + + def nilpotent_elements(self): + if self.number == 0: + return [] + + ring_nilpotents = [] + + for element in self.elements: + if math.gcd(element, self.radical) == self.radical: + ring_nilpotents.append(element) + + return ring_nilpotents + + def idempotent_elements(self): + if self.number == 0: + return [] + + ring_idempotents = [] + + for element in self.elements: + if (element ** 2) % self.number == element: + ring_idempotents.append(element) + + return ring_idempotents + + def get_lists(self): + ring_list = [] + ring_list.append(self.inverse_elements()) + ring_list.append(self.zero_divisor_elements()) + ring_list.append(self.nilpotent_elements()) + ring_list.append(self.idempotent_elements()) + return ring_list + +if __name__ == "__main__": + modulo_ring = ModuloRing(int(input())) + print(modulo_ring.get_lists()) \ No newline at end of file -- 2.20.1