Number TheoryDivisibility and the Euclidean Algorithm

The Division Algorithm

For a positive divisor b, every integer a can be written as a = bq + r with 0 ≤ r < b, even when a is negative.

Not started Jump to practice

In grade 4 you wrote $17 \div 5 = 3$ R $2$. The division algorithm says the same thing as an equation, $17 = 5 \cdot 3 + 2$, and makes it work for every integer, negative ones included. Almost everything in this course leans on it: remainders, bases, the Euclidean algorithm and congruences.

Theorem (division algorithm). Let $a$ and $b$ be integers with $b > 0$. There are unique integers $q$ (the quotient) and $r$ (the remainder) with $$a = bq + r \quad\text{and}\quad 0 \le r < b.$$ In fact $q = \lfloor a/b \rfloor$, the largest integer that is at most $a/b$, and $r = a - bq$.

The multiples of 5 cut the line into pieces of length 5. $a = -17$ lies in the piece from $-20$ to $-15$, so $q = -4$, and the hop from $-20$ to $-17$ is the remainder $r = 3$.

Why it works

Existence. The multiples $\dots, -2b, -b, 0, b, 2b, \dots$ cut the number line into pieces $[bq, bq + b)$ of length $b$ that do not overlap and cover everything. So $a$ lies in exactly one of them: $bq \le a < bq + b$. Subtract $bq$: $0 \le a - bq < b$. Call $a - bq$ the remainder $r$.
Uniqueness. If also $a = bq' + r'$ with $0 \le r' < b$, then $b(q - q') = r' - r$. The right side is strictly between $-b$ and $b$, and the left side is a multiple of $b$, so both are $0$: $q = q'$ and $r = r'$.

Worked example: A positive dividend

Divide $a = 47$ by $b = 6$.
  1. The largest multiple of $6$ that is at most $47$ is $6 \cdot 7 = 42$, so $q = 7$.
  2. $r = 47 - 42 = 5$, and $0 \le 5 < 6$.

Answer: $47 = 6 \cdot 7 + 5$.

Worked example: A negative dividend

Divide $a = -17$ by $b = 5$.
  1. The multiples of $5$ near $-17$ are $-20$ and $-15$. We need the one at or below $-17$: $5 \cdot (-4) = -20$. So $q = -4$.
  2. $r = -17 - (-20) = 3$, and $0 \le 3 < 5$.
  3. Check: $5 \cdot (-4) + 3 = -20 + 3 = -17$.

Answer: $-17 = 5 \cdot (-4) + 3$, so $q = -4$ and $r = 3$.

Worked example: Counting backward on a calendar

Today is Wednesday. What day of the week was it 100 days ago?
  1. Going back 100 days is a change of $-100$ days. Divide by $7$: $-100 = 7 \cdot (-15) + 5$, since $7 \cdot (-15) = -105 \le -100$.
  2. The $15$ whole weeks change nothing, so going back 100 days lands on the same day as going forward $5$ days.
  3. Wednesday plus 5 days is Monday.

Answer: Monday.

Common mistake

Giving a negative remainder: "$-17 = 5 \cdot (-3) - 2$, so the remainder is $-2$." That equation is true, but $-2$ is not allowed: the remainder must satisfy $0 \le r < b$. Borrow one more $b$ from the quotient: $-17 = 5 \cdot (-4) + 3$. Quick check: is $r$ between $0$ and $b - 1$? (A shortcut: when $b \nmid a$, the remainder of $-a$ is $b$ minus the remainder of $a$. Here $17$ leaves $2$, so $-17$ leaves $5 - 2 = 3$.)
Programming languages disagree here. In JavaScript, C and Java, -17 % 5 is -2; in Python it is 3. Mathematics always uses the remainder from $0$ to $b - 1$.

Key idea

For $b > 0$, every integer $a$ can be written in exactly one way as $a = bq + r$ with $0 \le r < b$. For negative $a$ the quotient rounds down (toward $-\infty$), which keeps the remainder nonnegative.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 1 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 1, Prime Numbers (the division algorithm)
  • Lehman, Leighton and Meyer, Mathematics for Computer Science (MIT OpenCourseWare 6.042J), chapter on number theory: the division theorem
@teddyrookbook GitHub