/**
* 小数点以下の桁数
* @param {number} value
*/
const getDecimalLength = (value) => {
let x = (value + '').split('.')[1];
if (typeof x !== 'undefined' && x.length > 0) {
return x.length;
}
return 0;
};
/**
* 誤差のない割り算
* @param {number} x
* @param {number} y
*/
const division = (x, y) => {
const n = 10 ** (getDecimalLength(x) + getDecimalLength(y));
x = +(x + '').replace('.', '');
y = +(y + '').replace('.', '');
return [x / y / n, (x % y) / n];
};
/**
* 素数判定
* @param {number} n
*/
const isPrimeNumber = (n) => {
if (n < 2) return false;
else if (n == 2) return true;
else if (n % 2 == 0) return false;
for (let i = 3; i <= Math.sqrt(n); i++) {
if (division(n, i)[1] == 0) {
return false;
}
}
return true;
};
目次