prime-functions
v1.3.3
Published
Advanced Prime Numbers Functions. All functions that you need. Generate primes and process with prime numbers
Maintainers
Readme
Prime Functions (Useful Prime Numbers Functions)

Primes are of the utmost importance to number theorists because they are the building blocks of whole numbers, and important to the world because their odd mathematical properties make them perfect for our current uses. On that matter we've built a library to create and find prime numbers
Features
- Basic prime number generators
- Primes' indexes
- High performance
- Some special prime arrays
- Relations with normal integers
Playground
You can play with the functions on prime.elexron.com

Installation
Usage
const pr = require('prime-functions');
console.log(pr.isPrime(13)); //trueYou can simply use the prime-functions on the client side:
<script src="https://cdn.jsdelivr.net/npm/prime-functions/index.min.js"></script>
<script>
const pr = primeFunctions;
console.log(pr.isPrime(13)); //true
</script>Working with large numbers
JavaScript's Number can only hold integers exactly up to Number.MAX_SAFE_INTEGER
(9007199254740991, about 9×10¹⁵). Beyond that a plain number literal is silently
rounded by JavaScript itself, before any library sees it:
const n = 13354124587972147317351777779793215477; // no 'n' suffix
console.log(BigInt(n)); // 13354124587972148371836662641938399232 <- a different number!So for anything past 9×10¹⁵, pass a BigInt or a string:
pr.isPrime(13354124587972147317351777779793215477n); // BigInt -> false
pr.isPrime("13354124587972147317351777779793215477"); // string -> false
pr.primeDivisors("13354124587972147317351777779793215477");
// [ 13n, 67n, 6714647n, 12998431n, 50582263n, 3472840952557n ]Number, BigInt and string input are accepted by isPrime, primeDivisors,
primeDivisorsSum, primeDivisorsTimes, isPrimeOrDivisors, phi/totient,
isMersennePrime, isEmirp, hasTwinPrime, closestPrime, randomPrime,
digits, integerToArray, firstNDigits, lastNDigits, reverseNumber,
beautifyInteger, sum and times.
These throw a RangeError if you hand them a Number that has already
lost precision, rather than quietly answering a question about a different
number. The values they return mirror the input type: a Number argument gives
Numbers back, a BigInt or string argument gives BigInts back — so the pieces
compose:
const n = "13354124587972147317351777779793215477";
pr.times(pr.primeDivisors(n)) === BigInt(n); // true (n is squarefree)Functions
- Main Functions
- isPrime
- nthPrime
- indexOfPrime
- nthPrimesSum
- nthPrimesTimes
- nextPrime
- prevPrime
- primeSmallerThan
- primeBiggerThan
- primeDivisors
- primeDivisorsSum
- primeDivisorsTimes
- isPrimeOrDivisors
- primesSmallerThan
- closestPrime
- randomPrime
- randomPrimeDigits
- nextNPrimes
- prevNPrimes
- primesBetween
- firstNPrimes
- isEmirp
- nthEmirp
- hasTwinPrime
- isTruncatable
- truncatableValues
- nthTruncatablePrime
- isPandigitalPrime
- Theoretical Functions
- Helper Functions
isPrime(number)
Return if a number is Prime Number
let result = pr.isPrime(13); // truelet result = pr.isPrime(28); // falsenthPrime(order)
Get nth prime
let result = pr.nthPrime(5); // 11indexOfPrime(primeNumber)
Get index of prime number
let result = pr.indexOfPrime(13); // 5Index starts from 0
nthPrimesSum(...arguments)
let result = pr.nthPrimesSum(3,5,7); // 5 + 11 + 17 = 33nthPrimesTimes(...arguments)
let result = pr.nthPrimesTimes(3,5,7); // 5 * 11 * 17 = 935nextPrime(currentPrime)
let result = pr.nextPrime(17); // 19prevPrime(currentPrime)
let result = pr.prevPrime(17); // 13primeSmallerThan(number)
let result = pr.primeSmallerThan(100); // 97primeBiggerThan(number)
let result = pr.primeBiggerThan(100); // 101primeDivisors(nonPrimeNumber)
The distinct prime divisors, the set ω(n) counts — so repeated factors appear once.
Returns false for a prime, and [] for 0, 1 and -1.
let result = pr.primeDivisors(42); // [2,3,7]
let result = pr.primeDivisors(12); // [2,3] - 12 = 2^2 x 3, not [2,2,3]
let result = pr.primeDivisors(13); // false - 13 is primeLarge values are factored with Pollard's rho, so they resolve in milliseconds:
pr.primeDivisors("13354124587972147317351777779793215477");
// [ 13n, 67n, 6714647n, 12998431n, 50582263n, 3472840952557n ]primeDivisorsSum(nonPrimeNumber)
let result = pr.primeDivisorsSum(42); // 2 + 3 + 7 = 12primeDivisorsTimes(nonPrimeNumber)
The product of the distinct prime divisors — the radical rad(n). It equals n only when n is squarefree.
let result = pr.primeDivisorsTimes(42); // 2 * 3 * 7 = 42
let result = pr.primeDivisorsTimes(12); // 2 * 3 = 6, not 12isMersennePrime(primeNumber)
Checks if a prime is a Mersenne Prime
let result = pr.isMersennePrime(127); // truenthMersennePrime(order)
Get nth Mersenne Prime. Returns a Number while the result fits in one, and a BigInt beyond that.
let result = pr.nthMersennePrime(5); // 8191
let result = pr.nthMersennePrime(9); // 2305843009213693951n (2^61 - 1)nthMersennePrimeExponents(order)
Get nth Mersenne Prime's exponents
let result = pr.nthMersennePrimeExponents(5); // 13 - That means 2^13isPrimeOrDivisors(number)
If the number is prime it returns true, otherwise it returns prime divisors
primesSmallerThan(number)
let result = pr.primesSmallerThan(25); // [ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]closestPrime(number)
let result = pr.closestPrime(25); // 23randomPrime(minVal, maxVal)
let result = pr.randomPrime(25, 48); // 31randomPrimeDigits(digit)
Returns a random prime with exactly digit digits
let result = pr.randomPrimeDigits(3); // e.g. 863 (a random 3-digit prime)nextNPrimes(minVal, n)
let result = pr.nextNPrimes(25, 5); // [ 29, 31, 37, 41, 43 ]prevNPrimes(number)
let result = pr.prevNPrimes(25, 5); // [ 23, 19, 17, 13, 11 ]primesBetween(number1, number2)
let result = pr.primesBetween(80, 150); // [ 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149 ]firstNPrimes(count)
let result = pr.firstNPrimes(7); // [ 2, 3, 5, 7, 11, 13, 17 ]digits(number)
helper function
let result = pr.digits(1554); // 4sum(numbersArray)
helper function
let result = pr.sum([2,3,4]); // 9times(numbersArray)
helper function
let result = pr.times([2,3,4]); // 24remainDividedBy(number, divisor)
helper function
let result = pr.remainDividedBy(8,3); // 2printExecutionTime()
helper function That should be bottom of the script
pr.printExecutionTime(); // Execution time: 119msbeautifyInteger()
helper function
pr.beautifyInteger(123123123); // 123.123.123reverseNumber(number)
helper function
pr.reverseNumber(123456); // 654321integerToText()
helper function
pr.integerToText(1234567890); // bcdefghijaintegerToString(number)
helper function
pr.integerToString(1234567890); // '1234567890'integerToArray(number)
helper function
pr.integerToArray(1234567890); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]firstNDigits(number, n, returnAsInteger=true)
helper function
Returns number first n digits
pr.firstNDigits(1234567890, 4); // 1234lastNDigits(number, n, returnAsInteger=true)
helper function
Returns number last n digits
pr.lastNDigits(1234567890, 4); // 7890isEmirp(number)
returns if the given number is emirp.
pr.isEmirp(13); // true
pr.isEmirp(31); // true
pr.isEmirp(19); // falsenthEmirp(number)
returns nth emirp (an emirp is a prime that becomes a different prime when its digits are reversed; palindromic primes like 11 or 101 don't count). 1 is 13
pr.nthEmirp(1); // 13
pr.nthEmirp(4); // 37hasTwinPrime(number, returnItsTwin=true)
check if the prime has a twin
pr.hasTwinPrime(3); // 5
pr.hasTwinPrime(5); // [3, 7] (5 sits between two twin-prime pairs: 3 and 7)
pr.hasTwinPrime(311); // 313
pr.hasTwinPrime(3, false); // True
pr.hasTwinPrime(37); // falsefactorial(number)
helper
pr.factorial(3); // 6
pr.factorial(pr.factorial(3)); // 720wilsonsTheorem(n, returnWithExplanation=true)
The Wilson's Theorem.
n+1 should be prime number if and only if n! mod(n+1) = n.
returnWithExplanation is the conditions and explanation of Wilson's Theorem.
pr.wilsonsTheorem(6);
/*
{
formula: 'FORMULA: f(n) = ( 6! mod(6+1) / n ) * ( 6+1 ) + 2 --- CONDITIONS: if 6+1 is prime if and only if 6! mod(6+1) = 6 ',
result: 7
}
*/
pr.wilsonsTheorem(6, false); // 7phi(n)
Euler's phi and also known as totient function.
Function can be used as both phi and totient
pr.totient(1) // 1
pr.phi(2) // 1
pr.phi(3) // 2
pr.phi(4) // 2
pr.totient(5) // 4
pr.phi(6) // 2
pr.phi(7) // 6
pr.totient(8) // 4
pr.phi(9) // 6
pr.phi(10) // 4isTruncatable(number)
Check if the given number is Truncatable Prime
pr.isTruncatable(3797); //true
pr.isTruncatable(373); //true
pr.isTruncatable(11); //false (single-digit truncation isn't prime on both sides)truncatableValues(number)
Returns number's Truncatable values
pr.truncatableValues(3797);
/*
{
leftToRight: [ 3, 37, 379, 3797 ],
rightToLeft: [ 7, 97, 797, 3797 ]
}
*/nthTruncatablePrime(n)
Finds the nth Truncatable Prime.
There are exactly 11 two-sided truncatable primes in base 10 — 23, 37, 53, 73,
313, 317, 373, 797, 3137, 3797, 739397 (OEIS A020994) —
so anything past the 11th returns false.
pr.nthTruncatablePrime(10); // 3797
pr.nthTruncatablePrime(11); // 739397 (the largest one)
pr.nthTruncatablePrime(12); // false (no 12th exists)isPanditalPrime(n)
Checks if the given number is Pandigital Prime
pr.isPandigitalPrime(2143); // true