Loading [MathJax]/jax/output/HTML-CSS/jax.js

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

Base b representation

When we write a number nN, we represent it as a sequence of digits dkdk1d2d1d0 where for each di, 0di<10. We interpret this string of digits as a number by reading d0 in the ones place, d1 in the tens place, d2 in the hundreds place, and so on. In other words, we have

n=dk10k+dk110k1++d2102+d1101+d0100=idi10i

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 dkdk1d2d1d0 is a string of digits with each di satisfying 0di<b, then

(dkdk1d2d1d0$)b=idibi

If this sum is n, we say that dkd1d0 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, , 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 nN has a base-b representation for all b2.

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(n1).

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

()b=0i<0dibi=0.

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

To prove P(n), first assume P(n1), i.e. that n1 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 d0=r and let djdj1d2d1 be the digits of the base-b representation of q. Note that we have relabeled them so that the last digit is d1; this means quot(n,b)=idibi1. Then n=qb+r=(i>0dibi1)b+d0=i0dibi as required.

However, this proof is invalid, because of the line "let 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 n1 has a base-b representation, but that every number less than or equal to n1 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 kN with kn, 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(n1).

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

To prove P(n), we first assume P(n1), i.e. that for all kn1, k has a base-b representation. We must show that for all kn, that k has a base-b representation. Note that our assumption gives us almost all of the cases: if kn, then kn1 so P(n1) 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 d0=r and let djdj1d2d1 be the digits of the base-b representation of q we know this representation exists because q<n. Note that we have relabeled them so that the last digit is d1; this means quot(n,b)=idibi1. Then n=qb+r=(i>0dibi1)b+d0=i0dibi as required.

Strong induction

This is the idea behind strong induction. Given a statement P(n), you can prove n,P(n) by proving P(0) and proving P(n) under the assumption k<n,P(k). Compare this to weak induction, which requires you to prove P(0) and P(n) under the assumption P(n1). 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<n.

To prove P(0), we must show that for all k with k0, that k has a base b representation. Since the only k0 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<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 d0=r and let djdj1d2d1 be the digits of the base-b representation of q we know this representation exists because q<n so we have assumed P(q). Note that we have relabeled them so that the last digit is d1; this means quot(n,b)=idibi1. Then n=qb+r=(i>0dibi1)b+d0=i0dibi 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:N×NN 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)<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<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<b. Since r<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=aqb=qkcc=(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,bZ one can find gcd(a,b) by finding g(|a|,|b|).