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
- Basic prime number generators
- Primes' indexes
- High performance
- Some special prime arrays
- Relations with normal integers
You can play with the functions on prime.elexron.com
npm install prime-functions
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>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)- 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
Return if a number is Prime Number
let result = pr.isPrime(13); // truelet result = pr.isPrime(28); // falseGet nth prime
let result = pr.nthPrime(5); // 11Get index of prime number
let result = pr.indexOfPrime(13); // 5Index starts from 0
let result = pr.nthPrimesSum(3,5,7); // 5 + 11 + 17 = 33let result = pr.nthPrimesTimes(3,5,7); // 5 * 11 * 17 = 935let result = pr.nextPrime(17); // 19let result = pr.prevPrime(17); // 13let result = pr.primeSmallerThan(100); // 97let result = pr.primeBiggerThan(100); // 101The 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 ]let result = pr.primeDivisorsSum(42); // 2 + 3 + 7 = 12The 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 12Checks if a prime is a Mersenne Prime
let result = pr.isMersennePrime(127); // trueGet 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)Get nth Mersenne Prime's exponents
let result = pr.nthMersennePrimeExponents(5); // 13 - That means 2^13If the number is prime it returns true, otherwise it returns prime divisors
let result = pr.primesSmallerThan(25); // [ 2, 3, 5, 7, 11, 13, 17, 19, 23 ]let result = pr.closestPrime(25); // 23let result = pr.randomPrime(25, 48); // 31Returns a random prime with exactly digit digits
let result = pr.randomPrimeDigits(3); // e.g. 863 (a random 3-digit prime)let result = pr.nextNPrimes(25, 5); // [ 29, 31, 37, 41, 43 ]let result = pr.prevNPrimes(25, 5); // [ 23, 19, 17, 13, 11 ]let result = pr.primesBetween(80, 150); // [ 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149 ]let result = pr.firstNPrimes(7); // [ 2, 3, 5, 7, 11, 13, 17 ]helper function
let result = pr.digits(1554); // 4helper function
let result = pr.sum([2,3,4]); // 9helper function
let result = pr.times([2,3,4]); // 24helper function
let result = pr.remainDividedBy(8,3); // 2helper function That should be bottom of the script
pr.printExecutionTime(); // Execution time: 119mshelper function
pr.beautifyInteger(123123123); // 123.123.123helper function
pr.reverseNumber(123456); // 654321helper function
pr.integerToText(1234567890); // bcdefghijahelper function
pr.integerToString(1234567890); // '1234567890'helper function
pr.integerToArray(1234567890); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]helper function
Returns number first n digits
pr.firstNDigits(1234567890, 4); // 1234helper function
Returns number last n digits
pr.lastNDigits(1234567890, 4); // 7890returns if the given number is emirp.
pr.isEmirp(13); // true
pr.isEmirp(31); // true
pr.isEmirp(19); // falsereturns 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); // 37check 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); // falsehelper
pr.factorial(3); // 6
pr.factorial(pr.factorial(3)); // 720The 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); // 7Euler'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) // 4Check 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)Returns number's Truncatable values
pr.truncatableValues(3797);
/*
{
leftToRight: [ 3, 37, 379, 3797 ],
rightToLeft: [ 7, 97, 797, 3797 ]
}
*/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)Checks if the given number is Pandigital Prime
pr.isPandigitalPrime(2143); // true
