PowerMod
✖
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 allclose allBasic Examples (3)Summary of the most common use cases
Scope (7)Survey of the scope of standard use cases
Numerical Evaluation (4)

https://wolfram.com/xid/0j42dluq-yvd759


https://wolfram.com/xid/0j42dluq-jkq


https://wolfram.com/xid/0j42dluq-5wzqg8


https://wolfram.com/xid/0j42dluq-l81a2x


https://wolfram.com/xid/0j42dluq-k3q

PowerMod threads over lists:

https://wolfram.com/xid/0j42dluq-ubw

TraditionalForm Formatting:

https://wolfram.com/xid/0j42dluq-hildxd

Symbolic Manipulation (3)
Use Reduce to simplify expressions:

https://wolfram.com/xid/0j42dluq-npu0o8

Use FindInstance to find solutions to expressions containing PowerMod

https://wolfram.com/xid/0j42dluq-dg7pul

Use PowerMod in a sum:

https://wolfram.com/xid/0j42dluq-nd928r

Applications (6)Sample problems that can be solved with this function
Basic Applications (2)
Find a PrimitiveRoot of 9:

https://wolfram.com/xid/0j42dluq-iv0

Use PowerMod to generate all coprime integers modulo 9:

https://wolfram.com/xid/0j42dluq-ijkmdx


https://wolfram.com/xid/0j42dluq-73ghej
Find all base 2 pseudoprimes below 1000:

https://wolfram.com/xid/0j42dluq-ddotbl

Find all base 5 pseudoprimes below 1000:

https://wolfram.com/xid/0j42dluq-v2utbo

Number Theory (4)
Build an RSA-like toy encryption scheme. Start with the modulus:

https://wolfram.com/xid/0j42dluq-b23pum

Find the universal exponent of the multiplication group modulo n:

https://wolfram.com/xid/0j42dluq-ndudo


https://wolfram.com/xid/0j42dluq-dqq7e


https://wolfram.com/xid/0j42dluq-dphnlq


https://wolfram.com/xid/0j42dluq-esagl5


https://wolfram.com/xid/0j42dluq-dgtw2

Build an RSA-like toy encryption scheme:

https://wolfram.com/xid/0j42dluq-wi9r7
Perform a cycling attack. One of the outputs will be the plaintext:

https://wolfram.com/xid/0j42dluq-eqzmrb

Alice and Bob publicly agree on a prime number and a primitive root modulo that prime
:

https://wolfram.com/xid/0j42dluq-jcibqc

https://wolfram.com/xid/0j42dluq-k0fs8w
Then Alice and Bob each choose private keys:

https://wolfram.com/xid/0j42dluq-mgkfzh
Then Alice sends Bob mod
while Bob sends Alice
mod
:

https://wolfram.com/xid/0j42dluq-m69s8l

https://wolfram.com/xid/0j42dluq-n3z4dz
Bob then computes mod
and Alice computes
mod
:

https://wolfram.com/xid/0j42dluq-h8qnsc


https://wolfram.com/xid/0j42dluq-stj76g

This is their secret number which they now both share:

https://wolfram.com/xid/0j42dluq-up29yo

Create a random number generator that uses the current time as a seed:

https://wolfram.com/xid/0j42dluq-qx2pgi


https://wolfram.com/xid/0j42dluq-kvu9y8
Compute random numbers between
and
:

https://wolfram.com/xid/0j42dluq-8c5v1l

Properties & Relations (8)Properties of the function, and connections to other functions
PowerMod is a periodic function:

https://wolfram.com/xid/0j42dluq-ud8amv


https://wolfram.com/xid/0j42dluq-d8q8jd

Determine ModularInverse:

https://wolfram.com/xid/0j42dluq-o5llde


https://wolfram.com/xid/0j42dluq-i1dp6y


https://wolfram.com/xid/0j42dluq-tssupn


https://wolfram.com/xid/0j42dluq-lqnk95


https://wolfram.com/xid/0j42dluq-n4wlj


https://wolfram.com/xid/0j42dluq-bm3vdo

Fermat's little theorem states that when is prime, then
:

https://wolfram.com/xid/0j42dluq-wuz10w

The results have the same sign as the modulus:

https://wolfram.com/xid/0j42dluq-mfn


https://wolfram.com/xid/0j42dluq-di3

Possible Issues (1)Common pitfalls and unexpected behavior
Neat Examples (3)Surprising or curious use cases
Plot a list of powers of 3 where the exponent is varied, modulo some prime number:

https://wolfram.com/xid/0j42dluq-g430z5

Plot values of varying powers of numbers with a fixed modulus:

https://wolfram.com/xid/0j42dluq-h1vfz5

Plot an Ulam spiral where numbers are colored based on PowerMod:

https://wolfram.com/xid/0j42dluq-gsvx6n

Wolfram Research (1988), PowerMod, Wolfram Language function, https://reference.wolfram.com/language/ref/PowerMod.html.
Text
Wolfram Research (1988), PowerMod, Wolfram Language function, https://reference.wolfram.com/language/ref/PowerMod.html.
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.
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
Wolfram Language. (1988). PowerMod. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PowerMod.html
BibTeX
@misc{reference.wolfram_2025_powermod, author="Wolfram Research", title="{PowerMod}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/PowerMod.html}", note=[Accessed: 15-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_powermod, organization={Wolfram Research}, title={PowerMod}, year={1988}, url={https://reference.wolfram.com/language/ref/PowerMod.html}, note=[Accessed: 15-April-2025
]}