Lecture 30: Number bases, Euclidean GCD algorithm, and strong induction

Base \(b\) representation

When we write a number \(n \in \mathbb{N}\), we represent it as a sequence of digits \(d_kd_{k-1}\dots{}d_2d_1d_0\) where for each \(d_i\), \(0 \leq d_i \lt 10\). We interpret this string of digits as a number by reading \(d_0\) in the ones place, \(d_1\) in the tens place, \(d_2\) in the hundreds place, and so on. In other words, we have

\[ n = d_k \cdot 10^k + d_{k-1} \cdot 10^{k-1} + \cdots + d_2 \cdot 10^2 + d_1 \cdot 10^1 + d_0 \cdot 10^0 = \sum_{i} d_i 10^i \]

We can represent the same number with digits between \(0\) and \(b\) for an arbitrary \(b\) using exactly the same interpretation (with \(b\) replacing \(10\)); if \(d_kd_{k-1}\dots{}d_2d_1d_0\) is a string of digits with each \(d_i\) satisfying \(0 \leq d_i \lt b\), then

\[(d_kd_{k-1}\dots{}d_2d_1d_0$)_b = \sum_i d_ib^i\]

If this sum is \(n\), we say that \(d_k\dots{}d_1d_0\) is a base-\(b\) representation of \(n\).

Number bases in the wild

binary is another name for base 2, octal means base 8, and hexadecimal means base 16. In computer languages, one often writes octal numbers with a preceeding 0 and hexadecimal numbers with a proceeding 0x.

When writing numbers in a base greater than 10, it is conventional to use letters for digits bigger than 10. The usual convention is \(a = 10\), \(b = 11\), \(\dots\), \(f = 15\). For example, \(930 = (3A2)_{16}\) which might be written \(0x3A2\) in some programming languages; this is also equal to \((1110100010)_2\).

The primary reason to use number bases other than 10 is that it is easy to divide by \(b\) with remainder: the remainder is just the last digit, and the quotient is given by the remaining digits. For example, hexadecimal is useful because when building computer systems it is convenient to store blocks of data of sizes that are powers of two; counting the number of blocks means dividing by a power of two, and often that means dividing by 16.

Existence of base \(b\) representation

Claim: Every \(n \in \mathbb{N}\) has a base-\(b\) representation for all \(b \geq 2\).

Proof attempt: By induction on \(n\). Fix \(b\), and let \(P(n)\) be the statement "\(n\) has a base \(b\) representation." We will try to show \(P(0)\) and \(P(n)\) assuming \(P(n-1)\).

\(P(0)\) is easy: \(0\) is represented by the empty string of digits, because the sum over the empty sequence is 0:

\(()_b = \sum_{0 \leq i \lt 0} d_ib^i = 0\).

If you prefer, we could take a single-digit representation \(0 = (0)_b\).

To prove \(P(n)\), first assume \(P(n-1)\), i.e. that \(n-1\) has a base-\(b\) representation. We would like to use the fact that the last digit of the base \(b\) representation of \(n\) should be \(rem(n,b)\) and the other digits are the digits of \(quot(n,b)\). In other words, we would like to write the following:

Let \(q\) and \(r\) be the quotient and remainder of \(n\) over \(b\). Let \(d_0 = r\) and let \(d_jd_{j-1}\dots{}d_2d_1\) be the digits of the base-\(b\) representation of \(q\). Note that we have relabeled them so that the last digit is \(d_1\); this means \(quot(n,b) = \sum_i d_{i}b^{i-1}\). Then \[n = qb + r = (\sum_{i>0} d_i b^{i-1})b + d_0 = \sum_{i\geq 0} d_ib^i\] as required.

However, this proof is invalid, because of the line "let \(\cdots\) be digits of the base-\(b\) representation of \(q\)". We can only do this if we know that \(q\) has a base-\(b\) representation, and that is what we are trying to prove.

We could consider using the inductive hypothesis, but this doesn't help, because the inductive hypothesis says that n-1 has a base-\(b\) representation, not that \(q\) has a base-\(b\) representation.

To make this proof go through, we need to strengthen the inductive hypothesis, so that it not only tells us \(n-1\) has a base-\(b\) representation, but that every number less than or equal to \(n-1\) has a base \(b\) representation. This will let us finish the proof as above, because \(q\) will be less than \(n\).

Fixed proof: By induction on \(n\). Let \(P(n)\) be the statement "for all \(k \in \mathbb{N}\) with \(k \leq n\), \(k\) has a base-\(b\) representation." (Compare this to \(P(n)\) in the failed proof above). We will prove \(P(0)\) and \(P(n)\) assuming \(P(n-1)\).

To prove \(P(0)\), we must show that for all \(k\) with \(k \leq 0\), that \(k\) has a base \(b\) representation. Since the only \(k \leq 0\) is \(0\), this is exactly the same as the proof of \(P(0)\) above.

To prove \(P(n)\), we first assume \(P(n-1)\), i.e. that for all \(k \leq n-1\), \(k\) has a base-\(b\) representation. We must show that for all \(k \leq n\), that \(k\) has a base-\(b\) representation. Note that our assumption gives us almost all of the cases: if \(k \neq n\), then \(k \leq n-1\) so \(P(n-1)\) tells us that \(k\) has a base \(b\) representation. So all that's left is to show that \(n\) has a base \(b\) representation.

This part is the same as our aborted proof above, except that it works.

