PowerMod

Modular exponentiation SymPy: mod_inverse, nth_root_mod, sqrt_mod, and Pow; WMA link.


PowerMod[x, y, m]

computes x^y modulo m.

PowerMod[x, -1, m]

computes modular inverse of x modulo m.

PowerMod[x, 1/r, m]

computes a module r-th root of x.

Compute 7 squared mod 5:

PowerMod is periodic:

Plot the sequence of PowerMod using varying powers:

PowerMod can handle large integers:

PowerMod works on square roots:

PowerMod works on nth roots other than a square root:

When y is a root $1/r$, there may be more than one solution for PowerMod.
However, the result must satisfy $x^y = r$ mod m:

> x=11; r=3; m=19; Mod[(PowerMod[x, 1/r, m] ^ r), m] == x
= True

Note the inverse relationship PowerMod has when y is negative one:

Also, PowerMod with $-y$, is inverse of PowerMod with y:

The first parameter, x should be invertible for modulus m:

Also, you should not use zero as a modulus for y:

PowerMod threads over lists

ModularInverse
Quotient