From aea8d85191565ea5a209c11044dae933e5c8b0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Bary=C5=82ka?= Date: Sun, 8 Jul 2018 22:24:39 +0000 Subject: [PATCH] Zadanie 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Program można uruchomić za pomocą polecenia java Main "6" "[1,2,3,0,1]" "[2,4,0,0,2]" --- zad2/Main.class | Bin 0 -> 2853 bytes zad2/Main.java | 34 ++++++++++++ zad2/PolynomialOperations.class | Bin 0 -> 3314 bytes zad2/PolynomialOperations.java | 88 ++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 zad2/Main.class create mode 100644 zad2/Main.java create mode 100644 zad2/PolynomialOperations.class create mode 100644 zad2/PolynomialOperations.java diff --git a/zad2/Main.class b/zad2/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..26f5cc38b13f30c9221b2f81c63a43cb9546fa59 GIT binary patch literal 2853 zcma)8X;%|h7=DH-)^_^r2r+0}A$veNe%^0G`Bg&-Az?~TW)+gKWY{uN68v3;WjNa;c*0EwBzSx5 zgf5|`&#?4?Y;sgj4{M_)Il(@AOfwH?X+xazOs_L(WN4$WUo)&uYA3l%s1L{cj%w3d ztEpKNtwT=QuqHZ%btPd;8yUl}vdx)Z2^I0e zVK>buv!-LD1l&ahPu=OXHpA$w;|U3>opn-KCx?haLyk5!)vu-8aAdT|XqRz}7a14x zu$|Yg50|2C5}rL2ln@xQv*|It*ASj7MJ`%I_$nH)Mn+mi29AW@!L*$@dGlf@WhbrIFC0~yoI+V)b6lthgs25 z{kk)0Co(mDyi<#JYkYXG78g{!j}K&gsNy4hT#HY5>#G$(T*M_6m+`5L&s2PlFNj!V zg9hi3?HxU;k1>-)tU@V^Su+_&PcnEGTS8r~a)#YH$Tk$E*R>>Db%|?sAzOl+5<({3 z)|D$cTX^w2oS5b46Cyx&&NRC3>_JWITrU*923L+}N*sHX;1_h!t=Nx^?x@JGlb4 z1hexNmUAT{ekSA!L(E>cXw4CCWES<_#4MHtm(QYM4lAxR4*Y>t6}XQ31a*G}m(>z} zqqAJ~s|glZjpb~bDEIj;XvQ{jL`olXrMKe>_W+%JhoA608t^R7^|Yf=W|WCbxYQ&J G9Q_ZDd)pfT literal 0 HcmV?d00001 diff --git a/zad2/Main.java b/zad2/Main.java new file mode 100644 index 0000000..96e4cb4 --- /dev/null +++ b/zad2/Main.java @@ -0,0 +1,34 @@ +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.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(); + } +} diff --git a/zad2/PolynomialOperations.class b/zad2/PolynomialOperations.class new file mode 100644 index 0000000000000000000000000000000000000000..85ee99535514c5d1f3b134f4e8de1a6cdb17d704 GIT binary patch literal 3314 zcma)8&r=)s75}VO+6A#HpjgBpWRL?6NFd_Y#x*#uZDPkt0L6jexTGnIw4lW+t*Rfy zr0q;+;#+4rJ+_BrS}wWd;GRO$jCyXTxBdw|^wQ4s>`Cf=-tGp8xK8Ua-yd(^_r1^O zz4v`z{rTUYwE>sXF+aiW1|Omqv*xkQ9Orx zGJIc#O&Q*i;RiCjE5i?UZ0Rr+X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;$9*PB8Xoyf^w15nf6EM$+mf&l}Fk3uQ8a(G7$0A?CZ13A3yn5 zrbGO!7xGo6x8OC$K~gx4H5TbQVZB8t8^^^XSN#S_Nb#iEYkXp9_8Om9x~tbjphlJW zKvW*^plU9&=0iLhNsiR6bZ)Kxid^g}lZa=Bnhe8DB2Z|CKL(yQQ+~=!`h*$j(p_c< j{8es4%-I9ZQ5xLG`*@FMR=AgTu}Yl+vs(yOZ=vuXV3BfA literal 0 HcmV?d00001 diff --git a/zad2/PolynomialOperations.java b/zad2/PolynomialOperations.java new file mode 100644 index 0000000..5dd3ea2 --- /dev/null +++ b/zad2/PolynomialOperations.java @@ -0,0 +1,88 @@ +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) { + throw new IllegalArgumentException("Numerator and denominator vectors must have the same size"); + } + 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 = divide(a, b, mod); + a = b; + b = c; + } + return a; + } +} \ No newline at end of file