({{, (RefLink[Mod, paclet:ref/Mod])[m,n]
gives the remainder on division of m by n. }, {, (RefLink[Mod, paclet:ref/Mod])[m,n,d]
uses an offset d. }})

OpenersNotesSection(Spacer1)Details

Integer mathematical function, suitable for both symbolic and numerical manipulation.

For integers m and n, (RefLink[Mod, paclet:ref/Mod])[m,n] lies between 0 and n-1.

(RefLink[Mod, paclet:ref/Mod])[m,n,1] gives a result in the range 1 to n, suitable for use in functions such as (RefLink[Part, paclet:ref/Part]). »

(RefLink[Mod, paclet:ref/Mod])[m,n,d] gives a result x such that d≤x<d+n and xmodn=mmodn.

The sign of (RefLink[Mod, paclet:ref/Mod])[m,n] is always the same as the sign of n, at least so long as m and n are both real.

(RefLink[Mod, paclet:ref/Mod])[m,n] is equivalent to m-n (RefLink[Quotient, paclet:ref/Quotient])[m,n].

(RefLink[Mod, paclet:ref/Mod])[m,n,d] is equivalent to m-n (RefLink[Quotient, paclet:ref/Quotient])[m,n,d].

The arguments of (RefLink[Mod, paclet:ref/Mod]) can be any numeric quantities, not necessarily integers. »

For positive x, (RefLink[Mod, paclet:ref/Mod])[x,1] gives the fractional part of x.

For exact numeric quantities, (RefLink[Mod, paclet:ref/Mod]) internally uses numerical approximations to establish its result. This process can be affected by the setting of the global variable (RefLink[$MaxExtraPrecision, paclet:ref/$MaxExtraPrecision]).

(RefLink[Mod, paclet:ref/Mod]) works with complex numbers, using its definition in terms of (RefLink[Quotient, paclet:ref/Quotient]).

(RefLink[Mod, paclet:ref/Mod]) automatically threads over lists.

OpenersPrimaryExamplesSection(Spacer1)Examples  (27)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

Remainders mod 3:

Mod[{1,2,3,4,5,6,7},3]

Remainders mod 3 offset to start with 1:

Mod[{1,2,3,4,5,6,7},3,1]

Plot the sequence with fixed modulus:

DiscretePlot[Mod[n,8],{n,50}]

Plot the sequence, varying the modulus:

DiscretePlot[Mod[100,m],{m,50}]

OpenersExampleSection1(Spacer1)Scope  (8)

OpenersExampleSubsection0(Spacer1)Evaluation  (4)

Reduce an approximate number:

Mod[4.56,2]

Reduce an exact numeric quantity:

Mod[Exp[Pi],2]

N[%]

Reduce mod π:

Mod[20,Pi]

Complex number:

Mod[Sqrt[-113],2]

OpenersExampleSubsection1(Spacer1)Manipulation  (4)

Simplify[Mod[2n+1,2],n∈Integers]

Simplify[Mod[(1+x)^16,2],x∈Integers]

Wilson's theorem:

FunctionExpand[Mod[(p-1)!,p],p∈Primes]

Solve a modular equation:

Reduce[Mod[2x+1,5]==Mod[x,7]&&0<x<50,x]

Evaluate an integral:

Integrate[Mod[x^2,2],{x,0,4}]

OpenersExampleSection2(Spacer1)Applications  (4)

Extract parts cyclically:

{a,b,c}[[Mod[Range[10],3,1]]]

Select primes below 100 having the form of 6 k+1:

