Skip to content

kenany/primality

Folders and files

NameName
Last commit message
Last commit date

Latest commit

8f76e5f · Aug 11, 2013
Jul 28, 2013
Aug 11, 2013
May 19, 2013
Aug 11, 2013
Jul 28, 2013
Aug 11, 2013
May 16, 2013
Apr 7, 2013
Jan 11, 2013
May 30, 2013
Jun 28, 2013
Apr 2, 2013
May 30, 2013
Jul 28, 2013
Jan 10, 2013
Aug 11, 2013
Aug 11, 2013
Aug 11, 2013
Aug 11, 2013
Aug 11, 2013
May 30, 2013

Repository files navigation

primality.js

Build Status Dependency Status

Primality is a JavaScript library for prime numbers. It features a fantastic primality test and identification of the various classes of prime numbers.

Download

Features

  • Check the primality of a number
  • Works with numbers disguised as strings
  • Also does arrays
  • Provides checks for the following classes of prime numbers:
  • About 1 kibibyte minified and gzipped
  • CommonJS and AMD loader support

Support

Primality has been tested in:

  • Chrome 6–26
  • Firefox 15–20
  • Internet Explorer 10
  • Opera 11.6-12.15
  • Safari 5.0.5–5.1
  • Node.js 0.6.21–0.10.12

Installation

In browsers:

  • Standalone
<script src="primality.js"></script>
  • Component
$ component install KenanY/primality
  • Bower
$ bower install primality

Using npm:

$ npm install primality

In Node.js and RingoJS v0.8.0+:

var primality = require('primality');

In RingoJS v0.7.0-:

var primality = require('primality').primality;

In Rhino:

load('primality.js');

In an AMD loader like RequireJS:

require({
  'paths': {
    'primality': 'path/to/primality'
  }
},
['primality'], function(primality) {
  console.log(primality.VERSION);
});

Usage

Primality's flagship method is primality(), which works as you might expect it to:

primality(7);
// => true

primality(6);
// => false

Of course, you can pass strings instead of numbers if you'd like:

primality('13');
// => true

Primality can even do arrays. If any of the values of an array are not prime, false is returned.

primality([17, 19, 23]);
// => true

primality([17, 20, 23]);
// => false

Eventually, primality tests get boring. In order to mitigate this boredom, Primality can also tell you if a pair of numbers are twin or sexy primes.

Twin primes are prime numbers that differ from another by two. Similarly, cousin primes differ by four and sexy primes differ by six.

primality.areTwinPrimes(3, 5);
// => true

primality.areCousinPrimes(3, 7);
// => true

primality.areSexyPrimes(5, 11);
// => true

You can also check for Wilson primes. Only three Wilson primes are known at the moment: 5, 13, and 563.

primality.isWilsonPrime(563);
// => true

Release Notes

1.5.7

  • isWilsonPrime now works with unknown Wilson primes

The full changelog is available here.