Function

nerdamer.modInv

src/index.ts:794
nerdamer.modInv(a: Expression, p: Expression): Expression

Computes the modular multiplicative inverse of

`a`

modulo

`p`

using the extended Euclidean algorithm. That is, finds

`t`

such that

`a * t ≡ 1 (mod p)`

.

throws

MathError If the inverse does not exist (i.e.

`gcd(a, p) ≠ 1`

).

Parameters

NameTypeDescription
aExpressionThe number whose modular inverse is to be calculated.
pExpressionThe modulus (typically prime).

Returns

Expression — The modular inverse of `a` mod `p`.

Examples

modInv(Expression.create(3), Expression.create(7)).text()    // "5"
modInv(Expression.create(10), Expression.create(17)).text()  // "12"