strict-prime-identifier
v1.1.1
Published
Strict integer validation and hybrid prime identification using BigInt
Maintainers
Readme
strict-prime-identifier
A hybrid prime number identifier for JavaScript that safely and accurately checks primality for extremely large integers using BigInt, with strict input validation to ensure correctness.
✨ Key Features
Strict Validation Rejects non-integer numbers, invalid strings, or malformed inputs to prevent logical errors.
BigInt-Powered Computing Supports integers beyond Number.MAX_SAFE_INTEGER (up to $2^{53}-1$ and beyond).
Optimized Algorithm Implements the 6k ± 1 optimization, offering roughly 3x faster performance than standard trial division.
Smart Type Return Returns safe integers as Number, and integers exceeding safe limits as a string (e.g., "9007199254740997n") to avoid precision loss.
🚀 Installation npm install strict-prime-identifier
💻 Usage
Node.js """ const isP = require('./isP');
isP('97'); // [97, true] isP(1000003); // [1000003, true] isP(10n); // [10, false] """
Browser """
"""
🔒 Strict Validation Examples
The identifier prioritizes data integrity and rejects ambiguous inputs:
const isP = require('strict-prime-identifier');
console.log(isP(3.14)); // [3.14, false] (Rejected: Not an integer) console.log(isP("123abc")); // ["123abc", false] (Rejected: Invalid characters) console.log(isP(" 101 ")); // [101, true] (Accepted: Trimmed automatically)
📊 How It Works
Validation Stage Checks if the input is a number, string, or bigint and ensures it represents a whole integer.
Conversion Stage Converts all valid inputs to BigInt for high-precision arithmetic.
Primality Test Uses an optimized $O(\sqrt{n})$ approach checking divisibility by 6k ± 1.
Type Restoration Ensures the return type is appropriate:
Safe integers → Number
Large integers → string with n suffix
📝 License
MIT License. Feel free to use and contribute.
