prime-number

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

KLP js-standard-style

Table of Contents

Usage

As you might expect, you can do

var isPrime = require('prime-number')

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

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

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

You can benchmark other primality check algorithms.

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

benchmark(isPrime)(from, to)

Here there are the results, using a oneliner, of few primality check packages found on npm, so I can state that

My algorithm sucks! 🐸

# node -e "require('prime-number/benchmark')(require('is-prime'))(100000, 10000000)"
Found 654987 primes
primality benchmark: 2507ms

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

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

Installation

With npm do

$ npm install prime-number

Or copy and paste the code below.

Source

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

The algorithm is really basic:

Export primeNumber function

module.exports = primeNumber

License

MIT