# Extention of Euclid's GCD Algorithm. (The Art of Computer Programming, Volume 1, Edition 3, Section 1.2.1, Exercise 12)

Euclid's GCD algorithm which is used to find GCD of two input numbers, say, $c$ and $d$, needs the inputs to be positive integers.

Exercise 12 provides an extension to this algorithm and allows $c$ & $d$ to accept values of the form $u+v\sqrt{2}$, where $u$ and $v$ are integers. In this case we can find a $r$ (of the form $u+v\sqrt{2}$) such that $c=dq+r$ , $q$ is a positive integer.
The algorithm can then continue as usual with $c$<-$d$ and $d$<-$r$ in the next step.

The algorithm will however not terminate if $c=1$ and $d=\sqrt{2}$ because there is no common divisor($q$) here.

However, the algorithm can be made to terminate in this case also if some extension is done to the divisor $q$, as explained here (by the author):

If we extend the concept of divisor so that $u+v\sqrt{2}$ is said to divide $a(u+v\sqrt{2})$ if and only if $a$ has the form $u'+v'\sqrt{2}$ for integers $u'$ and $v'$, there is a way to extend the algorithm so that it always will terminate. If we have $c=u+v\sqrt{2}$ and $d=u'+v'\sqrt{2}$, compute $c/d=c(u'-v'\sqrt{2})/(u'^2-2v'^2)=x+y\sqrt{2}$ where x and y are rational. Now let $q=u''+v''\sqrt{2}$ where $u''$ and $v''$ are the nearest integers to $x$ and $y$; and let $r=c-qd$. If $r=u'''+v'''\sqrt{2}$, it follows that $|u'''^2-2v'''^2|<|u'^2-2v'^2|$, hence the computation will terminate.

I did not understand the last line that

If $r=u'''+v'''\sqrt{2}$, it follows that $|u'''^2-2v'''^2|<|u'^2-2v'^2|$, hence the computation will terminate.

Please explain how $|u'''^2-2v'''^2|<|u'^2-2v'^2|$
and how this proves that computation will terminate.

• To learn more about euclidean quadratic number fields I recommend having on hand at least one number theory textbook. This is discussed in many classical textbooks, e.g. those by Hardy & Wright, Harvey Cohn, and Harold Stark, to name just a few of many elementary expositions. – Bill Dubuque Dec 19 '14 at 16:58

The computation should terminate because of the same reason as the usual Euclidean algorithm, in which we search the greatest common divisor of natural numbers $$c$$ and $$d$$ with $$c>d$$. Namely, we have $$c=qd+r$$ with $$r. If $$r>0$$ then we have $$d=q’r+r’$$ with $$r’ and so forth. Following this way we consecutively construct a sequence $$b>r>r’>r’’>\dots$$ of non-negative integer residues, which have to be finite and stop at the zero. In the question to this sequence corresponds a sequence $$|b|>|r|>|r’|>\dots$$, where $$|u+\sqrt{2}|=|u^2-2v^2|$$. When the sequence stops at $$|r_k|=|u_k+\sqrt{2}v_k|$$ we have $$|u_k^2-2v_k^2|=0$$. Since $$\sqrt{2}$$ is irrational, the equality can hold only if $$u_k=v_k=0$$.
Algebraically, the proposed algorithm extension is a part of a proof that a ring consisting of numbers of the form $$u+\sqrt{2}v$$ is a Euclidean domain, endowed with a Euclidean function $$|u^2-2v^2|$$. In particular, that $$|u'''^2-2v'''^2|<|u'^2-2v'^2|$$ is proved in this answer.