forked from kalmar/DALGLI0
88 lines
2.0 KiB
JavaScript
88 lines
2.0 KiB
JavaScript
|
const rl = require('readline').createInterface({
|
||
|
input: process.stdin,
|
||
|
output: process.stdout
|
||
|
});
|
||
|
|
||
|
//zapytanie o wartosc n
|
||
|
rl.question('n? ', n => {
|
||
|
n = parseInt(n);
|
||
|
//sprawdzenie czy n na pewno jest liczba
|
||
|
if (isNaN(n)) throw Error('Must be Number');
|
||
|
// deklarowanie slownikow dla poszczegolnych elementow(aby się nie powtorzyly)
|
||
|
let elOdw = new Dictionary();
|
||
|
let dzielZero = new Dictionary();
|
||
|
let elNil = new Dictionary();
|
||
|
let elIden = new Dictionary();
|
||
|
//#region dodanie trywalnych
|
||
|
dzielZero.add(0, 0);
|
||
|
elNil.add(0, 0);
|
||
|
elIden.add(0, 0);
|
||
|
//#endregion
|
||
|
for (let y = 0; y < n; y++) {
|
||
|
//#region Obliczanie elementow Idenpotetnych
|
||
|
if (Math.pow(y, 2) % n == y) {
|
||
|
elIden.add(y, y);
|
||
|
}
|
||
|
//#endregion
|
||
|
for (let x = 0; x < n; x++) {
|
||
|
//#region obliczanie elementow Nilpotetnych
|
||
|
if (Math.pow(y, x) % n == 0) {
|
||
|
elNil.add(y, y);
|
||
|
}
|
||
|
//#endregion
|
||
|
//#region Obliczanie elementow odwracalnych'
|
||
|
if (x >= y && (x * y) % n == 1) {
|
||
|
elOdw.add(x, x);
|
||
|
elOdw.add(y, y);
|
||
|
}
|
||
|
//#endregion
|
||
|
//#region obliczanie dzielnikow zera
|
||
|
if (x >= y && x != 0 && y != 0 && (x * y) % n == 0) {
|
||
|
dzielZero.add(x, x);
|
||
|
dzielZero.add(y, y);
|
||
|
}
|
||
|
//#endregion
|
||
|
}
|
||
|
}
|
||
|
//wypisanie obliczonych wartosci wartosci
|
||
|
console.log([
|
||
|
elOdw.getValues(),
|
||
|
dzielZero.getValues(),
|
||
|
elNil.getValues(),
|
||
|
elIden.getValues()
|
||
|
]);
|
||
|
//zamkniecie połaczenia z input/output
|
||
|
rl.close();
|
||
|
});
|
||
|
|
||
|
function NWD(a, b) {
|
||
|
return b ? NWD(b, a % b) : a;
|
||
|
}
|
||
|
// klasa słownika
|
||
|
class Dictionary {
|
||
|
constructor() {
|
||
|
this.elements = {};
|
||
|
}
|
||
|
add(key, value) {
|
||
|
this.elements[key] = value;
|
||
|
}
|
||
|
remove(key, value) {
|
||
|
delete this.elements[key];
|
||
|
}
|
||
|
|
||
|
getKeys() {
|
||
|
let keys = [];
|
||
|
for (let key in this.elements) {
|
||
|
keys.push(key);
|
||
|
}
|
||
|
return keys;
|
||
|
}
|
||
|
getValues() {
|
||
|
let values = [];
|
||
|
for (let key in this.elements) {
|
||
|
values.push(this.elements[key]);
|
||
|
}
|
||
|
return values;
|
||
|
}
|
||
|
}
|