prime-functions

1.2.2 • Public • Published

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-functions.truncgil.com

Installation

npm install prime-functions

Usage

const pr = require('prime-functions');
console.log(pr.isPrime(13));  //true

You 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> 

Functions

isPrime(number)

Return if a number is Prime Number

let result = pr.isPrime(13);    // true
let result = pr.isPrime(28);    // false

nthPrime(order)

Get nth prime

let result = pr.nthPrime(5);    // 11

indexOfPrime(primeNumber)

Get index of prime number

let result = pr.indexOfPrime(13);    // 5

Index starts from 0

nthPrimesSum(...arguments)

let result = pr.nthPrimesSum(3,5,7);    // 5 + 11 + 17 = 33

nthPrimesTimes(...arguments)

let result = pr.nthPrimesTimes(3,5,7);    // 5 * 11 * 17 = 935

nextPrime(currentPrime)

let result = pr.nextPrime(17);    // 19

prevPrime(currentPrime)

let result = pr.prevPrime(17);    // 13

primeSmallerThan(number)

let result = pr.primeSmallerThan(100);    // 97

primeBiggerThan(number)

let result = pr.primeBiggerThan(100);    // 101

primeDivisors(nonPrimeNumber)

let result = pr.primeDivisors(42);    // [2,3,7]

primeDivisorsSum(nonPrimeNumber)

let result = pr.primeDivisorsSum(42);    // 2 + 3 + 7 = 12

primeDivisorsTimes(nonPrimeNumber)

let result = pr.primeDivisorsTimes(42);    // 2 * 3 * 7 = 42

isMersennePrime(primeNumber)

Checks if a prime is a Mersenne Prime

let result = pr.isMersennePrime(127);    // true

nthMersennePrime(order)

Get nth Mersenne Prime

let result = pr.nthMersennePrime(5);    // 8191

nthMersennePrimeExponents(order)

Get nth Mersenne Prime's exponents

let result = pr.nthMersennePrimeExponents(5);    // 13  - That means 2^13

isPrimeOrDivisors(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);    // 23

randomPrime(minVal, maxVal)

let result = pr.randomPrime(25, 48);    // 31

whatWillThisPrimeBe(primeNumber)

let result = pr.whatWillThisPrimeBe(23);    // It'll strengthen you

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);    // 4

sum(numbersArray)

helper function

let result = pr.sum([2,3,4]);    // 9

times(numbersArray)

helper function

let result = pr.times([2,3,4]);    // 24

remainDividedBy(number, divisor)

helper function

let result = pr.remainDividedBy(8,3);    // 2

printExecutionTime()

helper function That should be bottom of the script

pr.printExecutionTime();    // Execution time: 119ms

beautifyInteger()

helper function

pr.beautifyInteger(123123123);    // 123.123.123

reverseNumber(number)

helper function

pr.reverseNumber(123456);    // 654321

integerToText()

helper function

pr.integerToText(1234567890);    // bcdefghija

integerToString(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);    // 1234

lastNDigits(number, n, returnAsInteger=true)

helper function

Returns number last n digits

pr.lastNDigits(1234567890, 4);    // 7890

isEmirp(number)

returns if the given number is emirp.

pr.isEmirp(13);    // true
pr.isEmirp(31);    // true
pr.isEmirp(19);    // false

nthEmirp(number)

returns nth emirp. 1 is the 11

pr.nthEmirp(2);    // 13
pr.nthEmirp(5);    // 37

hasTwinPrime(number, returnItsTwin=true)

check 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);    // false

factorial(number)

helper

pr.factorial(3);    // 6
pr.factorial(pr.factorial(3));    // 720

wilsonsTheorem(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);    // 7

phi(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)      // 4

isTruncatable(number)

Check if the given number is Truncatable Prime

pr.isTruncatable(3797); //true
pr.isTruncatable(373);  //true
pr.isTruncatable(23);   //false

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

pr.nthTruncatablePrime(10);   // 3797

isPanditalPrime(n)

Checks if the given number is Pandigital Prime

pr.isPandigitalPrime(2143);   // true

Package Sidebar

Install

npm i prime-functions

Weekly Downloads

5

Version

1.2.2

License

MIT

Unpacked Size

32.9 kB

Total Files

3

Last publish

Collaborators

  • sanduvac
  • umitunc