Lecture 17: Division and number bases

Working in base b

Base b representation is a way to write numbers using the digits {0, 1, …, (b − 1)}.

Common bases:

A string of digits in base b, written (anan − 1. . . a3a2a1a0)b, represents the number a0b0 + a1b1 + a2b2 + ⋯ + anbn.

Arithmetic in base b

There are two ways to work with numbers in base b. The hard way: convert to base 10, apply the algorithms you already know for addition and multiplication, convert back.

The easy way: use the algorithms you already know for addition, multiplication, division, but remember that (10)b stands for b and not 10.

We did examples with long addition. Long multiplication and division work the same way.

Euclidean Division

Euclidean division algorithm is actually a theorem. There is a close connection between proofs and algorithms, and you can extract an algorithm from the proof.

Theorem (Euclidean division algorithm): For any numbers a and b ≠ 0, there exist unique numbers q and r satisfying

  1. a = qb + r
  2. 0 ≤ r < b

Proof (existence): To simplify, we'll only prove for a > 0 and b > 1. The cases for a = 0, a < 0, b = 1 and b < 0 are all straightforward extensions. I encourage you to check them on your own.

We'll prove the claim by induction on a. In the base case, when a = 1, we can simply choose q = 0 and r = 1. The two properties are immediately verified.

In the inductive case, assume that there exists qʹ and rʹ satisfying a = qʹb + rʹ and 0 ≤ rʹ < b. We wish to show that there are numbers q and r satisfying a + 1 = qb + r.

We consider two cases separately:

  1. rʹ < b − 1. In this case, let q = qʹ and let r = rʹ + 1. It is easy to see that
    a + 1 = qʹb + rʹ + 1 = qb + r
    and since 0 ≤ rʹ < b − 1, we see 0 < r < b.

  2. rʹ = b − 1. In this case, choose q = qʹ + 1 and r = 0. Then
    a + 1 = qʹb + rʹ + 1 = (q − 1)b + (rʹ + 1) = (q − 1)b + b + 0 = qb + r
    and clearly 0 ≤ r < b.

Proof (uniqueness): suppose there are two different pairs q, r and qʹ, rʹ satisfying properties 1 and 2. Then we have
a = qb + r = qʹb + rʹ
so (q − qʹ)b = rʹ − r.

The possible values of the left hand side are . . . ,  − 2b,  − b, 0, b, 2b, . . . , so the left hand side is either 0 or  ≥ b or  ≤  − b.

Recall that 0 ≤ r < b and 0 ≤ rʹ < b. The biggest the right hand side can be is if rʹ = b − 1 and r = 0, in this case the right hand side is b − 1. Similarly, smallest it can be is  − (b − 1).

Thus the only way the left and right hand sides can be equal is if they are both 0. Thus r − rʹ = 0 and qʹ − q = 0, so r = rʹ and q = qʹ, as required.