number-theory-2
v0.8.0
Published
A modern JavaScript library for number theory and modular arithmetic — including primes, totients, modular inverses, and more.
Maintainers
Readme
number-theory-2
A lightweight and modern number theory library for JavaScript and Node.js.
It includes common number theory utilities all written with clarity and BigInt support.
Installation
npm install number-theory-2Features
- Works with both Number and BigInt
- Pure JavaScript (no dependencies)
- Simple, modular, and easy to extend
- Accurate for very large integer computations
Usage
import { gcd, mod, fibonacci } from "number-theory-2";
console.log(gcd(84, 18)); // ➜ 6n
console.log(mod(-10, 3)); // ➜ 2n
console.log(fibonacci(10)); // ➜ [0n, 1n, 1n, 2n, 3n, 5n, 8n, 13n, 21n, 34n, 55n]Currently Available Functions
Arithmetic
ceilDiv(a, b)– Integer ceiling divisionceil(a / b)with correct signed behaviorcrt(remainders, moduli)– Chinese Remainder Theorem solution for coprime modulidivides(a, b)– Checks ifadividesb(throws ifa = 0)divisors(n)– Returns all positive divisors of a given integer nextendedGCD(a, b)– Extended Greatest Common DivisorfloorDiv(a, b)– Euclidean division returning{ q, r }with0 ≤ r < |b|gcd(a, b)– Greatest Common Divisor using Euclid’s algorithmgcdArray(arr)– Computes the GCD of an array of numbersisEven(n)– Checks if a number n is evenisOdd(n)– Checks if a number n is oddleastAbsoluteResidue(a, m)– Returns minimal absolute residue in(-m/2, m/2](tie atm/2resolves to+m/2)lcm(a, b)– Least Common Multiple using the formulalcm(a, b) = |a*b| / gcd(a, b)lcmArray(arr)– Computes the LCM of an array of numbersmod(a, b)– Modular remainder (handles negative numbers and BigInts)modAdd(a, b, m)– Modular addition(a + b) mod mwith normalizationmodDiv(a, b, m)– Modular divisiona * b⁻¹ (mod m); requiresgcd(b, m) = 1modMul(a, b, m)– Modular multiplication(a * b) mod mwith normalizationmodSub(a, b, m)– Modular subtraction(a - b) mod mwith normalizationmodInverse(a, m)– Modular multiplicative inverse of a under modulo morderMod(a, m)– Multiplicative order: smallestksuch thata^k ≡ 1 (mod m); defined whengcd(a, m) = 1andm > 1powMod(base, exp, mod)– Fast modular exponentiation(base^exp) mod modsign(n)– Returns the sign of a number nsolveCongruence(a, b, m)– Solvesa·x ≡ b (mod m)canonically
Combinatorics
bell(n)– Computes the n-th Bell numbercatalan(n)– Computes the n-th Catalan numbercombination(n, r)– Computes the number of combinations of n items taken r at a timedoubleFactorial(n)– Computes the double factorial n!!factorial(n)– Computes the factorial of n (n!)fibonacci(n)– Generates Fibonacci sequence up to the n-th termhexagonal(n)– Computes the n-th hexagonal numberisHexagonal(n)– Checks if a number n is hexagonalisPentagonal(n)– Checks if a number n is pentagonalisPolygonal(s, n)– Checks if a number n is s-gonalisSquare(n)– Checks if a number n is squareisTriangular(n)– Checks if a number n is triangularlucas(n)– Generates Lucas sequence up to the n-th termmotzkin(n)– Generates Motzkin sequence up to the n-th termnthFibonacci(n)– Returns the n-th Fibonacci numbernthLucas(n)– Returns the n-th Lucas numbernthMotzkin(n)– Returns the n-th Motzkin numbernthPadovan(n)– Returns the n-th Padovan numbernthPerrin(n)– Returns the n-th Perrin numbernthTribonacci(n)– Returns the n-th Tribonacci numberpadovan(n)– Generates Padovan sequence up to the n-th termpascalRow(n)– Returns the n-th row of Pascal's trianglepentagonal(n)– Computes the n-th pentagonal numberperrin(n)– Generates Perrin sequence up to the n-th termpermutation(n, r)– Computes the number of permutations of n items taken r at a timepolygonal(s, n)– Computes the n-th s-gonal numbersquare(n)– Computes the n-th square numbersubfactorial(n)– Computes derangements !n (subfactorial)triangular(n)– Computes the n-th triangular numbertribonacci(n)– Generates Tribonacci sequence up to the n-th term
Function
aliquotSum(n)– Computes the Aliquot Sum of a number ncarmichael(n)– Computes the Carmichael function λ(n)collatzSequence(n)– Generates the Collatz sequence from n down to 1collatzSteps(n)– Counts steps to reach 1 under the Collatz processcototient(n)– Computes cototient n − φ(n)dedekindPsi(n)– Computes the Dedekind psi function ψ(n)digitalRoot(n)– Computes the digital root of an integer nisAbundant(n)– Checks if a number n is AbundantisDeficient(n)– Checks if a number n is DeficientisPerfect(n)– Checks if a number n is PerfectisSquareFree(n)– Checks if a number n is square-freejordanTotient(n, k)– Computes Jordan's totient function J_k(n)liouville(n)– Computes the Liouville function of a number nmobius(n)– Computes the Möbius function μ(n)omegaBig(n)– Computes the number of prime factors of n (big omega)omegaSmall(n)– Computes the number of distinct prime factors of n (small omega)productOfDigits(n)– Computes the product of the digits of an integer nradical(n)– Computes the radical of n (squarefree kernel)reducedTotient(n)– Returns reduced fraction φ(n)/n as { num, den }sigma(n)– Computes the sum of all positive divisors of nsigmaUnitary(n)– Computes the sum of unitary divisors σ*(n)sumOfDigits(n)– Computes the sum of the digits of an integer ntau(n)– Computes the number of positive divisors of ntauUnitary(n)– Computes the number of unitary divisors τ*(n)totient(n)– Computes Euler's Totient Function φ(n)
Prime
countPrimes(n)– Counts the number of prime numbers ≤ ncountPrimesInRange(a, b)– Counts the number of prime numbers between a and b (inclusive)isCoprime(a, b)– Checks whether two integers are coprime (i.e., gcd(a, b) = 1)isPrime(n)– Checks if a number is primenextPrime(n)– Finds the smallest prime number greater than nnthPrime(k)– Returns the k-th prime (BigInt), k ≥ 1prevPrime(n)– Returns the largest prime ≤ n (null if n < 2)primeFactorization(n)– Returns the full prime factorization of n as an array of objects{ prime, power }.primeFactors(n)– Returns the distinct prime factors of n as an array of BigInts.primeGapAt(n)– Returns{ p, next, gap }around n wherep≤ n andnext> psieve(n)– Generates all primes up to n using the Sieve of EratosthenessieveRange(a, b)– Generates all primes between a and b (inclusive) using the Sieve of EratosthenessumOfPrimes(n)– Returns the BigInt sum of all primes ≤ ntwinPrimesInRange(a, b)– Returns all twin prime pairs (p, p+2) in [a, b]
Running Tests
npm testLicense
MIT License © 2025
