next up previous
Next: Minimal polynomials and conjugate Up: lecture4 Previous: lecture4

The Euclidean algorithm

The Euclidean algorithm is perhaps the oldest algorithm in the world, being attributed to Euclid and appearing in his Elements. It applies on any Euclidean domain:
\begin{definition}
A {\bf Euclidean domain} is a set $D$ with operations $+$ ...
...tegers, the division
algorithm.
\end{enumerate} \end{enumerate}\end{definition}
Integers and polynomials form a Euclidean domain, where the metric for integers is simply the absolute value, and for polynomials it is the degree of the polynomial.

Before we get to Euclid's algorithm, we need one more fact about GCDs: For any integer $ x$, $ (a,b) = (b,a) = (a,-b) = (a,b+ax)$.

Euclid's algorithm finds the greatest common divisor of a pair elements in a Euclidean domain.

The Euclidean algorithm works by repeated division.
\begin{example}
Let $a=168$ and $b=166$, and find (168,166). We have
\begin{di...
...isplaymath}Take the {\bf last nonzero remainder}: $(168,166) = 2$.
\end{example}

\begin{example}
Find $(336,54)$. Divide:
\begin{displaymath}336 = 54 \cdot 6 + ...
... 0
\end{displaymath}Take the last nonzero remainder: (336,54) = 6.
\end{example}
This can be coded in nothing at all in MATLAB:
\begin{newprogenv}{GCD program 1}{gcdint1.m}{}{gcd1}{GCD program 1}{}{}{}{}1
\end{newprogenv}

We can do more, however. It is a fact of number theory that

$\displaystyle (a,b) = ax + by
$

for some integers $ x$ and $ y$. We can use the Euclidean algorithm to determine $ x$ and $ y$. In many of our applications, these will be the numbers that we need. (Another useful fact: if there is an $ x$ and $ y$ such that $ ax + by=1$, then $ (a,b)=1$.)
\begin{example}
Determine $(851,966)$. By the division algorithm we can write
\...
...dot 851,
\end{aligned}\end{displaymath}so $x = 15$ and $y = -17$.
\end{example}
More formally, the algorithm may be stated in the following theorem, which also fixes some useful notation.

Theorem 1 (The Euclidean algorithm)   Let $ b$ and $ c$ be integers $ >0$. Then by repeated application of the division algorithm write

$\displaystyle \begin{aligned}
b &= cq_1 + r_1 \qquad 0 < r_1 < c \\
c &= r_1 q...
..._j + r_j \qquad 0 < r_j < r_{j-1} \\
r_{j-1} &= r_j q_{j+1} + 0.
\end{aligned}$

Then $ (b,c) = r_j$, the last nonzero remainder of the division process.

That the theorem stops after a finite number of steps follows since every remainder must be less than the preceding remainder.

If the GCD $ g$ and the coefficients $ x$ and $ y$ are desired such that

$\displaystyle b x + cy = g,
$

a little more work is required. Observe that the recursion in the Euclidean algorithm may be expressed as

$\displaystyle q_i$ $\displaystyle = \lfloor r_{i-2}/r_{i-1} \rfloor$    
$\displaystyle r_{i}$ $\displaystyle = r_{i-2} - r_{i-1}q_i$ (1)

for $ i=1,2,\ldots$ (until termination) with $ r_{-1} = b$ and $ r_0 =
c$. The values for $ x$ and $ y$ may be obtained by finding intermediate integers $ x_i$ and $ y_i$ satisfying

$\displaystyle b x_i + c y_i = r_i
$

In conjunction with (1) we obtain

\begin{displaymath}\begin{split}x_i &= x_{i-2} - q_i x_{i-1} \ y_i &= y_{i-2} - q_i y_{i-1}\end{split}\end{displaymath} (2)

for $ i=1,2,\ldots$ (until termination) with

$\displaystyle \begin{aligned}
x_{-1} &= 1\qquad x_0 = 0 \\
y_{-1} &= 0\qquad y_0 = 1
\end{aligned}$

This algorithm applies as well to polynomials (over a field) as to integers. The following implementation is for polynomials.
\begin{newprogenv}{GCD program 2 -
polynominals}{gcdpoly.m}{}{gcd2}{GCD program 2 -polynomials}{}{}{}{}
\end{newprogenv}
As an example, we can try
>> [g,x,y] = gcdpoly([4 10 8 2],[8 14 7 1])
g =
    1.0000    1.5000    0.5000
x =
    0.3333
y =
   -0.1667


next up previous
Next: Minimal polynomials and conjugate Up: lecture4 Previous: lecture4
Todd Moon 2009-11-06