@hugoalh/is-numeric-prime
Version:
A module to determine whether the numeric is prime.
60 lines (59 loc) • 1.35 kB
JavaScript
import { bigintRootApproximate } from "./_bigint_root_approximate.js";
/**
* Determine whether the numeric is prime.
* @param {bigint | number} item Item that need to determine.
* @returns {boolean} Determine result.
* @example 1
* ```ts
* isNumericPrime(9876);
* //=> false
* ```
* @example 2
* ```ts
* isNumericPrime(8n);
* //=> false
* ```
* @example 3
* ```ts
* isNumericPrime(17);
* //=> true
* ```
* @example 4
* ```ts
* isNumericPrime(23n);
* //=> true
* ```
*/
export function isNumericPrime(item) {
let itemBigInt;
if (typeof item === "bigint") {
itemBigInt = item;
}
else {
if (!Number.isInteger(item)) {
return false;
}
itemBigInt = BigInt(item);
}
if (itemBigInt === 2n ||
itemBigInt === 3n ||
itemBigInt === 5n ||
itemBigInt === 7n) {
return true;
}
if (itemBigInt < 2n ||
itemBigInt % 2n === 0n ||
itemBigInt % 3n === 0n ||
itemBigInt % 5n === 0n ||
itemBigInt % 7n === 0n) {
return false;
}
const divisorMaximum = bigintRootApproximate(itemBigInt).ceil;
for (let divisor = 3n; divisor <= divisorMaximum; divisor += 2n) {
if (itemBigInt % divisor === 0n) {
return false;
}
}
return true;
}
export default isNumericPrime;