Mod
✖
Mod
Details

- Mod is also known as modulo operation.
- Mathematical function, suitable for both symbolic and numerical manipulation.
- Typically used in modular arithmetic, cryptography, random number generation and cyclic operations in programs.
- Mod[m,n] gives the remainder of m divided by n.
- Mod[m,n] is equivalent to m-n Quotient[m,n].
- For positive integers m and n, Mod[m,n] is an integer between 0 and n-1.
- Mod[m,n,d] gives a result
such that
and
.

Examples
open allclose allBasic Examples (4)Summary of the most common use cases

https://wolfram.com/xid/02cbm-in5

The remainder on division of 5 by 3 offset to start with 1:

https://wolfram.com/xid/02cbm-ebo

Plot the sequence with fixed modulus:

https://wolfram.com/xid/02cbm-bu1e9z

Plot the sequence, varying the modulus:

https://wolfram.com/xid/02cbm-bjcbqz

Scope (13)Survey of the scope of standard use cases
Numerical Evaluation (6)

https://wolfram.com/xid/02cbm-s15lus


https://wolfram.com/xid/02cbm-wmfosh

Mod works on integers:

https://wolfram.com/xid/02cbm-le07m0


https://wolfram.com/xid/02cbm-ccojl6


https://wolfram.com/xid/02cbm-m89


https://wolfram.com/xid/02cbm-ybp


https://wolfram.com/xid/02cbm-c3p


https://wolfram.com/xid/02cbm-10wfg


https://wolfram.com/xid/02cbm-c5gnrk

Mod threads over lists:

https://wolfram.com/xid/02cbm-8lqbj9

TraditionalForm formatting:

https://wolfram.com/xid/02cbm-6fi7x8

Symbolic Manipulation (7)

https://wolfram.com/xid/02cbm-din

Use Mod in a sum:

https://wolfram.com/xid/02cbm-2lbwm8


https://wolfram.com/xid/02cbm-17994q


https://wolfram.com/xid/02cbm-t86bhn

Identify Mod sequences:

https://wolfram.com/xid/02cbm-5okec


https://wolfram.com/xid/02cbm-ojcks5


https://wolfram.com/xid/02cbm-vjtibo


https://wolfram.com/xid/02cbm-pz93yz


https://wolfram.com/xid/02cbm-gaiyeu

Applications (19)Sample problems that can be solved with this function
Basic Applications (3)
The first 20 values of Mod:

https://wolfram.com/xid/02cbm-xwzbqi

Plot the sequence with a fixed modulus:

https://wolfram.com/xid/02cbm-kbphd2

Plot the sequence, varying the modulus:

https://wolfram.com/xid/02cbm-b4hvu3

Generating function of Mod[n,8]:

https://wolfram.com/xid/02cbm-j0vkps


https://wolfram.com/xid/02cbm-goee5n

Exponential generation function:

https://wolfram.com/xid/02cbm-7gkxaz

https://wolfram.com/xid/02cbm-5i00ln


https://wolfram.com/xid/02cbm-sypupo


https://wolfram.com/xid/02cbm-4tqu70

Numeric Identifiers (1)
Given an International Standard Book Number (ISBN), check whether or not it is valid:

https://wolfram.com/xid/02cbm-0jbdfr
An ISBN is valid if , where each
is the
digit of the ISBN:

https://wolfram.com/xid/02cbm-k38n9m
Check if each of the ISBNs are valid:

https://wolfram.com/xid/02cbm-o77l6o

Cryptography (2)
Build an RSA-like encryption scheme. Start with the modulus:

https://wolfram.com/xid/02cbm-b23pum

Find the universal exponent of the multiplication group modulo n:

https://wolfram.com/xid/02cbm-ndudo


https://wolfram.com/xid/02cbm-dqq7e


https://wolfram.com/xid/02cbm-dphnlq


https://wolfram.com/xid/02cbm-esagl5


https://wolfram.com/xid/02cbm-dgtw2

Use Mod to create a Caesar cipher that shifts letters in the alphabet to encrypt a message:

https://wolfram.com/xid/02cbm-xwc6ln

https://wolfram.com/xid/02cbm-0laq81


https://wolfram.com/xid/02cbm-gm577x

Number Theory (6)
Check if numbers of the form are prime or composite:

https://wolfram.com/xid/02cbm-00nxhi

https://wolfram.com/xid/02cbm-fcz7g7

Select primes below 100 having the form of :

https://wolfram.com/xid/02cbm-ts6


https://wolfram.com/xid/02cbm-dzozc8


https://wolfram.com/xid/02cbm-q60cqg


https://wolfram.com/xid/02cbm-1ac4i0


https://wolfram.com/xid/02cbm-wgxdmq


https://wolfram.com/xid/02cbm-yc8

Define a notation for addition modulo 2:

https://wolfram.com/xid/02cbm-vgn

https://wolfram.com/xid/02cbm-qny

Use Mod to solve systems of linear congruences:

