diff --git a/02-rozwiazanie.js b/02-rozwiazanie.js new file mode 100644 index 0000000..cb8660c --- /dev/null +++ b/02-rozwiazanie.js @@ -0,0 +1,19 @@ +let Polynomial = require('./polynomial.js'); +let mul, div, gcd; +console.log(process.argv[2]); + +let n = parseInt(process.argv[2]); +let f = new Polynomial.Class([1, 1, 1, 0, 1]); +let g = new Polynomial.Class([0, 1, 1]); +mul = Polynomial.multiply(f, g, 2).coefficients; +try { + div = Polynomial.divide(f, g, 2).coefficients +} catch (e) { + console.log(e) +} +try { + gcd = Polynomial.gcd(f, g, 2).coefficients; +} catch (e) { + console.log(e); +} +console.log([mul, div, gcd]) \ No newline at end of file diff --git a/polynomial.js b/polynomial.js new file mode 100644 index 0000000..9ae1461 --- /dev/null +++ b/polynomial.js @@ -0,0 +1,81 @@ +class Polynomial { + constructor(coefArray) { + this.degree = coefArray.length - 1; + this.coefficients = coefArray; + } +} + +exports.Class = Polynomial; + +function multiply(p1, p2, n) { + + let f = p1.coefficients; + let g = p2.coefficients; + result = new Array(f.length + g.length - 1).fill(0); + + let tmp = []; + for (let i = 0; i < f.length; i++) { + for (let j = 0; j < g.length; j++) { + result[i + j] += f[i] * g[j]; + } + } + return new Polynomial(result.map(x => (x % n) + (x < 0 ? n : 0))); +} + +function divide(p1, p2, n) { + + let inverse = (x) => { + for (let i = 1; i < 2; i++) { + let r = (i * x) % 2; + if (r == 1) + return i + else + throw "divisionError" + + } + } + if (p1.degree < p2.degree) + return p1; + let f = p1.coefficients; + let g = p2.coefficients; + let g_lead_coef = g[g.length - 1]; + let g_deg = p2.degree; + + while (f.length >= g.length) { + + let f_lead_coef = f[f.length - 1]; + let tmp_coef = f_lead_coef * inverse(g_lead_coef); + let tmp_exp = f.length - 1 - g_deg; + let tmp = []; + for (let i = 0; i < tmp_exp; i++) { + tmp.push(0); + } + tmp.push(tmp_coef); + tmp_poly = new Polynomial(tmp); + let sub = multiply(p2, tmp_poly, n); + let tmp_f = []; + for (let i = 0; i < f.length; i++) { + for (let j = 0; j < sub.coefficients.length; j++) { + if (i == j) + tmp_f.push(f[i] - sub.coefficients[j]); + } + } + + f = tmp_f.map(x => (x % n) + (x < 0 ? n : 0)); + while (f && f[f.length - 1] === 0) + f.pop(); + + } + return new Polynomial(f); +} + +function gcd(p1, p2, n) { + if (p2.coefficients.length === 0) { + return p1; + } + return gcd(p2, divide(p1, p2, n), n); +} + +exports.multiply = multiply; +exports.divide = divide; +exports.gcd = gcd; \ No newline at end of file