Skip to content

Remainders: Reading a mod m

Textbook

For an integer $a$ and a positive integer $m$, there are unique integers $q$ and $r$ with $a=qm+r$ and $0\le r<m$. The number $r$ is the remainder, written $a\bmod m$. Its required range makes the decomposition unique.

Since $17=3\cdot5+2$, it follows that $17\bmod5=2$. For a negative input, $-1=(-1)\cdot5+4$, so $-1\bmod5=4$. A programming language’s remainder operator may use a different convention for negative inputs. Translating the mathematical expression into code requires checking that contract.

Uniqueness follows from the range restriction. If $a=qm+r=q'm+r'$ with valid remainders, then $(q-q')m=r'-r$. The right side has absolute value below $m$, while the left is a multiple of $m$, so both must be zero. Existence follows by taking $q=\lfloor a/m\rfloor$ and $r=a-qm$: the floor inequalities directly give $0\le r<m$.

Definitions and further reading: Mathematics for Computer Science.

In plain language

Packing 17 objects into full boxes of capacity 5 leaves three boxes and two loose objects; the remainder describes the loose part. A negative input uses the same chosen range from zero to $m-1$, like a dial with five marks. A backward step from zero reaches the last mark, and each complete turn returns to the previous position. The box analogy ends there: a remainder records a position without keeping the accounts for every completed lap.

Example

The example compares division of 17 by 5 with input βˆ’1. The last step rejects remainder βˆ’1 because of its range, even though using quotient zero would preserve the equality.

Remainders: Reading a mod m

Full groups

Removing three groups of 5 from 17 leaves 2.

Step 1 of 3: Full groups

Read all steps
  1. Full groups

    Removing three groups of 5 from 17 leaves 2.

    State
    17current15current2current
  2. Negative input

    With q = βˆ’1, the remainder falls in the required range.

    State
    βˆ’1currentβˆ’5current4current
  3. Range check

    With modulus 5, only 0, 1, 2, 3, and 4 are allowed.

    State
    βˆ’1excluded0included1included2included3included4included

For input βˆ’1, the pair $q=0$, $r=-1$ passes the equality check but fails the range check. The pair $q=-1$, $r=4$ passes both and is therefore selected. Verification reconstructs the input as $qm+r$, then separately compares the remainder with zero and the modulus. Passing just one of these checks does not establish a valid Euclidean remainder.

Related concepts: Floor.

The original text of this free article is published under CC BY-NC-ND 4.0.

Previous / next

Previous
The Ceiling Function: Rounding Up
Next
Finite Sums: Reading the Summation Sign
  • Natural Numbers: Counting Discrete Objects

    Which values count discrete objects, and why the zero convention must be stated.

    Read article
    Math
  • Integers: Counting on Both Sides of Zero

    Positive and negative integers, zero, and the limits of integer division.

    Read article
    Math
  • Rational Numbers: Ratios of Integers

    Numbers as ratios of integers: equivalent fractions and the nonzero denominator requirement.

    Read article
    Math