WOLFRAM

Mod[m,n]

gives the remainder on division of m by n.

Mod[m,n,d]

uses an offset d.

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 all

Basic Examples  (4)Summary of the most common use cases

Compute 5 mod 3:

Out[1]=1

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

Out[1]=1

Plot the sequence with fixed modulus:

Out[1]=1

Plot the sequence, varying the modulus:

Out[1]=1

Scope  (13)Survey of the scope of standard use cases

Numerical Evaluation  (6)

Compute using integers:

Out[2]=2

Compute using an offset:

Out[2]=2

Mod works on integers:

Out[1]=1

Rational numbers:

Out[2]=2

Real numbers:

Out[3]=3

Complex numbers:

Out[4]=4

Exact numbers:

Out[1]=1

Inexact number:

Out[2]=2

Compute using large integers:

Out[1]=1

Mod threads over lists:

Out[1]=1

TraditionalForm formatting:

Symbolic Manipulation  (7)

Solve a modular equation:

Out[2]=2

Use Mod in a sum:

Out[1]=1

Product:

Out[2]=2

Simplify an expression:

Out[1]=1

Identify Mod sequences:

Out[1]=1

Recurrence equation:

Out[1]=1

DirichletTransform:

Out[1]=1

Generating function:

Out[1]=1
Out[2]=2

Applications  (19)Sample problems that can be solved with this function

Basic Applications  (3)

The first 20 values of Mod:

Out[22]=22

Plot the sequence with a fixed modulus:

Out[1]=1

Plot the sequence, varying the modulus:

Out[2]=2

Generating function of Mod[n,8]:

Out[1]=1
Out[2]=2

Exponential generation function:

Out[4]=4

Dirichlet series:

Out[5]=5
Out[6]=6

Numeric Identifiers  (1)

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

An ISBN is valid if , where each is the digit of the ISBN:

Check if each of the ISBNs are valid:

Out[14]=14

Cryptography  (2)

Build an RSA-like encryption scheme. Start with the modulus:

Out[2]=2

Find the universal exponent of the multiplication group modulo n:

Out[3]=3

Private key:

Out[4]=4

Public key:

Out[5]=5

Encrypt a message:

Out[6]=6

Decrypt it:

Out[7]=7

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

Encrypt a message with a key:

Out[2]=2

Decrypt the message:

Out[3]=3

Number Theory  (6)

Check if numbers of the form are prime or composite:

Out[2]=2

Select primes below 100 having the form of :

Out[1]=1

Fermat's little theorem:

Out[1]=1
Out[2]=2

Euler's theorem:

Out[3]=3
Out[4]=4

Wilson's theorem:

Out[1]=1

Define a notation for addition modulo 2:

Out[2]=2

Use Mod to solve systems of linear congruences:

Out[1]=1
Out[2]=2

Computer Sciences  (3)

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

Out[1]=1

Choose a modulus and base:

Compute 1000 random numbers between 0 and 1:

Out[68]=68

Extract parts of a list cyclically:

Out[1]=1

Modular computation of a matrix inverse:

First compute the matrix adjoint:

Then compute the modular inverse of a matrix:

Out[3]=3

Check that the inverse gives the correct result:

Out[4]=4

Politics, Economics and Social Sciences  (2)

Assign memory addresses to social security numbers based on a hashing algorithm:

Assign each social a location, ensuring that there are no collisions:

Compute the result:

Out[24]=24

Compute the hash of a single social security number:

Out[2]=2

Other Applications  (2)

Simulate a particle bouncing in a noncommensurate box:

Out[9]=9

System of 12-tone equal temperament:

Out[2]=2

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

Out[3]=3

Properties & Relations  (7)Properties of the function, and connections to other functions

Mod is a periodic function:

Out[4]=4

Mod is defined over all complex numbers:

Out[5]=5

Range:

Out[1]=1

Mod is transitive. If and , then :

Out[1]=1
Out[2]=2
Out[3]=3

If divides then :

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2

Use PowerMod to compute the modular inverse:

Out[1]=1

Check the result:

Out[2]=2

The results have the same sign as the modulus:

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2

Possible Issues  (1)Common pitfalls and unexpected behavior

Some computations may require higher internal precision than the default:

Out[1]=1

Reset the value of $MaxExtraPrecision:

Out[2]=2

Neat Examples  (4)Surprising or curious use cases

Binomial coefficients modulo 2:

Out[1]=1

Additive cellular automaton:

Out[1]=1

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

Out[2]=2

Modular addition tables:

Out[1]=1
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).

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 ]}

@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 ]}

@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 ]}