\documentstyle[12pt,twoside,macpsfig,amssymb,amstex]{article}

\input{macros-course-lectures}
\parindent0pt
\parskip1.5ex

\begin{document}
\lecture{notes6}{Notes 6}{February 25, 1997}

Daniele's 1pm and 2pm recitations have been moved to  24-115, and 
Alex's 1pm recitation has been moved to 36-156.

\setcounter{section}{10}

\renewcommand{\gcd}[2]{\mbox{GCD}(#1,#2)}
\renewcommand{\lcm}[2]{\mbox{LCM}(#1,#2)}

\section{Natural Numbers and Integers}

We review some basic concepts: divisibility, primes, composite
numbers, modular arithmetic, etc., and give basic theorems about these notions, 
and some simple algorithms based on these theorems. 

Today we will talk about 
\begin{itemize}
\item Fundamental theorem of arithmetic -- every number has unique
factorization into primes. 
\item Algorithm for finding GCD, the greatest common divisor of two numbers. 
\end{itemize}

This subject matter is usually called {\em number theory}, and 
used to be just for fun, but recently has become very important in
practice, mainly because of applications to security.  Next time,
we'll see one---the RSA public key encryption scheme.

\subsection{Divisibility and Primes}

\begin{defin}
If $a$ and $b$ are integers, $a \neq 0$, then $a$ {\em divides} $b$
(written $a \mid b$) if $(\exists c)$, where $c$ is an integer, such that
$b = a c$.

We say $a$ {\em goes into} $b$ (in elementary school), or $b$ is {\em
divisible} by $a$.
\end{defin}

This makes sense even for negative integers, but we'll mainly think of
positive integers.

\begin{defin}
If $a$ is a natural number $\geq 2$, then
$a$ is {\em prime} if the only natural numbers dividing $a$ are $1$
and $a$.
\end{defin}

\begin{defin}
If $a$ is a natural number $\geq 2$, then
$a$ is {\em composite} if $a=bc$ for natural numbers $b,c \ne 1,a$.
\end{defin}

Basic divisibility properties:

\begin{theorem}
\label{additive}
\label{multiply}
\begin{enumerate}
\item
If $a \mid b$ and $a \mid c$, then $a \mid (b+c)$ and $a \mid (b-c)$.
\item
If $a \mid b$ then $a \mid bc$ for any $c$.
\item
If $a \mid b$ and $b \mid c$ then $a \mid c$.
\end{enumerate}
\end{theorem}

\begin{proof}
(Of part 1)
By the definition of divisibility, 
$(\exists t) b = at$ and $(\exists s) c = as$.\\  
Therefore, $b+c = at+as = a(t+s)$.\\  
Therefore, by definition, $a \mid b+c$. \\

\noindent Parts 2 and 3 are similar (use 2 to prove 3).
\end{proof}

In lecture 3 and last Friday's recitation, we already saw some results
of these theorems -- e.g., the division algorithm, and half of the
Fundamental Theorem of Arithmetic:

\begin{theorem}
The Fundamental Theorem of Arithmetic \\
Every natural number $\geq 1$ has a factorization into primes.
Moreover, the factorization is unique (except for reordering the factors).
\end{theorem}

We proved existence using strong induction, and later today, we will also prove uniqueness.

Another useful definition is:

\begin{defin}
If $x$ and $y$ are integers, where $y > 0$, then
$x \bmod y$ is the remainder on division of $x$ by $y$. 
\end{defin}

$x \bmod y$ is well-defined by the division algorithm, and we find that 
$0 \le x \bmod y < y$. 

\subsection{GCDs and LCMs}

Remember Die Hard? 
Why did buckets of size $5$ and $3$ work but not $6$ and $3$?
What if the buckets had sizes of $21$ and $133$, and goal was $13$ gallons?

\begin{defin}
Let $a$, $b$ be integers, not both $0$.
The {\em greatest common divisor} of $a$ and $b$, written
$\gcd{a}{b}$, is the largest natural number $g$ such that $g \mid a$
and $g \mid b$.
\end{defin}

We can prove that GCD is well-defined, 
using well-ordering to prove
that any nonempty upper-bounded set has a {\em largest} element. \\ 
The set
of common divisors includes $1$, and is finite because it is bounded
above by either number.  Note that GCD is always positive.

It turns out that in Die Hard, buckets of size $a$, $b$ always produce
numbers divisible by $\gcd{a}{b}$.
Can all such numbers in range $0, \ldots, \max{(a,b)}$ be
produced?

The following is an interesting theorem that shows that a GCD is always
expressible as a sum. 
We will this theorem for many purposes, especially in proving
the uniqueness part of the Fundamental Theorem of Arithmetic.

\begin{theorem}
Let $x$ and $y$ be integers, not both $0$. 
Then $\gcd{x}{y}$ can be expressed in the form $ax+by$ 
for some integers $a$ and $b$.
\end{theorem}

For example,
if $x = 22$, $y = 16$, 
then the GCD($x$,$y$) is $2$, express as $2 = 3 (22) + (-4) (16)$.
Note that we can use negative integers.

If $x = -22$, $y = 16$,  the GCD is again
$2$, and can be expressed as $2 = (-3) (-22) + (-4) (16)$.
(The GCD is always nonnegative.)

\begin{proof}
This is a nontrivial proof.  Until now, we've been looking at
mechanics of proving things.  This proof also requires some
creativity.

\noindent Assume without loss of generality that $x, y \geq 0$.  (Otherwise, use negative coefficients
as above.)
If either is $0$, then the GCD is defined to be the other.
So we're left with the case where $x$ and $y$ are both positive. 

\noindent Let $S$ be the set of positive integers expressible in the form $ax +
by$, where $a$ and $b$ are integers. 
Fix $l$ to be the smallest element in $S$, which exists by well-ordering, since
$S$ is nonempty because $x \in S$. 
Fix $a$ and $b$ such that $l = ax + by$. 

\noindent As an example, let $x=22$, and $y=16$. $S$ includes all numbers 
$ax+by$, for all integers $x$ and $y$. It includes:
\begin{eqnarray*}
0x + 1y &= 16\\
1x + (-1)y &= 8\\
(-1)x + 2y &= 10\\
2x + (-2)y &= 12\\
3x + (-4)y &= 2
\end{eqnarray*}
and many others. $l=2$ turns out to be the smallest element in $S$.
We claim $l$ is what we want, {\em i.e.}, $\gcd{x}{y}$. We need to show: 
\begin{enumerate}
\item
$l$ is a common divisor of $x$ and $y$. \\

First show $l \mid x$, by writing 
$x = ql+r$ by division algorithm, where $0 \leq r < l$. 
Then we find that:
\begin{eqnarray*}
r &= &x-ql\\
  &= &x - q(ax+by)\\
  &= &(1-qa)x + (-qb)y
\end{eqnarray*}
So $r$ is also expressible in the sum form in terms of x and y.
We know that $0 \leq r < l$, but 
if $r$ were positive, it would be in $S$, and would be smaller than
$l$, which would contradict our choice of $l$ as the smallest
element of $S$. \\
So it $r$ must be $0$, and 
so $x = q l$, which says that $l \mid x$. 

Similarly, $l \mid y$. 

\item
Any common divisor of $x$ and $y$ also divides $l$ (so $l$ is the
greatest -- if a number $c$ goes into a positive number $l$, then $c
\leq l$).

Easy: $c \mid x$ and $c \mid y$ imply $c \mid ax+by=l$.
\end{enumerate}
\end{proof}

The symmetric concept to GCD is LCM:

\begin{defin}
Let $a$, $b$ be positive integers. 
The {\em least common multiple} of $a$ and $b$, written
$\lcm{a}{b}$, is the smallest natural number $l$ such that 
$a \mid l$ and $b \mid l$.
\end{defin}

\subsection{Fundamental Theorem of Arithmetic}

With the addition characterization we just proved for the GCD, we are
finally ready to prove the Fundamental Theorem. 

Recall that we already proved existence using strong induction, so it 
remains to prove uniqueness. 
For this, we will use basic number theory lemmas: 

\begin{lemma}
Let $p$ be a prime, and $a$ and $b$ any integers.  
If $p \mid ab$, then $p \mid a$ or $p \mid b$.   
\end{lemma}

\begin{proof}
If $p \mid a$, we are done, otherwise, $\gcd{p}{a} = 1$,
because the only positive divisors of $p$ are $p$ and $1$, 
and $p$ doesn't divide $a$.
So by the addition form $ax +by$, we can write $1$ as a sum of the form
$1 = sa + tp$, where $s$ and $t$ are integers. \\
Multiplying by $b$ we get
$b = sab + tpb$. \\
We have $p \mid sab$, since $p \mid ab$.
and also, $p \mid tpb$. \\
So since $p \mid sab + tpb$, $p \mid b$ (by Theorem~\ref{additive}).
\end{proof}

\begin{corollary}
Let $p$ be a prime, and $a_1, \cdots, a_k$ any integers. 
If $p \mid a_1 a_2 \cdots a_k$, then $(\exists i) p\mid a_i$.
\end{corollary}
\begin{proof}
Homework
\end{proof}

Now prove uniqueness, in the Fundamental Theorem of Arithmetic:

\begin{proof} 
(Uniqueness of prime decomposition of a number $n$)
We argue by (strong) induction.  
BASE CASE:\\
The base case $n=1$ is trivial -- the product of no factors is defined
to be $1$. \\
INDUCTIVE STEP: \\
Suppose the claim is true for all numbers less than $n$, show it's true
for $n$: \\
Suppose for contradiction that there are two different factorizations,
$n=p_1p_2\cdots p_k$ and $n=q_1q_2\cdots q_l$.  \\
Then $p_1 \mid q_1\cdots q_l$. \\
By corollary, $(\exists i) p_1 \mid q_i$. \\
But since $q_i$ is prime, $p_1 = q_i$. \\
Divide both sides, and get two factorizations of $n/p_1$.  \\
This contradicts our inductive
hypothesis, so the theorem is proved.
\end{proof}

Unique factorization gives us a method for computing GCD: \\
Factor both numbers, and then multiply out all their common factors (note
that you may have multiple instances of the same prime factor).

Example:
100 vs. 120: \\
$100 = 2 \times 2 \times 5 \times 5$ \\
$120 = 2 \times 2 \times 2 \times 3 \times 5$ \\
Common factors (including multiplicities): $2$, $2$, $5$. \\
Product $2 \times 2 \times 5 = 20$ is GCD.

Note that we're taking the minimum number of occurrences of each prime in 
each of the two numbers. 
Likewise, unique factorization gives us a method for computing LCM:
Factor both numbers, and then multiply the maximum number of occurrences of each
prime.

Example:
100 vs. 120: \\
Product $2 \times 2 \times 2 \times 3 \times 5 \times 5 = 600$ is LCM.

These observations lead to the following useful connection between GCD
and LCM:

\begin{theorem}
Let $a$, $b$ be positive integers.
Then $a b = \gcd{a}{b} \times \lcm{a}{b}$

Example:
100 vs. 120: \\
$100 \times 120 = 20 \times 600$\\
$2^2 \cdot 5^2 \times 2^3 \cdot 3 \cdot 5 = 2^2 \cdot 5 \times 2^3 \cdot 3 \cdot 5^2$
\end{theorem}

\begin{proof}
For each distinct prime in the prime factorizations,
$ab$ uses the sum of the number of occurrences in $a$ and $b$, 
while $ \gcd{a}{b} \times \lcm{a}{b}$ uses the minimum plus the maximum number
of occurrences. 
So for each distinct prime, the number of occurrences is the same (the sum 
of two 
numbers is the same as their min plus their max). 
\end{proof}

\subsection{Euclid's GCD Algorithm}

Unfortunately, the ``algorithms'' given above to find GCD's and LCM's
-- factor and multiply some of the factors -- don't work well in practice
because factoring is expensive. 
No one knows how to do it very well -- and the execution time grows 
exponentially as the numbers grow. 

In this section, we present an algorithm for GCD (and therefore also LCM) that
{\em is} practical.
This doesn't help us in factoring the numbers , though. 

The algorithm was developed by Euclid, and is based on division. 

\subsubsection{Euclid's Algorithm for GCD}

\newcommand{\Euclid}[2]{\mbox{Euclid}(#1,#2)}

{\bf Procedure} $\Euclid{x}{y}$, where $x$ and $y$ are nonnegative integers, not
both $0$, and $x \geq y$:
\begin{enumerate}
\item If $y=0$, then return $x$.
\item Otherwise, return $\Euclid{y}{x \bmod y}$.
\end{enumerate}

Example execution: 
$\Euclid{22}{16}$ \\
Call $\Euclid{16}{6}$. \\
Call $\Euclid{6}{4}$. \\
Call $\Euclid{4}{2}$. \\
Call $\Euclid{2}{0}$. \\
Return $2$.

\subsubsection{Correctness Proof}

There are two properties we need to show:
\begin{itemize}
\item
If it ever terminates, it gives the right answer (partial
correctness).
\item
It eventually terminates.
\end{itemize}

Both of these are normally part of correctness proofs (for standard
sequential algorithms).

\paragraph{Partial correctness}

Why does this give the correct answer, if it terminates? \\
We will show that each step ``preserves'' the GCD, which implies (by simple
induction) that the whole algorithm does. \\

\begin{lemma}
If $x$, $y$ are any integers, $y \neq 0$, then
$\gcd{x}{y}=\gcd{y, x \bmod y}$.
\end{lemma}

\begin{proof}
We show that any nonzero integer $c$ is a common divisor of $x$ and
$y$ if and only if it's a common divisor of $y$ and $(x \bmod y)$. 

Write $x = q y + (x \bmod y)$, using the division algorithm. 

\noindent Suppose that $c$ is a common divisor of $x$ and $y$:
$c \mid x$ and $c \mid y$. \\
Then also $c \mid x - q y = x \bmod y$, by Theorem \ref{additive}. \\
So $c$ is a common divisor of $y$ and $(x \bmod y)$. \\

\noindent On the other hand, suppose that $c$ is a common divisor of $y$ and $(x
\bmod y)$ then $c \mid y$ and $c \mid x \bmod y$. \\
Then $c \mid qy + (x \bmod y) = x$ by Theorem~\ref{additive}.  \\
So $c$ is a common divisor of $x$ and $y$. 
\end{proof}

Since each call reduces the problem to one with the same GCD, the
algorithm, {\em if it terminates}, produces the correct GCD for the
original arguments. 

\begin{theorem}
If $\Euclid{x}{y}$ terminates, then its output is $\gcd{x}{y}$.
\end{theorem}

\begin{proof}
By induction on the number of recursive calls.  Suppose true for $n-1$
calls.  Consider an instance $\Euclid{x}{y}$ that terminates in $n$
calls.  Then The answer that is output is (by inductive hypothesis)
$\gcd{y}{x\bmod y}$ which is (by previous lemma) $\gcd{x}{y}$.
\end{proof}

\begin{theorem}
Euclid's algorithm terminates after at most $x+y$ recursive calls.
\end{theorem}

\begin{proof}
Strong induction on the sum of the two arguments, $x + y$.\\
BASE CASE:\\ 
The sum is $1$, so one argument is $0$, and the other $1$. $\Euclid{x}{y}$
terminates in $1$ step, with $1$ as output. \\
INDUCTIVE STEP: \\
Suppose $\Euclid{x}{y}$ terminates whenever the
sum of the arguments is $< n$, and consider
$\Euclid{x}{y}$ where $x + y = n$. 
We want to show that it terminates after at most $n$ recursive calls. \\
If it terminates in $1$ pass, we're done, since $1 \leq n$. \\
If not, then it calls $\Euclid{y}{x \mod y}$. \\

\noindent The sum of the arguments in the call is $y + (x \mod y)$,
which we claim is strictly less than $n = x + y$, {\em i.e.}, is $\leq
n-1$. \\ If we show this, then we can apply strong induction to show
that the number of recursive calls remaining is at most $n-1$, which
adds to the present pass to yield the result. \\ 

\noindent So we just need to show that $y + (x \mod y) < x + y$, {\em i.e.},
$x \mod y < x$. This is true since $x \geq y$.
\end{proof}

Here's an alternative proof that some might find clearer:
\begin{proof}
Strong induction on the second argument $y$.\\
INDUCTIVE STEP (complete with implicit BASE CASE):
Suppose $\Euclid{x}{y}$ terminates whenever $y<n$.\\
Consider a call to $\Euclid{x}{n}$.  \\
If $n=0$ (base case) then we terminate in $0\le x+n$ calls.\\
If $n \ne 0$, we call $\Euclid{n}{x \bmod n}$.  But we know $x \bmod n
< n$.\\
Thus, by inductive hypothesis, recursive call terminates in at most $n+x
\bmod n$ calls.  \\
So our call terminate in $n+x \bmod n +1 < n+x+1 \le n+x$ calls.
\end{proof}


\begin{example} Show that $\gcd{949}{273} = 13$ as follows:
\begin{verbatim}
quotient    x       y
           949     273  
   3       273     130  
   2       130      13  
  10        13       0  
\end{verbatim}
\end{example}

\end{document}
% LocalWords:  pt Daniele's pm Alex's GCD LCM GCDs LCMs vs GCD's LCM's