Select[Array[Prime,PrimePi[100]],Mod[#,6]==1&]

Simulate a particle bouncing in a non-commensurate box:

Graphics[Line[Table[Mod[t,{(Sqrt[2]),1}],{t,0,20,.001}]]]

Define a notation for addition mod 2:

x_⊕y_:=Mod[x+y,2]

(1⊕0)⊕1

OpenersExampleSection3(Spacer1)Properties & Relations  (6)

The results have the same sign as the second argument:

Mod[{-3,-2,-1,0,1,2,3},3]

Mod[{-3,-2,-1,0,1,2,3},-3]

Plot[Mod[x,2],{x,-6,6}]

(RefLink[Mod, paclet:ref/Mod]) can be represented as a (RefLink[DifferenceRoot, paclet:ref/DifferenceRoot]):

DifferenceRootReduce[Mod[k,3],k]

The generating function for (RefLink[Mod, paclet:ref/Mod]):

GeneratingFunction[Mod[n,4],n,x]

Series[%,{x,0,10}]

(RefLink[FindSequenceFunction, paclet:ref/FindSequenceFunction]) can recognize the (RefLink[Mod, paclet:ref/Mod]) sequence:

Table[Mod[n,5],{n,10}]

FindSequenceFunction[%,n]

The exponential generating function for (RefLink[Mod, paclet:ref/Mod]):

ExponentialGeneratingFunction[Mod[n,4],n,x]

OpenersExampleSection4(Spacer1)Possible Issues  (2)

Expressions generated by (RefLink[Mod, paclet:ref/Mod]) can be difficult to evaluate with machine precision:

Mod[GoldenRatio^100,1]

Machine-precision numerical evaluation gives the wrong answer:

N[%]

Arbitrary precision gives the correct answer:

N[%%,30]

Some computations may require higher internal precision than the default:

Mod[GoldenRatio^1000,1]

Block[{$MaxExtraPrecision=Infinity},Mod[GoldenRatio^1000,1]]

OpenersExampleSection5(Spacer1)Neat Examples  (3)

ArrayPlot[Table[Mod[i,j],{i,30},{j,30}]]

Binomial coefficients mod 2:

ArrayPlot[Table[Mod[Binomial[i,j],2],{i,0,63},{j,0,63}]]

Mod 4 additive cellular automaton:

ArrayPlot[CellularAutomaton[{Mod[Total[#],4]&,{},1},{{1},0},50]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[PowerMod, paclet:ref/PowerMod])  ▪  (RefLink[Quotient, paclet:ref/Quotient])  ▪  (RefLink[QuotientRemainder, paclet:ref/QuotientRemainder])  ▪  (RefLink[Divisible, paclet:ref/Divisible])  ▪  (RefLink[CoprimeQ, paclet:ref/CoprimeQ])  ▪  (RefLink[ModularInverse, paclet:ref/ModularInverse])  ▪  (RefLink[FractionalPart, paclet:ref/FractionalPart])  ▪  (RefLink[MantissaExponent, paclet:ref/MantissaExponent])  ▪  (RefLink[PolynomialMod, paclet:ref/PolynomialMod])  ▪  (RefLink[PolynomialRemainder, paclet:ref/PolynomialRemainder])  ▪  (RefLink[Xor, paclet:ref/Xor])  ▪  (RefLink[Modulus, paclet:ref/Modulus])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Some Mathematical Functions, paclet:tutorial/SomeMathematicalFunctions])}, {▪(RefLinkPlain[Integer and Number Theoretic Functions, paclet:tutorial/IntegerAndNumberTheoreticalFunctions])}, {▪(WebLink[An Elementary Introduction to the Wolfram Language: More about Numbers, https://www.wolfram.com/language/elementary-introduction/23-more-about-numbers.html])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Mathematical Functions, paclet:guide/MathematicalFunctions])}, {▪(RefLinkPlain[Differential Equations with Events, paclet:guide/DifferentialEquationsWithEvents])}, {▪(RefLinkPlain[Integer Functions, paclet:guide/IntegerFunctions])}, {▪(RefLinkPlain[Number Theory, paclet:guide/NumberTheory])}, {▪(RefLinkPlain[Number Theoretic Functions, paclet:guide/NumberTheoreticFunctions])}, {▪(RefLinkPlain[Representation of Numbers, paclet:guide/RepresentationOfNumbers])}, {▪(RefLinkPlain[Numerical Functions, paclet:guide/NumericalFunctions])}, {▪(RefLinkPlain[Cryptographic Number Theory, paclet:guide/CryptographicNumberTheory])}, {▪(RefLinkPlain[Conditionals, paclet:guide/Conditionals])}, {▪(RefLinkPlain[Additive Number Theory, paclet:guide/AdditiveNumberTheory])}, {▪(RefLinkPlain[Multiplicative Number Theory, paclet:guide/MultiplicativeNumberTheory])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-MathWorld.png(Spacer1)MathWorld, http://mathworld.wolfram.com/Congruence.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-Functions.png(Spacer1)The Wolfram Functions Site, http://functions.wolfram.com/IntegerFunctions/Mod/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=Mod]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})