A high-performance, mathematically optimized JavaScript utility package to validate and generate various special types of numbers (e.g., Perfect, Automorphic, Kaprekar, Narcissistic numbers, etc.).
Unlike naive implementations that rely on brute-force
npm install numbers_gngThis package supports both Named Exports (highly recommended for tree-shaking to keep your bundle small) and Namespace Objects.
import { Abundant, Perfect, Automorphic } from 'numbers_gng';
// Validation (Expects a Number, String, or BigInt -> Returns Boolean)
console.log(Abundant.is(12)); // true
console.log(Perfect.is(28)); // true
// Generation (Expects a Count -> Returns an Array of Strings)
console.log(Automorphic.get(5)); // ['0', '1', '5', '6', '25']import { isNarcissistic, getNarcissistic, isHappy } from 'numbers_gng';
console.log(isHappy(19)); // true
console.log(getNarcissistic(4)); // Returns first 4 Narcissistic numbersEvery number type exports two primary functions:
is[Type](number): Validates if the given input is a valid number of that type. Returns a boolean. Supports large numbers via BigInt.get[Type](count): Generates the firstcountnumbers of that type. Returns anArray<string>containing the values.
Here is a brief breakdown of what to expect and the underlying algorithms used for optimal performance:
Definition: A number whose proper divisors sum up to more than the number itself.
Algorithm Used: Uses a Harmonic Series Sieve (similar to the Sieve of Eratosthenes) to compute divisor sums. It utilizes the mathematically proven density limit (~24.8% for abundant numbers) to pre-calculate the exact array size, resolving the request in a single pass without throwing away allocation memory.
Definition: A number whose proper divisors sum up to less than the number itself.
Algorithm Used: Uses a Harmonic Series Sieve (similar to the Sieve of Eratosthenes) to compute divisor sums. It utilizes the mathematically proven density limit (~75.2% for deficient numbers) to pre-calculate the exact array size, resolving the request in a single pass without throwing away allocation memory.
Definition: A number whose square ends in the same digits as the number itself (e.g.,
Algorithm Used: Bypasses string processing completely during generation. It utilizes Modular Lifting (Hensel's Lemma) to solve the congruence
Definition: A number which eventually reaches 1 when replaced by the sum of the square of its digits repeatedly.
Algorithm Used: Validation implements Floyd's Cycle-Finding Algorithm (Tortoise and Hare) using
Definition: An integer that is divisible by the sum of its digits (e.g.,
Algorithm Used: Generation circumvents expensive string allocations inside tight loops by maintaining a native integer tracking register that computes digit sums incrementally via currentNum % 10 === 0.
Definition: A positive integer with a web-property where if you square it, the split halves of the string representation sum back up to the original number (e.g.,
Algorithm Used: Rather than squaring every integer sequentially, it mathematically computes factors of
Definition: A number that is the sum of its own digits each raised to the power of the number of digits (e.g.,
Algorithm Used: Implements D.G. Radcliffe's algorithm of Combinations with Repetitions. Instead of testing integers sequentially up to
Definition: Numbers that read the same backwards and forwards (e.g.,
Algorithm Used: Uses a direct Root-Half Construction Matrix. It dynamically generates only the left mathematical root of the palindrome and mirrors it onto itself, resulting in an exact
Definition: A positive integer that is equal to the sum of its positive proper divisors (e.g.,
Algorithm Used: Strictly implements the Euclid-Euler Theorem along with the Lucas-Lehmer Test for Mersenne Primes (
Definition: A composite number for which the sum of its digits is equal to the sum of the digits of its prime factors.
Algorithm Used: Employs a pre-computed Smallest Prime Factor (SPF) Sieve via a modified Sieve of Eratosthenes. This upgrades factorization lookups from standard
MIT