Skip to content

ModularInverse ​

The xx with ax≡1(modm)a x \equiv 1 \pmod m, when aa is a unit mod mm.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageModularInverse!
ModularInverse(a, m)the inverse of aa modulo mm, in [0,m)[0, m)
SageMathenumeratio_inverse_mod(a, m)SymPymod_inverse(a, m)Oscarinvmod(ZZ(a), ZZ(m))Juliainvmod(ZZ(a), ZZ(m))Rustmod_inverse(a, m)
ModularInverse(z, m)over the Gaussian integers
SageMathenumeratio_inverse_mod(z, m)SymPymod_inverse(z, m)Oscarinvmod(ZZ(z), ZZ(m))Juliainvmod(ZZ(z), ZZ(m))Rustmod_inverse(z, m)
Typed signatures
TypePackageApplies
(integer, integer) -> integercompute-enginealways
(number, number) -> numbernumber-theoryreplaces compute-enginealways

Domain: Modular arithmetic

Details
  • Exists exactly when gcd⁡(a,m)\gcd(a, m) is a unit; the call is otherwise left unevaluated.
  • Read off the Bézout coefficient of ExtendedGCD.
  • For Gaussian integers, Wolfram reduces the inverse into [0,m)[0, m) part by part for a positive rational-integer mm, and as Mod does otherwise.

Plot

Examples

24 more kept as data, checked against the oracles
Basic #

3⋅5=15≡13 \cdot 5 = 15 \equiv 1

live ↗
InModularInverse(3,7)\mathrm{ModularInverse}(3, 7)Out55
live ↗
InModularInverse(2,11)\mathrm{ModularInverse}(2, 11)Out66

3⋅2≡1(mod5)3\cdot2\equiv1 \pmod5

live ↗
InModularInverse(3,5)\mathrm{ModularInverse}(3, 5)Out22
Scope #

a negative argument: −3⋅2=−6≡1-3 \cdot 2 = -6 \equiv 1

live ↗
InModularInverse(−3,7)\mathrm{ModularInverse}(-3, 7)Out22

a 21-digit modulus

live ↗
InModularInverse(3,1020+1)\mathrm{ModularInverse}(3, 10^{20}+1)Out33 333 333 333 333 333 33433\,333\,333\,333\,333\,333\,334

Listable: threads over a list

live ↗
InModularInverse([2,3,4],11)\mathrm{ModularInverse}(\bigl\lbrack2, 3, 4\bigr\rbrack, 11)Out[6,4,3]\bigl\lbrack6, 4, 3\bigr\rbrack

(2+i)(6+4i)=8+14i≡1(mod7)(2 + i)(6 + 4i) = 8 + 14i \equiv 1 \pmod 7

live ↗
InModularInverse(2+i,7)\mathrm{ModularInverse}(2+i, 7)Out6+4i6+4i
live ↗
InModularInverse(11−7i,7+4i)\mathrm{ModularInverse}(11-7i, 7+4i)Out−1+2i-1+2i

Negative input is reduced first: −5≡2-5\equiv2, and 2⋅4≡1(mod7)2\cdot4\equiv1 \pmod7

live ↗
InModularInverse(−5,7)\mathrm{ModularInverse}(-5, 7)Out44

Enormous power inverted modulo a prime without forming it

live ↗
InModularInverse(10100 000,NextPrime(1 000))\mathrm{ModularInverse}(10^{100\,000}, \mathrm{NextPrime}(1\,000))Out942942
Properties #

the same as PowerMod with exponent −1-1

live ↗
InModularInverse(3,7)=PowerMod(3,−1,7)\mathrm{ModularInverse}(3, 7)=\mathrm{PowerMod}(3, -1, 7)OutTrue\mathrm{True}

a⋅a−1≡1a \cdot a^{-1} \equiv 1, modulo the Mersenne prime 261−12^{61} - 1

live ↗
In12 345ModularInverse(12 345,261−1) mod (261−1)12\,345\mathrm{ModularInverse}(12\,345, 2^{61}-1)\bmod(2^{61}-1)Out11

Congruent inputs (2≡72\equiv7) have the same inverse

live ↗
In[ModularInverse(2,5),ModularInverse(7,5)]\bigl\lbrack\mathrm{ModularInverse}(2, 5), \mathrm{ModularInverse}(7, 5)\bigr\rbrackOut[3,3]\bigl\lbrack3, 3\bigr\rbrack

Agrees with PowerMod at exponent −1-1

live ↗
In[ModularInverse(3,5),PowerMod(3,−1,5)]\bigl\lbrack\mathrm{ModularInverse}(3, 5), \mathrm{PowerMod}(3, -1, 5)\bigr\rbrackOut[2,2]\bigl\lbrack2, 2\bigr\rbrack

5⋅3=15≡1(mod7)5\cdot3=15\equiv1 \pmod7

live ↗
InModularInverse(5,7)\mathrm{ModularInverse}(5, 7)Out33

A negative modulus gives a result in the negative range

live ↗
InModularInverse(5,−7)\mathrm{ModularInverse}(5, -7)Out−4-4

7⋅7=49≡1(mod12)7\cdot7=49\equiv1 \pmod{12}: self-inverse

live ↗
InModularInverse(7,12)\mathrm{ModularInverse}(7, 12)Out77

Inverting twice returns the original residue

live ↗
InModularInverse(ModularInverse(7,9),9)\mathrm{ModularInverse}(\mathrm{ModularInverse}(7, 9), 9)Out77
Possible issues #

gcd⁡(2,4)=2\gcd(2, 4) = 2: no inverse

live ↗
InModularInverse(2,4)\mathrm{ModularInverse}(2, 4)OutModularInverse(2,4)\mathrm{ModularInverse}(2, 4)

10⋅19=190≡1(mod21)10\cdot19=190\equiv1 \pmod{21}

live ↗
InModularInverse(10,21)\mathrm{ModularInverse}(10, 21)Out1919

Order matters: 21≡1(mod10)21\equiv1 \pmod{10}, so the inverse is 11

live ↗
InModularInverse(21,10)\mathrm{ModularInverse}(21, 10)Out11
About
Examples45
Checked againstWolfram Language 44 agree1 differSageMath 38 agree7 errorSymPy 18 agree4 differ23 errorOscar 15 agreeJulia 19 agree26 errorRust 19 agree2 differ1 error
CrosswalkWolfram LanguageSageMathSymPyOscarJuliaRust

Implementation

mappedoscarpackages/oracle/src/mappings.ts
mappedwolframpackages/oracle/src/mappings.ts

Wolfram's own ModularInverse (not PowerMod[a, -1, m]): PowerMod's negative-exponent path checks invertibility of a mod the NORM of a Gaussian m, not m itself, and wrongly declines some invertible cases ModularInverse gets right — e.g. ModularInverse(11 - 7i, 7 + 4i) = -1 + 2i, where PowerMod[11 - 7I, -1, 7 + 4I] errors because 11 - 7i shares a factor with N(7 + 4i) = 65.

mappedsagepackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts

SymPy's mod_inverse raises where no inverse exists, and for a negative modulus gives a result in (m, 0] as ours does. It reads a non-integer argument (a Gaussian integer) as an ordinary number rather than reducing it in the Gaussian integers.

mappedjuliapackages/oracle/src/mappings.ts
mappedrustpackages/oracle/src/mappings.ts

See also: PowerMod, ExtendedGCD, PowerModList