Zadanie 1 i 2 #38

Open
s416042 wants to merge 6 commits from s416042/DALGLI0:master into master
11 changed files with 201 additions and 0 deletions

4
readme Normal file
View File

@ -0,0 +1,4 @@
ZAD1
Program można uruchomić za pomocą polecenia: java Main 6
ZAD2
Program można uruchomić za pomocą polecenia: java Main "6" "[1,2,3,0,1]" "[2,4,0,0,2]"

BIN
zad1/Main.class Normal file

Binary file not shown.

18
zad1/Main.java Normal file
View File

@ -0,0 +1,18 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n;
if(args.length == 0) {
System.out.println("Proszę podać n ");
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
}
else{
n = Integer.parseInt(args[0]);
}
Ring ring = new Ring(n);
System.out.println(ring.findRingDetails());
}
}

BIN
zad1/NWDCalculator.class Normal file

Binary file not shown.

9
zad1/NWDCalculator.java Normal file
View File

@ -0,0 +1,9 @@
public class NWDCalculator {
public static int findNWD(int a, int b) {
if (b == 0)
return a;
else {
return findNWD(b, a % b);
}
}
}

BIN
zad1/Ring.class Normal file

Binary file not shown.

43
zad1/Ring.java Normal file
View File

@ -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<Integer> 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();
}
}

BIN
zad2/Main.class Normal file

Binary file not shown.

35
zad2/Main.java Normal file
View File

@ -0,0 +1,35 @@
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Proszę podać parametry np. java Main \"6\" \"[1,2,3,0,1]\" \"[2,4,0,0,2]\"");
return;
}
int modulo = Integer.parseInt(args[0]);
int[] polyF = stringToArray(args[1]);
int[] polyG = stringToArray(args[2]);
String multiple = Arrays.toString(PolynomialOperations.multiple(polyF, polyG, modulo));
int[] divisionArray = PolynomialOperations.divide(polyF, polyG, modulo);
String division;
if(divisionArray == null)
division = "DivisionError";
else if (divisionArray.length == 0)
division = "[0]";
else
division = Arrays.toString(divisionArray);
String nwd = Arrays.toString(PolynomialOperations.NWD(polyF, polyG, modulo));
if (nwd.equals("null"))
nwd = "DivisionError";
String outputString = "[" + multiple + ", " + division + ", " + nwd + "]";
System.out.println(outputString);
}
private static int[] stringToArray(String str) {
return Arrays.stream(str.substring(1, str.length() - 1).split(","))
.map(String::trim).mapToInt(Integer::parseInt).toArray();
}
}

Binary file not shown.

View File

@ -0,0 +1,92 @@
import java.util.Arrays;
public class PolynomialOperations {
public static int[] multiple(int[] f, int[] g, int n) {
int[] result = new int[f.length + g.length - 1];
for (int i = 0; i < f.length; i++)
for (int j = 0; j < g.length; j++)
result[i + j] += f[i] * g[j];
for (int i = 0; i < result.length; i++)
result[i] = result[i] % n;
return result;
}
private static int findFactor(int a, int b, int mod) {
for (int i = 0; i < mod; i++)
if (a == (b * i) % mod)
return i;
return Integer.MIN_VALUE;
}
private static int[] shr(int[] p, int shift) {
if (shift <= 0)
return p;
int[] shiftedPoly = new int[p.length];
for (int i = 0; i < p.length - shift; i++)
shiftedPoly[i] = p[i + shift];
return shiftedPoly;
}
private static void polyMultiply(int[] p, int n) {
Arrays.stream(p).forEach(value -> value = value * n);
}
private static int[] polySubtract(int[] p, int[] s) {
int result[] = Arrays.copyOf(p, p.length);
for (int i = 0; i < p.length; ++i) {
result[i] = p[i] - s[i];
}
return result;
}
private static int[] createNewPoly(int[] p, int size) {
int[] tmpTab = new int[size];
for (int j = 0; j < p.length; j++) {
tmpTab[tmpTab.length - 1 - j] = p[p.length - 1 - j];
}
return tmpTab;
}
private static void applyModuloToTab(int p[], int mod) {
Arrays.stream(p).forEach(value -> value = value % mod);
}
public static int[] divide(int[] n, int[] d, int mod) {
if (n.length < d.length) {
return null;
}
int nd = n.length - 1;
int dd = d.length - 1;
int index = 0;
int[] tmpTab = createNewPoly(d, n.length);
while (nd >= dd) {
int factor = findFactor(n[nd], d[dd], mod);
tmpTab = shr(tmpTab, index);
polyMultiply(tmpTab, factor);
applyModuloToTab(tmpTab, mod);
tmpTab = polySubtract(n, tmpTab);
nd--;
index++;
}
return Arrays.copyOf(n, index - 1);
}
public static int[] NWD(int[] a, int[] b, int mod) {
if (a.length <= 0 || b.length <= 0)
return null;
while (b.length != 0) {
int[] c;
if (a.length >= b.length)
c = divide(a, b, mod);
else
c = divide(b, a, mod);
a = b;
b = c;
}
return a;
}
}