Skip to content

Mod ​

The remainder of a on division by b.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaModuloMathWorldModWikidataQ1799665DLMFfront/introduction.common.t2.r10FungrimMod✓Fungrim entry54093175231e959a25✓Wolfram LanguageMod!
Mod(a, b)remainder of aa on division by bb, with the sign of bb.
SageMathenumeratio_mod(a, b)Oscarmod(a, b)Juliamod(a, b)Rustmod_floor(a, b)
Mod(a, b, d)the x≡a(modb)x \equiv a \pmod b with d≤x<d+bd \le x < d + b.
Mod(z, m)for Gaussian integers, z−m Quotient(z,m)z - m\,\mathrm{Quotient}(z, m): the remainder in the box around 0
SageMathenumeratio_mod(z, m)Oscarmod(z, m)Juliamod(z, m)Rustmod_floor(z, m)
Mod(a, b, d?)The remainder of a on division by b.
Typed signatures
TypePackageApplies
(real, real) -> realanalyticreplaces compute-enginealways
(real, real) -> realcompute-enginealways
(real, real) -> realresiduesreplaces analyticalways
(number, number, number?) -> numbernumber-theoryreplaces residuesalways

Domain: Modular arithmetic

Details
  • Equivalent to a−b Quotient(a,b)a-b\,\mathrm{Quotient}(a,b), i.e. a−b⌊a/b⌋a-b\lfloor a/b\rfloor.
  • When b>0b>0 the result lies in [0,b)[0,b); the sign of the result always matches the sign of bb.
  • Periodic: a mod n=(a+kn) mod na\bmod n=(a+kn)\bmod n for any integer kk.
  • compute-engine returns NaN for a zero modulus rather than leaving the call unevaluated.
  • A third argument dd offsets the range to [d,d+b)[d, d+b), as Wolfram's Mod[a, b, d] does.
  • Sage's Mod(a, m) builds a residue class, which is IntegerMod here (IntegerMod(a, m) keeps its modulus); Mod(a, m) is the remainder, a plain number, as in Wolfram. Sage's a % m is this head.

Examples

30 more kept as data, checked against the oracles
Basic #
live ↗
In17 mod 517\bmod5Out22
live ↗
In100 mod 7100\bmod7Out22

The result takes the sign of the modulus

live ↗
In(−7) mod 3(-7)\bmod3Out22

Threads element-wise over a list

live ↗
In[1,2,3,4,5] mod 3\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack\bmod3Out[1,2,0,1,2]\bigl\lbrack1, 2, 0, 1, 2\bigr\rbrack

The remainder of 5 divided by 3

live ↗
In5 mod 35\bmod3Out22

Mod binds tighter than addition, so this is 1+(4 mod 3)1 + (4 \bmod 3)

live ↗
In1+4 mod 31+4\bmod3Out22

Sage's Mod(17, 5) is a residue class, IntegerMod; Mod(17, 5) here is the remainder 2, a plain integer

live ↗
In(17  mod  5)({17}\;\mathrm{mod}\;5)Out(2  mod  5)(2\;\mathrm{mod}\;5)
Scope #

A third argument offsets the range: the result lies in [d,d+n)[d, d + n), here [1,6)[1, 6)

live ↗
InMod(17,5,1)\mathrm{Mod}(17, 5, 1)Out22

With offset 1 the residues run 1,2,31, 2, 3 rather than 0,1,20, 1, 2

live ↗
InMod(5,3,1)\mathrm{Mod}(5, 3, 1)Out22

Rationals are reduced exactly

live ↗
In52 mod 2\frac{5}{2}\bmod2Out12\frac{1}{2}
live ↗
In3.14 mod 23.14\bmod2Out1.141.14

An exact irrational reduces exactly to 27−32\sqrt7 - 3

live ↗
In28 mod 3\sqrt{28}\bmod3Out27−32\sqrt{7}-3

A symbolic constant reduces exactly to π−2\pi - 2

live ↗
Inπ mod 2π\bmod2Outπ−2π-2

a 10 001-digit dividend

live ↗
In1010 000 mod 10 00710^{10\,000}\bmod10\,007Out6 3336\,333

Threads over a list of moduli

live ↗
In10 mod [3,4,7]10\bmod\bigl\lbrack3, 4, 7\bigr\rbrackOut[1,2,3]\bigl\lbrack1, 2, 3\bigr\rbrack

a Gaussian integer: (5+3i)−2(2+2i)(5 + 3i) - 2(2 + 2i)

