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 🙏

© 2024 – Pkg Stats / Ryan Hefner

prime-number

v1.0.2

Published

is a recursive function to check if a number is prime (and a benchmark to test how slow it is :)

Downloads

150

Readme

prime-number

is a recursive function to check if a number is prime (and a benchmark to test slow it is :)

KLP js-standard-style

Table of Contents

Is it 1 a prime ? Some years ago I composed a djembe rhythm based on prime numbers, and it sounds better if 1 is considered prime. Casually, the algorithm implemented here defines 1 as a not prime.

Usage

As you might expect, you can do

const isPrime = require('prime-number')

console.log(isPrime(19)) // true

There is a list of few primes available, if you want to use it

const primeNumberList = require('prime-number/list')
console.log(primeNumberList)

You can benchmark other primality check algorithms.

const isPrime = require('yet-another-primality-check')
const benchmark = require('prime-number/benchmark')
const from = 1000
const to = Number.MAX_SAFE_INTEGER

benchmark(isPrime)(from, to)

Using a oneliner, let's check few primality check packages found on npm.

# node -e "require('prime-number/benchmark')(require('prime-number'))(10000, 100000)"
Found 8363 primes
Primality benchmark: 44.703s

# node -e "require('prime-number/benchmark')(require('is-prime'))(10000, 100000)"
Found 8363 primes
Primality benchmark: 14.885ms

# node -e "require('prime-number/benchmark')(require('check-prime'))(10000, 100000)"
Found 654987 primes
primality benchmark: 61.613ms

So I can state that

My algorithm sucks! 🐸

Installation

With npm do

npm install prime-number

Or copy and paste the code below.

Source

First of all, do not check if num is an integer or positive or less than Number.MAX_SAFE_INTEGER. You can do it with some other function before calling primeNumber.

// Remember if a number is prime.
const memoize = { isPrime: {}, isNotPrime: {} }
memoize.isNotPrime[1] = true
memoize.isPrime[2] = true

/**
 * Check if a number is prime.
 *
 * @param {Number}
 *
 * @returns {Boolean}
 */

function primeNumber (num) {
  // Avoid computing twice.
  if (memoize.isPrime[num]) return true
  if (memoize.isNotPrime[num]) return false

  const knowPrimes = Object.keys(memoize.isPrime)

  for (let i = 0; i < knowPrimes.length; i++) {
    const p = Number(knowPrimes[i])

    if (num === p) return true

    if (num % p === 0) {
      memoize.isNotPrime[num] = true
      return false
    }
  }

  for (
    let i = 3;
    // Do not excede the square root of num.
    i * i <= num;
    // All prime numbers are 1 or 5 modulo 6.
    // Since we start with 3, this will do: 3 -> 5 -> 7 -> 11 ... +2 -> +4 -> +2 -> +4 ...
    i = i % 6 === 1 ? i + 4 : i + 2
  ) {
    if (primeNumber(i)) { // <-- Recursion here!
      if (num % i === 0) {
        memoize.isNotPrime[num] = true
        return false
      }
    }
  }

  memoize.isPrime[num] = true
  return true
}

Export primeNumber function

module.exports = primeNumber

License

MIT