From 084753cc47229896a3c0624edfcf6f0abf904c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Bary=C5=82ka?= Date: Sun, 8 Jul 2018 22:22:29 +0000 Subject: [PATCH] =?UTF-8?q?Dodano=20brakuj=C4=85cy=20plik?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zad1/Ring.java | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 zad1/Ring.java 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(); + } +}