Skip to content

NullDev/mathlib

Repository files navigation

mathlib

License

Zero-dependency collection of advanced mathematical functions using BigInts.


✅ What this is

This is my personal zero-dependency collection of various mathematical functions using BigInts,
completely written from scratch.

Functions should be optimized for performance and accuracy, and should be safe for large numbers.
But I can give zero guarentees. If you're using this, you're on your own.
I do write tests for every single function implemented, but I can't guarentee, that the tests are sane either...
All functions are documented - and I tried to comment the code as well as the tests as good as possible.


⛔ What this isn't

This is not a fully fledge mathematical library like mathjs.
It's a collection of functions I re-use occasionally. And it's BigInt only!


📜 What's implemented:

  • abs - Absolute value of a BigInt
  • floorLog2 - Calculate the floor of the base-2 logarithm of a number
  • clamp - Clamp a BigInt to a given range
  • bitLength - Bit length of a BigInt
  • hammingWeight - Population count (number of set bits) of a BigInt
  • prodOdd - Calculate the product of all odd numbers in a given range
  • pow - Power of a BigInt (x^y)
  • nthRoot - nth root of a BigInt (x^(1/n))
  • sqrt - Square root of a BigInt
  • manhattanDist - Manhattan distance between two BigInts
  • euclideanDist - Euclidean distance between two BigInts
  • gcd - Greatest common divisor (2 inputs)
  • gcdMulti - Greatest common divisor of multiple numbers (variadic)
  • gcdExt - Extended Euclidean gcd algorithm for Bézout coefficients
  • lcm - Least common multiple (2 inputs)
  • lcmMulti - Least common multiple of multiple numbers (variadic)
  • mod - True modulo operation for BigInt (not remainder)
  • modPow - Modular exponentiation of a base raised to an exponent modulo a number
  • modInv - Modular inverse of a number using the Extended Euclidean algorithm
  • modDiv - Modular division of two numbers
  • modSqrt - Tonelli-Shanks square root mod an odd prime p
  • modNthRoot - Tonelli-Shanks nth root mod an odd prime p
  • isPrime - Miller-Rabin primality test on 64-bit-size BigInts with deterministic base set
  • eulerCriterion - Euler's criterion for quadratic residues modulo a prime p
  • randomBigInt - Cryptographically-strong (hopefully) random bigint in given range
  • quadraticPoly - Quadratic polynomial f(x) = x² + c modulo n
  • pollardRho - Pollard's ρ algorithm for integer factorization using Brent's cycle detection
  • factor - Prime factorization using Pollard's rho
  • highestSetBit - Position of the highest set bit in a BigInt
  • fibPair - Calculate the nth Fibonacci number modulo m using iterative fast-doubling
  • fib - Calculate the nth Fibonacci number Fₙ for any integer n
  • primePisano - Pisano period π(p) for a prime p
  • pisanoPeriod - Pisano period π(n) for any positive integer n ≥ 1
  • totient - Euler's totient function φ(n)
  • jacobi - Calculate the Jacobi symbol (a | n) for any positive integer n ≥ 1
  • mobius - Möbius function μ(n) for positive integer n ≥ 1
  • factorial - Calculate the exact factorial n! of a number for bigint n ≥ 0
  • crt - Chinese Remainder Theorem solver for a system of congruences

... more to come


📋 To-Do

  • Test coverage overview / reporting
  • Build step & minification for browser
  • Better test setup (still figuring out bun:test)
  • Add more functions (only with tests)
  • NPM package maybe
  • Maybe split up mathlib into smaller related chunks

:octocat: Contributing

Don't really expect anyone to contribute but...

  • Bun is needed to run the tests
  • TypeScript only
  • Adhere to style guide (eslint)
  • Every function should have a test
  • Every function should be documented

About

🧮 Zero-dependency collection of mathematical functions using BigInts

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published