# SP20:Lecture 15 GCD algorithm

We finished the proof that the base b representation exists. We also discussed base b arithmetic. Finally we introduced Euclid's GCD algorithm, and listed the properties we need to prove to justify calling it the greatest common divisor.

## Contents

# Base b representation

We started this lecture by further discussion of base b representation, which we started last lecture. Recall that the base b interpretation of a sequence of digits is:

**Base b interpretation**

## Existence

Note that this proof contains an algorithm for finding the digits. Note also that The base b representation is unique.

In the base case, we WTS must find a base b representation of 0. The natural choice might be to take the single digit . Then is a digit (since ), and clearly .

However, when using this proof as an algorithm, it is preferable to take the 'empty' sequence as the representation of 0; otherwise all numbers would have a leading 0. Alternatively, one could give a special case in the inductive step for when is already a digit.

For the inductive step, assume that every number less than has a base b representation. We wish to show that has one also.

We can find the digits by dividing by : the last digit is given by the remainder, and the rest of the digits are those of the quotient.

Formally, let and . Let be the base b representation of (which we know exists by the inductive hypothesis).

Let let . We have

, and## Uniqueness

As usual, in order to justify calling the base b representation of "the" base b representation, we need to prove that it is unique. In fact, it is not necessarily unique if we allow leading 0's (for example, ), but if we rule them out, then we have the following fact:

The proof is left as an exercise. Use induction on and the fact that The quotient and remainder are unique.

## Arithmetic in base b

You are already familiar with several digit-by-digit algorithms for working with decimal numbers, such as long addition, long multiplication, and long division.

It turns out that these algorithms also work in base b; one just needs to represent digits in base . For example, to add and , we can add them digit by digit, starting from the right, and handling carries appropriately. We would add 6 and 4 to get the number 10, which is represented by ; we would keep the 3 and carry the 1. We would then add the carry and the second digits to get ; again we keep the 2 and carry the 1. Finally we add the third digit and the carry to get the third digit 2; this gives us .

1 | 2 | 4 | ||

+ | 6 | 6 | (base 7) | |

2 | 2 | 3 |

You could prove that these algorithms all work using induction.

# GCD algorithm

An important technical tool for our study of number theory is the greatest common divisor of two numbers. Here we present an efficient algorithm for computing it:

**gcd**

This function is a good inductively defined function because by definition, , so the second argument is decreasing in the inductive definition.

The algorithm can be extended to operate on negative integers by simply taking the gcd of the absolute values of its arguments.

In order to justify calling divisor", we should check that gcd(a,b) is a common divisor of a and b and gcd(a,b) is greater than all other common divisors of a and b.

the "greatest common