Let \(q\) and \(r\) be the quotient and remainder of \(n\) over \(b\). Let \(d_0 = r\) and let \(d_jd_{j-1}\dots{}d_2d_1\) be the digits of the base-\(b\) representation of \(q\) we know this representation exists because \(q \lt n\). Note that we have relabeled them so that the last digit is \(d_1\); this means \(quot(n,b) = \sum_i d_{i}b^{i-1}\). Then \[n = qb + r = (\sum_{i>0} d_i b^{i-1})b + d_0 = \sum_{i\geq 0} d_ib^i\] as required.

Strong induction

This is the idea behind strong induction. Given a statement \(P(n)\), you can prove \(\forall n, P(n)\) by proving \(P(0)\) and proving \(P(n)\) under the assumption \(\forall k \lt n, P(k)\). Compare this to weak induction, which requires you to prove \(P(0)\) and \(P(n)\) under the assumption \(P(n-1)\). Here is the proof above written using strong induction:

Rewritten proof: By strong induction on \(n\). Let \(P(n)\) be the statement " \(n\) has a base-\(b\) representation." (Compare this to \(P(n)\) in the successful proof above). We will prove \(P(0)\) and \(P(n)\) assuming \(P(k)\) for all \(k \lt n\).

To prove \(P(0)\), we must show that for all \(k\) with \(k \leq 0\), that \(k\) has a base \(b\) representation. Since the only \(k \leq 0\) is \(0\), this is exactly the same as the proof of \(P(0)\) above.

To prove \(P(n)\), we first assume \(P(k)\) for all \(k \lt n\). We want to show that \(n\) has a base \(b\) representation.

Let \(q\) and \(r\) be the quotient and remainder of \(n\) over \(b\). Let \(d_0 = r\) and let \(d_jd_{j-1}\dots{}d_2d_1\) be the digits of the base-\(b\) representation of \(q\) we know this representation exists because \(q \lt n\) so we have assumed \(P(q)\). Note that we have relabeled them so that the last digit is \(d_1\); this means \(quot(n,b) = \sum_i d_{i}b^{i-1}\). Then \[n = qb + r = (\sum_{i>0} d_i b^{i-1})b + d_0 = \sum_{i\geq 0} d_ib^i\] as required.

Euclid's GCD algorithm

A technical tool that will be useful to us in the coming lectures is Euclid's algorithm for finding the greatest common divisor. The algorithm is given by an inductively defined function:

Let \(g : \mathbb{N} \times \mathbb{N} \rightarrow \mathbb{N}\) be given as follows: \(g(a,0) ::= a\), and \(g(a,b) ::= g(b,rem(a,b))\). This inductive use of \(g\) is justified because \(rem(a,b) \lt b\).

Claim: \(g(a,b)\) gives the greatest common divisor of \(a\) and \(b\). That is, \(g(a,b)\) is a divisor of both \(a\) and \(b\), and any other divisor \(c\) of both \(a\) and \(b\) is less than \(g(a,b)\). In fact, \(c | g(a,b)\).

Proof: By strong induction on \(b\). Let \(P(b)\) be the statement "for all \(a\), \(g(a,b) | a\), \(g(a,b) | b\), and if \(c | a\) and \(c | b\) then \(c | g(a,b)\)."

In the base case, we must choose an arbitrary \(a\) and show that:

  1. \(g(a,0) | a\). This is clear, because \(g(a,0) = a\) and \(a | a\).
  2. \(g(a,0) | 0\). This is the same as saying that there exists \(k\) with \(kg(a,0) = 0\). Choosing \(k = 0\) works. This is a wordy way of saying that everything divides 0.
  3. If \(c | a\) and \(c | 0\) then \(c | g(a,b)\). Well, if \(c | a\) then \(c | g(a,0)\) because \(g(a,0) = a\).

In the inductive case, we assume that for all \(k \lt b\), that \(P(k)\) holds. We wish to show \(P(n)\). Choose an arbitrary \(a\).

We know \(g(a,b) = g(b,r)\) where \(a = qb + r\) and \(r \lt b\). Since \(r \lt b\), we can apply \(P(r)\) to conclude \(g(b,r) | b\) and \(g(b,r) | r\), and also that if \(c | b\) and \(c | r\) then \(c | g(b,r)\). We must show:

  1. \(g(a,b) | a\). Since \(g(b,r) | b\), we know (by definition) that there is some \(k\) such that \(kg(b,r) = b\). Similarly, there is some \(ℓ\) such that \(ℓg(b,r) = r\). Plugging this in to \(a = qb + r\) we have \[a = qkg(b,r) + ℓg(b,r) = (qk+ℓ)g(b,r) = (qk+ℓ)g(a,b)\] Thus \(g(a,b) | a\).
  2. \(g(a,b) | b\). This is clear since \(g(b,r) | b\) and \(g(a,b) = g(b,r)\).
  3. If \(c | a\) and \(c | b\) then \(c | g(a,b)\). Since \(c | a\) there exists \(k\) with \(kc = a\) and since \(c | b\) there exists \(ℓ\) with \(ℓc = b\). Now \(a = qb + r\) so \(r = a - qb = qkc - ℓc = (qk-ℓ)c\) so \(c | r\). Thus our inductive hypothesis lets us conclude that \(c | g(b,r) = g(a,b)\), as required.

Euclid's algorithm for negative integers

Note that \(a | b\) if and only if \(-a | b\) if and only if \(a | -b\). Therefore, for any \(a,b \in \mathbb{Z}\) one can find \(gcd(a,b)\) by finding \(g(|a|,|b|)\).