npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

numbers_gng

v1.0.1

Published

A number theory utility library for validating and generating special number types.

Readme

Specialized Number Theory Utilities

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 $O(N)$ or $O(N\sqrt{N})$ loops, this library leverages advanced number theory principles—such as Hensel's Lemma, the Lucas-Lehmer test, Extended Euclidean Algorithm, and Smallest Prime Factor (SPF) Sieves—to achieve optimal, sub-linear execution times.

Installation

npm install numbers_gng

Usage

This package supports both Named Exports (highly recommended for tree-shaking to keep your bundle small) and Namespace Objects.

Option 1: Namespace Objects (Grouped)

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']

Option 2: Direct Named Exports (Optimal for Tree-Shaking)

import { isNarcissistic, getNarcissistic, isHappy } from 'numbers_gng';

console.log(isHappy(19)); // true
console.log(getNarcissistic(4)); // Returns first 4 Narcissistic numbers

API & Algorithms Reference

Every 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 first count numbers of that type. Returns an Array<string> containing the values.

Here is a brief breakdown of what to expect and the underlying algorithms used for optimal performance:

1. Abundant Numbers

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.

2. Deficient Numbers

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.

3. Automorphic Numbers

Definition: A number whose square ends in the same digits as the number itself (e.g., $25^2 = 625$).

Algorithm Used: Bypasses string processing completely during generation. It utilizes Modular Lifting (Hensel's Lemma) to solve the congruence $3n^2 - 2n^3 \pmod{10^k}$, jumping directly from one valid digit length to the next in $\mathcal{O}(\log N)$ time.

4. Happy Numbers

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 $\mathcal{O}(1)$ auxiliary space. Generation uses a hardcoded low-bound threshold cache up to 2000, as all larger numbers mathematically collapse into this range within a few steps.

5. Harshad Numbers

Definition: An integer that is divisible by the sum of its digits (e.g., $18 / (1+8) = 2$).

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.

6. Kaprekar Numbers

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., $45^2 = 2025 \rightarrow 20 + 25 = 45$).

Algorithm Used: Rather than squaring every integer sequentially, it mathematically computes factors of $10^k - 1$ using prime factorization and applies the Extended Euclidean Algorithm to find modular inverse pairs.

7. Narcissistic Numbers (Armstrong Numbers)

Definition: A number that is the sum of its own digits each raised to the power of the number of digits (e.g., $153 = 1^3 + 5^3 + 3^3$).

Algorithm Used: Implements D.G. Radcliffe's algorithm of Combinations with Repetitions. Instead of testing integers sequentially up to $10^{39}$, it tests combinations of digit frequencies, decreasing search space sizes from billions to fractions of a second.

8. Palindromic Numbers

Definition: Numbers that read the same backwards and forwards (e.g., $12321$).

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 $\mathcal{O}(1)$ time complexity per generated palindrome.

9. Perfect Numbers

Definition: A positive integer that is equal to the sum of its positive proper divisors (e.g., $6 = 1 + 2 + 3$).

Algorithm Used: Strictly implements the Euclid-Euler Theorem along with the Lucas-Lehmer Test for Mersenne Primes ($M_p = 2^p - 1$). It evaluates whether a Mersenne number is prime and translates it directly into an even perfect number instantly.

10. Smith Numbers

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 $\mathcal{O}(\sqrt{N})$ trial division loops into blazing fast $\mathcal{O}(\log N)$ operations.

License

MIT