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

@marlonwq/primality

v1.0.0

Published

A high-performance TypeScript library for primality testing using Miller-Rabin and Fermat algorithms.

Readme

Primality

A high-performance TypeScript library for primality testing using Miller-Rabin and Fermat algorithms. Designed with BigInt support for cryptographic-grade calculations.

NPM Version NPM Downloads GitHub Repo stars License

Features

  • Miller-Rabin Test: Robust probabilistic test.
  • Fermat Primality Test: Fast probabilistic test based on Fermat's Little Theorem.
  • BigInt Support: Test extremely large numbers without precision loss.
  • Zero Dependencies: Lightweight and optimized for performance.

Installation

pnpm add @marlonwq/primality

Usage

import { isPrimeMillerRabin, isPrimeFermat } from '@marlonwq/primality';

// Using Miller-Rabin (Recommended for high accuracy)
// Use the 'n' suffix for BigInt literals
const num1 = 104729n;
console.log(isPrimeMillerRabin(num1)); // true

// Using Fermat (Faster, but watch out for Carmichael numbers)
const num2 = 561n; 
console.log(isPrimeFermat(num2)); // true (Fermat's false positive)
console.log(isPrimeMillerRabin(num2)); // false (Miller-Rabin's correct answer)

How it Works

The Miller-Rabin test is an evolution of Fermat's Little Theorem. Fermat states that if $p$ is prime, then for any integer $a$, $a^{p-1} \equiv 1 \pmod p$. However, some composite numbers (known as Carmichael numbers) also satisfy this condition, leading to false positives.

The Miller-Rabin Secret

The core of Miller-Rabin is the fact that in a prime field $\mathbb{Z}_p$, the equation $x^2 \equiv 1 \pmod p$ has only two solutions: $x = 1$ and $x = p - 1$ (or $-1$).

The Algorithm Steps:

  1. Write $n - 1$ as $2^s \cdot d$ by repeatedly factoring out powers of 2.
  2. Pick a random base $a$ in the range $[2, n - 2]$.
  3. Compute $x = a^d \pmod n$. If $x = 1$ or $x = n - 1$, the number is a probable prime.
  4. Otherwise, square $x$ repeatedly ($x = x^2 \pmod n$) up to $s - 1$ times.
  5. If at any point $x = n - 1$, the number is a probable prime.
  6. If the loop finishes without ever hitting $n - 1$, the number is definitely composite.

Accuracy

Miller-Rabin is a probabilistic algorithm. Each iteration ($k$) reduces the probability of a composite number being declared prime to less than $1/4$. With $k=40$, the error probability is less than $4^{-40}$ — a value so infinitesimal that it is statistically more likely for a meteor to strike your computer than for the test to provide a wrong answer.

Contributing

We use pnpm as our package manager. To get started:

pnpm install
pnpm build
pnpm test

If you find this project helpful, please consider contributing in the following ways: Submitting a pull request, opening an issue, giving the project a star or buying me a coffee!!

License

MIT