QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Modular Inverse

Computes the modular multiplicative inverse of a number a under modulo m, which is the integer x such that (a*x) mod m=1.

  • math
  • number-theory
  • algebra

Install

npx shadcn@latest add https://quicksnip.dev/r/modular-inverse.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

function modInverse(a, m) {
    function extendedGCD(a, b) {
        if (b === 0) {
            return { gcd: a, x: 1, y: 0 };
        }
        const { gcd, x: x1, y: y1 } = extendedGCD(b, a % b);
        const x = y1;
        const y = x1 - Math.floor(a / b) * y1;
        return { gcd, x, y };
    }

    const { gcd, x } = extendedGCD(a, m);

    if (gcd !== 1) {
        return null;
    }

    return (x % m + m) % m;
}

Usage

modInverse(3, 26);  // Returns: 9
modInverse(10, 17); // Returns: 12
modInverse(6, 9);   // Returns: null