live ↗
In(5+3i) mod 2(5+3i)\bmod2Out1−i1-i

1+i1 + i divides 5+3i=(1+i)(4−i)5 + 3i = (1 + i)(4 - i)

live ↗
In(5+3i) mod (1+i)(5+3i)\bmod(1+i)Out00

Gaussian integers: the quotient rounds, so each part lands in (−m/2,m/2](-m/2, m/2]

live ↗
In(7+5i) mod 3(7+5i)\bmod3Out1−i1-i

a Gaussian modulus: 7+5i=(2+i)(4+i)−i7 + 5i = (2 + i)(4 + i) - i

live ↗
In(7+5i) mod (2+i)(7+5i)\bmod(2+i)Out−i-i

exact in both parts past 2532^{53}

live ↗
In(100 000 000 000 000 000 001+9 007 199 254 740 993i) mod (2+i)(100\,000\,000\,000\,000\,000\,001+9\,007\,199\,254\,740\,993i)\bmod(2+i)Out00

Again 1+(16 mod 5)1 + (16 \bmod 5) from precedence

live ↗
In1+16 mod 51+16\bmod5Out22

The generating function of n mod 4n \bmod 4 is rational

live ↗
InGeneratingFunction(n mod 4,n,x)\mathrm{GeneratingFunction}(n\bmod4, n, x)Out3x3+2x2+x1−x4\frac{3x^3+2x^2+x}{1-x^4}
Applications #

282475249=710282475249 = 7^{10}, so Fermat's little theorem gives 1

live ↗
In282 475 249 mod 11282\,475\,249\bmod11Out11

Euler's theorem: 7φ(12)≡1(mod12)7^{\varphi(12)} \equiv 1 \pmod{12}

live ↗
In7Totient(12) mod 127^{\mathrm{Totient}(12)}\bmod12Out11

Cycling an index with Mod repeats the list

live ↗
In[a,b,c][1+((1..10)−1) mod 3]\bigl\lbrack a, b, c\bigr\rbrack[1+((1..10)-1)\bmod3]Out[a,b,c,a,b,c,a,b,c,a]\bigl\lbrack a, b, c, a, b, c, a, b, c, a\bigr\rbrack
Properties #

Periodicity: a mod n=(a+kn) mod na \bmod n = (a+kn) \bmod n for any integer kk

live ↗
In17 mod 5=(17+3×5) mod 517\bmod5=(17+3×5)\bmod5OutTrue\mathrm{True}

A negative modulus gives a result in (n,0](n, 0]

live ↗
In(−5) mod (−3)(-5)\bmod(-3)Out−2-2

the remainder of Quotient

live ↗
In(7+5i) mod (2+i)=(7+5i)−(2+i)Quotient(7+5i,2+i)(7+5i)\bmod(2+i)=(7+5i)-(2+i)\mathrm{Quotient}(7+5i, 2+i)OutTrue\mathrm{True}

Congruent numbers share a remainder

live ↗
In5 mod 3=2 mod 35\bmod3=2\bmod3OutTrue\mathrm{True}

2≡8(mod3)2 \equiv 8 \pmod 3

live ↗
In2 mod 3=8 mod 32\bmod3=8\bmod3OutTrue\mathrm{True}

Anything is 0 modulo 1

live ↗
In0 mod 10\bmod1Out00

Known value · Wolfram MathematicalFunctionData, Mod

Odd numbers leave remainder 1 modulo 2

live ↗
In1 mod 21\bmod2Out11

Known value · Wolfram MathematicalFunctionData, Mod

Remainder 1 at the start of the cycle

live ↗
In1 mod 31\bmod3Out11

Known value · Wolfram MathematicalFunctionData, Mod

The remainders modulo 3 cycle through 0, 1, 2

live ↗
In2 mod 32\bmod3Out22

Known value · Wolfram MathematicalFunctionData, Mod

A multiple of the modulus leaves 0

live ↗
In3 mod 33\bmod3Out00

Known value · Wolfram MathematicalFunctionData, Mod

The cycle repeats after 3

live ↗
In4 mod 34\bmod3Out11

Known value · Wolfram MathematicalFunctionData, Mod

8≡5(mod3)8 \equiv 5 \pmod 3, both leave 2

live ↗
In8 mod 3=5 mod 38\bmod3=5\bmod3OutTrue\mathrm{True}

An even number has remainder 0 modulo 2

live ↗
In8 mod 28\bmod2Out00

A larger remainder, 17=2⋅6+517 = 2\cdot 6 + 5

