PowerMod
Details
- PowerMod is also known as modular exponentiation.
- Mathematical function, suitable for both symbolic and numerical manipulation.
- Typically used in modular arithmetic, cryptography, random number generation and cyclic operations in programs.
- PowerMod[a,b,m] gives the remainder of ab divided by m.
- PowerMod[a,b,m] allows negative and rational values of b. It returns unevaluated if the corresponding modular inverse or root does not exist.
- For positive b, PowerMod[a,b,m] gives the same result as Mod[a^b, m] but is much more efficient.
Examples
open all close allBasic Examples (3)
Scope (7)
Numerical Evaluation (4)
PowerMod[3, 2, 7]PowerMod[3, -2, 7]PowerMod[3, 1 / 2, 2]PowerMod[2 + I, 2, 3]PowerMod[10 ^ 300 + 1, 7, 5]PowerMod threads over lists:
PowerMod[2, {10, 11, 12, 13, 14}, 5]TraditionalForm Formatting:
TraditionalForm[PowerMod[a, b, m]]Symbolic Manipulation (3)
Use Reduce to simplify expressions:
Reduce[PowerMod[a, 2, 17] == 2 && 0 < a < 17, a, Integers]Use FindInstance to find solutions to expressions containing PowerMod
FindInstance[PowerMod[a, b, 17] == 1 && 0 < a < 17 && 0 < b < 17, {a, b}, Integers, 2]Use PowerMod in a sum:
Sum[PowerMod[a, 2, 7], {a, 7}]Applications (6)
Basic Applications (2)
Find a PrimitiveRoot of 9:
PrimitiveRoot[9]Use PowerMod to generate all coprime integers modulo 9:
Union[Table[PowerMod[%, b, 9], {b, 10}]]BPseudoPrimeQ[b_, n_] := PowerMod[b, n - 1, n] == 1 && CompositeQ[n]Find all base 2 pseudoprimes below 1000:
Select[Range[1000], BPseudoPrimeQ[2, #]&]Find all base 5 pseudoprimes below 1000:
Select[Range[1000], BPseudoPrimeQ[5, #]&]Number Theory (4)
Build an RSA-like toy encryption scheme. Start with the modulus:
{p, q} = Prime[RandomInteger[{10 ^ 4, 10 ^ 5}, {2}]];
n = p qFind the universal exponent of the multiplication group modulo n:
λ = CarmichaelLambda[n]d = NestWhile[#1 + 1&, Round[n / 3], GCD[λ, #1] =!= 1&]e = PowerMod[d, -1, λ]PowerMod[ToCharacterCode["RSA in Mathematica"], e, n]FromCharacterCode[PowerMod[%, d, n]]Build an RSA-like toy encryption scheme:
{p, q} = {47, 59};
n = p q;
ϕ = EulerPhi[n];
d = NestWhile[#1 + 1&, Round[n / 3], GCD[ϕ, #1] =!= 1&];
e = 17;
PlainText = 1504;
CypherText = PowerMod[PlainText, e, n];Perform a cycling attack. One of the outputs will be the plaintext:
Cy[1] = PowerMod[CypherText, e, n];
Cy[j_] := PowerMod[Cy[j - 1], e, n];
Table[Cy[i], {i, Divisors[MultiplicativeOrder[e, ϕ]]}]Alice and Bob publicly agree on a prime number
and a primitive root modulo that prime
:
p = RandomPrime[10 ^ 50];g = PrimitiveRoot[p];Then Alice and Bob each choose private keys:
{a, b} = RandomInteger[p, {2}];Then Alice sends Bob
mod
while Bob sends Alice
mod
:
A = PowerMod[g, a, p];B = PowerMod[g, b, p];Bob then computes
mod
and Alice computes
mod
:
AlicesKey = PowerMod[A, b, p]BobsKey = PowerMod[B, a, p]This is their secret number which they now both share:
AlicesKey == BobsKeyCreate a random number generator that uses the current time as a seed:
x[0] = UnixTime[]
x[k_] := Mod[PowerMod[a, k, m]x[0], m];m = 2147483647;
a = 16807;Compute
random numbers between
and
:
Histogram[Table[x[i] / m, {i, 0, 1000}], 10]Properties & Relations (8)
PowerMod is a periodic function:
{PowerMod[7, 2, 5], PowerMod[7 + 5, 2, 5]}{PowerMod[2, 3, 5], Mod[2 ^ 3, 5]}Determine ModularInverse:
{PowerMod[2, -1, 5], ModularInverse[2, 5]}Mod[5, 3] == Mod[2, 3]Mod[2, 3] == Mod[8, 3]Mod[8, 3] == Mod[5, 3]{Divisible[8, 2], PowerMod[8, 3, 2] == 0}PowerMod[7, EulerPhi[19], 19] == 1Fermat's little theorem states that when
is prime, then
:
PowerMod[2, 17 - 1, 17]The results have the same sign as the modulus:
PowerMod[{5, -5}, 2, 3]PowerMod[{5, -5}, 2, -3]Possible Issues (1)
Neat Examples (3)
Plot a list of powers of 3 where the exponent is varied, modulo some prime number:
ListPlot[Table[PowerMod[3, b, Prime[44]], {b, 1, Prime[44] - 1}], Filling -> Axis]Plot values of varying powers of numbers with a fixed modulus:
ArrayPlot[Table[PowerMod[a, b, 32], {a, 2, 100}, {b, 2, 100}], ColorFunction -> "Rainbow"]Plot an Ulam spiral where numbers are colored based on PowerMod:
ulamSpiral[n_] := Permute[Range[n ^ 2], Accumulate@Take[Join[{n ^ 2 + 1} / 2, Flatten@Table
[(-1) ^ j i, {j, n}, {i, {-1, n}}, {j}]], n ^ 2]]~Partition~n
ArrayPlot[PowerMod[ulamSpiral[101], 3, 17], ColorFunction -> "Rainbow"]See Also
PowerModList ModularInverse Mod Power ExtendedGCD MultiplicativeOrder EulerPhi PrimitiveRoot PrimitiveRootList CyclicGroup RootOfUnityQ Encrypt Decrypt
Function Repository: PowerTowerMod TetrationMod
Tech Notes
Related Links
History
Introduced in 1988 (1.0)
Text
Wolfram Research (1988), PowerMod, Wolfram Language function, https://reference.wolfram.com/language/ref/PowerMod.html.
CMS
Wolfram Language. 1988. "PowerMod." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/PowerMod.html.
APA
Wolfram Language. (1988). PowerMod. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PowerMod.html
BibTeX
@misc{reference.wolfram_2026_powermod, author="Wolfram Research", title="{PowerMod}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/PowerMod.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_powermod, organization={Wolfram Research}, title={PowerMod}, year={1988}, url={https://reference.wolfram.com/language/ref/PowerMod.html}, note=[Accessed: 12-June-2026]}