An ES Module For Working With Prime Numbers – Primality.js

Category: Javascript | September 7, 2023
Author:kenany
Views Total:4 views
Official Page:Go to website
Last Update:September 7, 2023
License:MIT

Preview:

An ES Module For Working With Prime Numbers – Primality.js

Description:

Primality.js is a lightweight ES module that provides an accurate primality test to determine whether a given number is prime.

It not only identifies if a number is prime but can also unravel whether a pair of numbers are twin, cousin, or sexy primes:

  • Twin primes are pairs of prime numbers that differ by two.
  • Cousin primes are pairs of prime numbers differing by four.
  • Sexy primes (yes, they’re really called that) are pairs of primes that differ by six.

If you’re seeking rarity, the component can also detect Wilson primes, a class of primes so elusive that only three are known as of now: 5, 13, and 563.

How to use it:

1. Install and import the primality module into your JS project.

# NPM
$ npm i primality
import primality from 'primality';

2. The API exposes simple functions you can call to check for these different types of primes:

// false
primality(1);
// false
primality('1');
// true
primality(13);
// true
primality('13');
// true
primality([17, 19, 23]);
import { areCousinPrimes, areSexyPrimes, areTwinPrimes } from 'primality';
// true
areCousinPrimes(3, 7);
// true
areSexyPrimes(5, 11);
// true
areTwinPrimes(3, 5);
import { isWilsonPrime } from 'primality';
// true
isWilsonPrime(13);

Changelog:

v3.0.0 (09/07/2023)

  • Node.js v14 and v16 are no longer supported.

You Might Be Interested In:


Leave a Reply