live ↗
In17 mod 617\bmod6Out55

The remainder is smaller than the modulus 8

live ↗
In12 mod 812\bmod8Out44

Known value · Wolfram MathematicalFunctionData, Mod

Negative operands take the sign of the divisor

live ↗
In(−3) mod (−2)(-3)\bmod(-2)Out−1-1

Known value · Wolfram MathematicalFunctionData, Mod

Rational operands work too: −2710+2⋅235=1910-\frac{27}{10} + 2\cdot\frac{23}{5} = \frac{19}{10}

live ↗
In−2710 mod 235-\frac{27}{10}\bmod\frac{23}{5}Out1910\frac{19}{10}

Known value · Wolfram MathematicalFunctionData, Mod

Zero is zero modulo anything

live ↗
In0 mod 20\bmod2Out00

Known value · Wolfram MathematicalFunctionData, Mod

Every integer is 0 modulo 1

live ↗
In1 mod 11\bmod1Out00

Known value · Wolfram MathematicalFunctionData, Mod

15=2⋅7+115 = 2\cdot 7 + 1

live ↗
In15 mod 715\bmod7Out11

Works for reals: π mod e=π−e\pi \bmod e = \pi - e since π<2e\pi < 2e

live ↗
Inπ mod eπ\bmod eOutπ−eπ-e

Known value · Wolfram MathematicalFunctionData, Mod

live ↗
In2Totient(3) mod 32^{\mathrm{Totient}(3)}\bmod3Out11

Known value · Wolfram MathematicalFunctionData, Mod, Euler's totient theorem

live ↗
In(1+(NthPrime(2)−1)!) mod NthPrime(2)(1+(\mathrm{NthPrime}(2)-1)!)\bmod\mathrm{NthPrime}(2)Out00

Known value · Wolfram MathematicalFunctionData, Mod, Wilson's theorem

live ↗
In(2−JacobiSymbol(2,3)) mod 3(2-\mathrm{JacobiSymbol}(2, 3))\bmod3Out00

Known value · Wolfram MathematicalFunctionData, Mod, Euler's criterion

live ↗
In(1+(NthPrime(3)−1)!) mod NthPrime(3)(1+(\mathrm{NthPrime}(3)-1)!)\bmod\mathrm{NthPrime}(3)Out00

Known value · Wolfram MathematicalFunctionData, Mod, Wilson's theorem

live ↗
In2π mod e2π\bmod eOut2π−2e2π-2e

Known value · Wolfram MathematicalFunctionData, Mod

live ↗
In−π mod 2-π\bmod2Out4−π4-π

Known value · Wolfram MathematicalFunctionData, Mod

Possible issues #

Division by a 0 modulus yields Indeterminate rather than an error

live ↗
In5 mod 05\bmod0OutIndeterminate⁡\operatorname{Indeterminate}

rounding ties to even means 11 and −1-1 both appear as remainders mod 2

live ↗
In[(1+2i) mod 2,(3+2i) mod 2]\bigl\lbrack(1+2i)\bmod2, (3+2i)\bmod2\bigr\rbrackOut[1,−1]\bigl\lbrack1, -1\bigr\rbrack

An infinite dividend has no remainder

live ↗
In∞ mod 3∞\bmod3OutNaN⁡\operatorname{NaN}

An infinite modulus gives NaN: compute-engine takes no remainder against an infinity

live ↗
In5 mod ∞5\bmod∞OutNaN⁡\operatorname{NaN}

ComplexInfinity as the dividend leaves the call unevaluated

live ↗
In∞~ mod 3\tilde∞\bmod3Out∞~ mod 3\tilde∞\bmod3
About
Examples87
Checked againstWolfram Language 83 agree3 differSageMath 21 agree3 errorSymPy 48 agree17 differ12 unevaluated5 errormathlib4 4 agree1 unevaluated1 errorOscar 9 agree10 errorJulia 10 agree8 errorRust 7 agree9 error
CrosswalkWikipediaMathWorldWikidataDLMFFungrimFungrim entry ×3Wolfram LanguageSageMathOscarJuliaRust

Implementation

mappedwolframpackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts
mappedsagepackages/oracle/src/mappings.ts
mappedoscarpackages/oracle/src/mappings.ts
mappedjuliapackages/oracle/src/mappings.ts
mappedmathlib4packages/oracle/src/mappings.ts
mappedrustpackages/oracle/src/mappings.ts

See also: PowerMod, IntegerMod