Mod
Mod
Mod[ m, n ] returns the remainder when dividing two integers.
Usages
Mod[ m, n ]
Mod[ m, n ] returns the remainder when m is divided by n. The remainder is always between 0 and |n|-1, also for negative numbers. The result is 0 when n divides m, so Mod is often used to test divisibility.
| Example | in | out |
|---|---|---|
17 = 3 · 5 + 2. |
Mod[ 17, 5 ] |
2 |
5 divides 15. |
Mod[ 15, 5 ] |
0 |
A negative number: -8 = -3 · 3 + 1. |
Mod[ -8, 3 ] |
1 |
| A negative divisor gives the same result as a positive one. | Mod[ 8, -3 ] |
2 |
| Test if a script variable is even. | n := 12 |
r = True |
Mod[ list, n ]
If an argument is a list, the remainder is calculated for each element.
| Example | in | out |
|---|---|---|
The remainders when dividing by 3. |
Mod[ {10, 11, 12}, 3 ] |
{1, 2, 0} |
Details
| Invalid input | Result |
|---|---|
Mod[ 17, 0 ] |
Error: the second argument should be a non-zero natural number |
Mod[ 17 ] |
Error: the number of arguments should be 2 |
Mod[ 17.5, 5 ], Mod[ x, 5 ] |
Left unevaluated: the arguments must be integers. |
See also: CalcDivision, CalcDivisors, CalcGcd, EqualsEval