https://wolfram.com/xid/02cbm-if2ogp


https://wolfram.com/xid/02cbm-5hf4fd

Computer Sciences (3)
Create a random number generator that uses the current time as a seed:

https://wolfram.com/xid/02cbm-qx2pgi


https://wolfram.com/xid/02cbm-kvu9y8
Compute 1000 random numbers between 0 and 1:

https://wolfram.com/xid/02cbm-8c5v1l

Extract parts of a list cyclically:

https://wolfram.com/xid/02cbm-x36

Modular computation of a matrix inverse:

https://wolfram.com/xid/02cbm-1v1fjw
First compute the matrix adjoint:

https://wolfram.com/xid/02cbm-04ubeq
Then compute the modular inverse of a matrix:

https://wolfram.com/xid/02cbm-z1iqvr

Check that the inverse gives the correct result:

https://wolfram.com/xid/02cbm-1ck8su

Politics, Economics and Social Sciences (2)
Assign memory addresses to social security numbers based on a hashing algorithm:

https://wolfram.com/xid/02cbm-0yqjhn
Assign each social a location, ensuring that there are no collisions:

https://wolfram.com/xid/02cbm-3cycsr

https://wolfram.com/xid/02cbm-1jqepr

Compute the hash of a single social security number:

https://wolfram.com/xid/02cbm-xgcgio

https://wolfram.com/xid/02cbm-8pxpjb

Other Applications (2)
Simulate a particle bouncing in a noncommensurate box:

https://wolfram.com/xid/02cbm-oll

System of 12-tone equal temperament:

https://wolfram.com/xid/02cbm-b3kd4i

https://wolfram.com/xid/02cbm-lkpt53

Notes that have a difference of 1200 cents are considered to be from the same congruence class:

https://wolfram.com/xid/02cbm-hzcqol

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

https://wolfram.com/xid/02cbm-ud8amv

Mod is defined over all complex numbers:

https://wolfram.com/xid/02cbm-ctu0mc


https://wolfram.com/xid/02cbm-pqywiy

Mod is transitive. If and
, then
:

https://wolfram.com/xid/02cbm-i1dp6y


https://wolfram.com/xid/02cbm-tssupn


https://wolfram.com/xid/02cbm-lqnk95


https://wolfram.com/xid/02cbm-bk0r6w


https://wolfram.com/xid/02cbm-rhv2h3

The QuotientRemainder[a,n] is the same as Mod[a,n]:

https://wolfram.com/xid/02cbm-jllr0w


https://wolfram.com/xid/02cbm-fp5d3g

Use PowerMod to compute the modular inverse:

https://wolfram.com/xid/02cbm-jvgwab


https://wolfram.com/xid/02cbm-d8q8jd

The results have the same sign as the modulus:

https://wolfram.com/xid/02cbm-mfn


https://wolfram.com/xid/02cbm-di3

For a positive real number x, Mod[x,1] gives the fractional part of x:

https://wolfram.com/xid/02cbm-wwuhnq


https://wolfram.com/xid/02cbm-60ycn2

Possible Issues (1)Common pitfalls and unexpected behavior
Some computations may require higher internal precision than the default:

https://wolfram.com/xid/02cbm-xih


Reset the value of $MaxExtraPrecision:

https://wolfram.com/xid/02cbm-itv

Neat Examples (4)Surprising or curious use cases
Binomial coefficients modulo 2:

https://wolfram.com/xid/02cbm-g1u


https://wolfram.com/xid/02cbm-tj7

Plot of an Ulam spiral where numbers are colored based on their congruence modulo 49:

https://wolfram.com/xid/02cbm-zp3c6

https://wolfram.com/xid/02cbm-qhm0kv


https://wolfram.com/xid/02cbm-nmlpvs

Wolfram Research (1988), Mod, Wolfram Language function, https://reference.wolfram.com/language/ref/Mod.html (updated 2002).
Text
Wolfram Research (1988), Mod, Wolfram Language function, https://reference.wolfram.com/language/ref/Mod.html (updated 2002).
Wolfram Research (1988), Mod, Wolfram Language function, https://reference.wolfram.com/language/ref/Mod.html (updated 2002).
CMS
Wolfram Language. 1988. "Mod." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2002. https://reference.wolfram.com/language/ref/Mod.html.
Wolfram Language. 1988. "Mod." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2002. https://reference.wolfram.com/language/ref/Mod.html.
APA
Wolfram Language. (1988). Mod. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Mod.html
Wolfram Language. (1988). Mod. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Mod.html
BibTeX
@misc{reference.wolfram_2025_mod, author="Wolfram Research", title="{Mod}", year="2002", howpublished="\url{https://reference.wolfram.com/language/ref/Mod.html}", note=[Accessed: 29-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_mod, organization={Wolfram Research}, title={Mod}, year={2002}, url={https://reference.wolfram.com/language/ref/Mod.html}, note=[Accessed: 29-March-2025
]}