A number theory toolkit for JavaScript.
Determines all of the divisors for a given number.
var divisors = require('number-theory').divisors;
divisors(6); // Returns [1, 2, 3, 6]
Counts the positive integers less than a given number that are co-prime with the given number. For more information see the Wikipedia entry for Euler's Totient Function.
var phi = require('number-theory').eulerPhi;
phi(26); // Returns 12
Determines the prime factorization for a given integer. For more information see Wikipedia's Integer Factorization entry.
var factor = require('number-theory').factor;
/*
Returns: [
{ prime: 2, power: 2 },
{ prime: 3, power: 1 },
{ prime: 11, power: 1 }
]
*/
factor(132);
Uses the Pollard-Rho integer factorization algorithm to quickly find a small divisor of the given number. Note: the divisor found need not be prime (as Pollar-Rho is a general integer factorization algorithm).
var findDivisor = require('number-theory').findDivisor;
findDivisor(152); // Returns 8
Finds the greatest common divisor of two integers a and b.
var gcd = require('number-theory').gcd;
gcd(84, 172); // Returns 4
Given a mixed-radix number and the bases for each digit, this determines the increment of the number. For more information, see Wikipedia's entry on Mixed Radix number systems.
var incMixed = require('number-theory').incMixed;
// A number representing a mixed-radix "clock" at 11:59 PM
var number = [59, 59, 23];
// The bases for each of the mixed radix digits (60 seconds to a minute,
// 60 minutes to an hour, 24 hours to a day).
var base = [60, 60, 24];
incMixed(number, base); // Returns [0, 0, 0] (or midnight the next day)
Given an integer this function computes the modular multiplicative inverse to the given modulo.
var inverseMod = require('number-theory').inverseMod;
inverseMod(14, 17); // Returns 11
Given an integer, returns a Boolean indicating whether it's an abundant number.
var isAbundant = require('number-theory').isAbundant;
isAbundant(36); // Returns true
isAbundant(35); // Returns false
Given an integer, returns a Boolean indicating whether it's a deficient number.
var isDeficient = require('number-theory').isDeficient;
isDeficient(15); // Returns true
isDeficient(12); // Returns false
Given an integer, returns a Boolean indicating whether it's a heptagonal number.
var isHeptagonal = require('number-theory').isHeptagonal;
isHeptagonal(112); // Returns true
isHeptagonal(175); // Returns false
Given an integer, returns a Boolean indicating whether it's a hexagonal number.
var isHexagonal = require('number-theory').isHexagonal;
isHexagonal(190); // Returns true
isHexagonal(50); // Returns false
Given an integer, returns a Boolean indicating whether it's an octagonal number.
var isOctagonal = require('number-theory').isOctagonal;
isOctagonal(65); // Returns true
isOctaongal(50); // Returns false
Given an integer, returns a Boolean indicating whether it's a pentagonal number.
var isPentagonal = require('number-theory').isPentagonal;
isPentagonal(92); // Returns true
isPentagona(50); // Returns false
Given an integer, returns a Boolean indicating whether it's a perfect number.
var isPerfect = require('number-theory').isPerfect;
isPerfect(496); // Returns true
isPerfect(200); // Returns false
Determines if the given number is prime.
Note: this is a particularly slow method that uses full prime factorization to
determine if the number is prime. For a faster method see the miller
function
below.
var isPrime = require('number-theory').isPrime;
isPrime(7); // Returns true
isPrime(48); // Returns false
Given an integer, returns a Boolean indicating whether it's a square number.
var isSquare = require('number-theory').isSquare;
isSquare(16); // Returns true
isSquare(55); // Returns false
Given an integer, returns a Boolean indicating whether it's a triangular number.
var isTriangular = require('number-theory').isTriangular;
isTriangular(21); // Returns true
isTriangular(25); // Returns false
Computes the Jacobi Symbol for the given numbers.
var jacobiSymbol = require('number-theory').jacobiSymbol;
jacobiSymbol(928, 33); // returns 1
Finds the least common multiple of two integers a and b.
var lcm = require('number-theory').lcm;
lcm(4, 3); // Returns 12
Solves a discrete logarithm. For more information see the following:
Compute the value of the Möbius function for n using naive factorization. The Möbius function is defined as 1 if n is a square-free integer with an even number of prime factors, -1 if square-free with an odd number of prime factors, and 0 if n has a squared prime factor.
var mobius = require('number-theory').mobius;
mobius(30); // Returns -1
Compute the value of the Möbius function for integers from n1 to n2 - 1 (inclusive) using a sieve method. Compared to mobius
, this method still effectively factors each integer but is somewhat more efficient than factoring and computing each value individually. Numbers less than min(n1, sqrt(n2)) cannot be sieved implicitly during computation, so an explicit primality test must be performed. By default, the deterministic Miller-Rabin primality test is used, but any boolean primality test may optionally be provided.
var mobiusRange = require('number-theory').mobiusRange;
mobiusRange(1000000, 1000005); // Returns [0, 1, -1, -1, 0]
Uses the determinisic Miller-Rabin Primality Test to determine if the given number is prime. Works for all positive integers less than 341,550,071,728,321.
var miller = require('number-theory').miller;
miller(17); // Returns true
miller(284); // Returns false
Multiplies the two given numbers mod the given modulus. See Wikipedia's entry on Modular Arithmetic.
var multiplyMod = require('number-theory').multiplyMod;
multiplyMod(928, 284, 18); // Returns 14
Computes the power of a base mod the given modulus. For more information see Wikipedia's entry on Modular Exponentiation.
var powerMod = require('number-theory').powerMod;
powerMod(567283, 2843, 776); // Returns 299
Computes a list of all prime factors for the given integer. Note: while this
method fully computes the prime factorization of the integer, it only returns
the primes and not the powers of the factorization. For full prime factorization
please use factor
.
var primeFactors = require('number-theory').primeFactors;
primeFactors(18); // Returns [2, 3]
Computes the smallest primitive root for Z mod n, meaning a multiplicative generator for the group of units of Z mod n. For more information see Wikipedia's entry on Primitive roots modulo n.
var primitiveRoot = require('number-theory').primitiveRoot;
primitiveRoot(1043); // Returns 7
Computes a quadratic nonresidue for the given number. For more information see Wikipedia's entry for Quadratic Residues.
var quadraticNonresidue = require('number-theory').quadraticNonresidue;
quadraticNonresidue(777); // Returns 5
Find a random primitive root for Z mod n, meaning a multiplicative generator for the group of units of Z mod n. Unlike primitiveRoot, this function returns a random primitive root. For more information see Wikipedia's entry on Primitive roots modulo n.
Determines a list of prime numbers up to the given bound by performing the Sieve of Eratosthenes.
var sieve = require('number-theory').sieve;
sieve(10); // Returns [ 2, 3, 5, 7 ]
Determines all square roots of a given number modulo the given modulus. For more information see Wikipedia's entry on Quadratic Residues.
var squareRootMod = require('number-theory').squareRootMod;
squareRootMod(1023, 77); // Returns [76, 1]
Uses the Tonelli–Shanks algorithm to determine a single square root in Z mod p.
var squareRootModPrime = require('number-theory').squareRootModPrime;
squareRootModPrime(100, 19) // Returns 9
Pull requests are very welcome! If you see a function we're missing, have an alternate algorithm implementation, or even want to add a special case function we'd be delighted to review your code.
Try to stick to the following guidelines, as they will help get the PR merged and published quickly:
- New functions should be added to their own file under the
lib/
directory - Make sure to add an entry in the
module.exports
for new functions in theindex.js
file. - Use two space characters per tab
- Please document your function using jsdoc
(see any function in
lib/
for an example on how to do this). - Write a test for your function and place it in the
tests/
folder with the same name that you gave for itslib/
counterpart. - Add an entry to the documentation in this file (
README.md
). Also please try to keep the function list alphabetized for quick reference.
Thanks!
MIT