diff --git a/zad1/Ring.java b/zad1/Ring.java new file mode 100644 index 0000000..794563b --- /dev/null +++ b/zad1/Ring.java @@ -0,0 +1,43 @@ +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.IntStream; + +public class Ring { + private int n; + + Ring(int n) { + this.n = n; + } + + public String findRingDetails() { + int[] reversibleElements = getReversibleElements(); + int[] zeroDivisors = getZeroDivisors(); + Set nilpotentElements = getNilpotentElements(zeroDivisors); + int[] idempotentElements = getIdempotentElements(); + return "[" + Arrays.toString(reversibleElements) + ", " + Arrays.toString(zeroDivisors) + ", " + nilpotentElements + ", " + Arrays.toString(idempotentElements) + "]"; + } + + private int[] getReversibleElements() { + return IntStream.rangeClosed(0, n - 1).filter(k -> NWDCalculator.findNWD(k, n) == 1).toArray(); + } + + private int[] getZeroDivisors() { + return IntStream.rangeClosed(0, n - 1).filter(k -> NWDCalculator.findNWD(k, n) > 1).toArray(); + } + + private Set getNilpotentElements(int[] zeroDivisors) { + Set nilpotentElements = new HashSet<>(); + for (int i = 0; i < n; i++) { + for (int i1 : zeroDivisors) { + if (Math.pow(i1, i) % n == 0) + nilpotentElements.add(i1); + } + } + return nilpotentElements; + } + + private int[] getIdempotentElements() { + return IntStream.rangeClosed(0, n - 1).filter(k -> k * k % n == k).toArray(); + } +}