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>- Main Functions
- isPrime
- nthPrime
- indexOfPrime
- nthPrimesSum
- nthPrimesTimes
- nextPrime
- prevPrime
- primeSmallerThan
- primeBiggerThan
- primeDivisors
- primeDivisorsSum
- primeDivisorsTimes
- isPrimeOrDivisors
- primesSmallerThan
- closestPrime
- randomPrime
- 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); // 101let result = pr.primeDivisors(42); // [2,3,7]let result = pr.primeDivisorsSum(42); // 2 + 3 + 7 = 12let result = pr.primeDivisorsTimes(42); // 2 * 3 * 7 = 42Checks if a prime is a Mersenne Prime
let result = pr.isMersennePrime(127); // trueGet nth Mersenne Prime
let result = pr.nthMersennePrime(5); // 8191Get 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); // 31let 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. 1 is the 11
pr.nthEmirp(2); // 13
pr.nthEmirp(5); // 37check if the prime has a twin
pr.hasTwinPrime(3); // 5
pr.hasTwinPrime(5); // [5, 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(23); //falseReturns number's Truncatable values
pr.truncatableValues(3797);
/*
{
leftToRight: [ 3, 37, 379, 3797 ],
rightToLeft: [ 7, 97, 797, 3797 ]
}
*/Finds the nth Truncatable Prime
pr.nthTruncatablePrime(10); // 3797Checks if the given number is Pandigital Prime
pr.isPandigitalPrime(2143); // true
