@hugoalh/is-numeric-prime
Version:
A module to determine whether the numeric is prime.
62 lines (61 loc) • 1.56 kB
JavaScript
import { bigintRootApproximate } from "./_bigint_root_approximate.js";
const primesKnown = [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n, 41n, 43n, 47n, 53n, 59n, 61n, 67n, 71n, 73n, 79n, 83n, 89n, 97n];
const primesPush = [];
/**
* 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) {
return false;
}
const primes = [...primesKnown, ...primesPush];
if (primes.includes(itemBigInt)) {
return true;
}
if (primes.some((prime) => {
return (itemBigInt > prime && itemBigInt % prime === 0n);
})) {
return false;
}
const rootCeil = bigintRootApproximate(itemBigInt).ceil;
for (let divisor = primesKnown[primesKnown.length - 1] + 2n; divisor <= rootCeil; divisor += 2n) {
if (itemBigInt % divisor === 0n) {
return false;
}
}
primesPush.push(itemBigInt);
return true;
}
export default isNumericPrime;