Skip to content
Apurva Kumar edited this page Feb 19, 2025 · 2 revisions

πŸ† Competitive Math Toolkit πŸš€

A high-performance math library optimized for competitive programming.

npm
license
stars


πŸ“Œ Features

βœ”οΈ Number Theory: GCD, LCM, Modular Exponentiation, Modular Inverse, Prime Sieve, Chinese Remainder Theorem
βœ”οΈ Combinatorics: Factorial, nCr (Combinations), nPr (Permutations), Catalan Numbers
βœ”οΈ Matrix Exponentiation: Fast Fibonacci, Solving Recurrence Relations
βœ”οΈ Graph Math: Eulerian Path Detection
βœ”οΈ Optimized for O(log n) and O(1) operations wherever possible


πŸ“¦ Installation

To install the package, use npm:

npm install competitive-math-toolkit

or yarn:

yarn add competitive-math-toolkit

πŸ”° Usage

Number Theory

const mathToolkit = require("competitive-math-toolkit");

console.log(mathToolkit.gcd(36, 60)); // Output: 12
console.log(mathToolkit.lcm(12, 15)); // Output: 60
console.log(mathToolkit.modExp(2, 10, 1000000007)); // Output: 1024
console.log(mathToolkit.isPrime(13)); // Output: true
console.log(mathToolkit.primeFactorization(60)); // Output: [2, 2, 3, 5]
console.log(mathToolkit.sieve(50)); // Output: [2, 3, 5, 7, 11, ...]
console.log(mathToolkit.chineseRemainderTheorem([3, 5, 7], [2, 3, 2])); // Output: 23

Combinatorics

console.log(mathToolkit.factorial(5)); // Output: 120
console.log(mathToolkit.nCr(5, 2)); // Output: 10
console.log(mathToolkit.nPr(5, 2)); // Output: 20

Matrix Exponentiation

console.log(mathToolkit.nthFibonacci(10)); // Output: 55

Graph Math

const graph = {
  A: ["B", "C"],
  B: ["A", "D"],
  C: ["A", "D"],
  D: ["B", "C"],
};
console.log(mathToolkit.countEulerianPaths(graph)); // Output: true

Bitwise Operations

console.log(mathToolkit.and(5, 3)); // Output: 1
console.log(mathToolkit.or(5, 3)); // Output: 7
console.log(mathToolkit.xor(5, 3)); // Output: 6
console.log(mathToolkit.binaryToDecimal("1010")); // Output: 10
console.log(mathToolkit.decimalToBinary(10)); // Output: "1010"

πŸ“œ API Reference

Function Description
gcd(a, b) Returns the Greatest Common Divisor (GCD) of two numbers
lcm(a, b) Returns the Least Common Multiple (LCM) of two numbers
modExp(base, exp, mod) Fast exponentiation (base^exp % mod)
modInverse(a, mod) Finds modular inverse using Extended Euclidean Algorithm
sieve(n) Returns all prime numbers up to n using Sieve of Eratosthenes
isDivisible(number, divisor) Checks if number is divisible by divisor. Returns "Yes" or "No".
findDivisors(number) Returns an array of all divisors of number.
primeFactorization(number) Returns an array of prime factors of number.
isPrime(number) Checks if number is prime. Returns true or false.
factorial(n, mod) Returns factorial of n modulo mod
nCr(n, r, mod) Returns nCr (binomial coefficient) modulo mod
nthFibonacci(n, mod) Returns nth Fibonacci number using Matrix Exponentiation
modAdd(a, b, m) Computes ( (a + b) \mod m )
modSubtract(a, b, m) Computes ( (a - b) \mod m )
modMultiply(a, b, m) Computes ( (a \times b) \mod m )
modInverse(a, m) Computes the modular inverse of ( a ) under modulo ( m )
modDivide(a, b, m) Computes ( (a / b) \mod m ) using modular inverse
nthRoot(n, a) Computes the ( n )-th root of ( a )
isPerfectSquare(n) Checks if ( n ) is a perfect square
isPerfectCube(n) Checks if ( n ) is a perfect cube
binaryExponentiation(base, exp, m) Computes ( \text{base}^{\text{exp}} \mod m ) using fast exponentiation
nPr(n, r) Computes permutations ( P(n, r) )
isCoprime(a, b) Checks if two numbers are coprime (i.e., their GCD is 1)
sumOfDivisors(n) Computes the sum of all divisors of ( n )
countPrimes(n) Counts the number of prime numbers up to ( n )
countEulerianPaths(graph) Checks if a given graph has an Eulerian path
chineseRemainderTheorem(num, rem) Solves a system of simultaneous congruences using the Chinese Remainder Theorem
and(a, b) Computes the bitwise AND of two numbers
or(a, b) Computes the bitwise OR of two numbers
xor(a, b) Computes the bitwise XOR of two numbers
not(a) Computes the bitwise NOT (1's complement) of a number
leftShift(a, n) Shifts bits of a to the left by n positions
rightShift(a, n) Shifts bits of a to the right by n positions (signed shift)
countSetBits(num) Counts the number of 1s in the binary representation of a number
isPowerOfTwo(num) Checks if a number is a power of two
setBit(num, pos) Sets the bit at position pos in num to 1
clearBit(num, pos) Clears the bit at position pos in num (sets to 0)
toggleBit(num, pos) Toggles the bit at position pos in num
checkBit(num, pos) Checks if the bit at position pos is 1
decimalToBinary(num) Converts a decimal number to binary (as a string)
binaryToDecimal(str) Converts a binary string to decimal
decimalToHex(num) Converts a decimal number to hexadecimal (as a string)
hexToDecimal(str) Converts a hexadecimal string to decimal
decimalToOctal(num) Converts a decimal number to octal (as a string)
octalToDecimal(str) Converts an octal string to decimal
binaryToHex(str) Converts a binary string to hexadecimal
hexToBinary(str) Converts a hexadecimal string to binary

⚑ Performance

  • Most operations are O(log N) for efficiency.
  • Matrix exponentiation speeds up Fibonacci and recurrence relations.
  • Uses modular arithmetic for safe computation.

πŸ› οΈ Running Tests

You can run unit tests using:

npm test

πŸ“ License

This project is licensed under the MIT License.


🌟 Contributing

Feel free to contribute, report issues, or request new features!

  1. Fork the repository.
  2. Create a new branch: git checkout -b feature-new-feature
  3. Commit changes: git commit -m "Added a new feature"
  4. Push and submit a PR!

πŸš€ Future Enhancements

  • βœ… Add big integer support
  • βœ… Implement fast polynomial calculations
  • βœ… Provide TypeScript support

πŸ‘¨β€πŸ’» Author

Developed by Apurva Kumar πŸš€
GitHub | LinkedIn