Rozwiązanie zadania "Pierścień Zn" #5
11
README-Zad1.txt
Normal file
11
README-Zad1.txt
Normal file
@ -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
|
94
modulo.py
Normal file
94
modulo.py
Normal file
@ -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())
|
Loading…
Reference in New Issue
Block a user