\section{The Euclidean Algorithm}

\definition{}
The \textit{greatest common divisor} of $a$ and $b$ is the greatest integer that divides both $a$ and $b$. \par
We denote this number with $\gcd(a, b)$. For example, $\gcd(45, 60) = 15$.

\problem{}
Find $\gcd(20, 14)$ by hand.

\begin{solution}
	$\gcd(20, 14) = 2$
\end{solution}

\vfill

\theorem{The Division Algorithm}<divalgo>
Given two integers $a, b$, we can find two integers $q, r$, where $0 \leq r < b$ and $a = qb + r$. \par
In other words, we can divide $a$ by $b$ to get $q$ remainder $r$.

\theorem{}<gcd_abc>
For any integers $a, b, c$, \par
$\gcd(ac + b, a) = \gcd(a, b)$

\problem{}
Compute the gcd of 12 and 976.
\begin{solution}
	$976 = 3 \times 324 + 4 = 3 \times 4 \times 81 + 4$
	So, $\gcd(a, b) = 4$
\end{solution}

\vfill

\problem{The Euclidean Algorithm}<euclid>
Using the two theorems above, detail an algorithm for finding $\gcd(a, b)$. \par
Then, compute $\gcd(1610, 207)$ by hand. \par

\begin{solution}
	Using \ref{gcd_abc} and the division algorithm,

	% Minipage prevents column breaks inside body
	\begin{multicols}{2}
		\begin{minipage}{\columnwidth}
			$\gcd(1610, 207)$ \par
			$= \gcd(207, 161)$ \par
			$= \gcd(161, 46)$ \par
			$= \gcd(46, 23)$ \par
			$= \gcd(23, 0) = 23$ \par
		\end{minipage}

		\columnbreak

		\begin{minipage}{\columnwidth}
			$1610 = 207 \times 7 + 161$ \par
			$207 = 161 \times 1 + 46$ \par
			$161 = 46 \times 3 + 23$ \par
			$46 = 23 \times 2 + 0$ \par
		\end{minipage}
	\end{multicols}
\end{solution}

\vfill
\pagebreak




\problem{}<extendedeuclid>
Using the output of the Euclidean algorithm,

\begin{itemize}
	\item[-] find a pair $(u, v)$ that satisfies $20u + 14v = \gcd(20, 14)$
	\item[-] find a pair $(u, v)$ that satisfies $541u + 34v = \gcd(541, 34)$
	% gcd = 1
	% u = 11; v = -175
\end{itemize}
This is called the \textit{extended Euclidean algorithm}. \par
\hint{
	You don't need to fully solve the last part of this question. \\
	Understand how you \textit{would} do it, then move on.
	Don't spend too much time on arithmetic.
}

%For which numbers $c$ can we find a $(u, v)$ so that $541u + 34v = c$? \\
%For every such $c$, what are $u$ and $v$?

\vspace{2mm}

\textbf{Hint:}

After running the Euclidean algorithm, you have a table similar to the one shown below. \par
You can use a bit of algebra to rearrange these statements to get what you need. \par

\vspace{5mm}

\newdimen\mywidth
\setbox0=\hbox{Using the Euclidean Algorithm to find that $\gcd(20, 14) = 2$:}
\mywidth=\wd0
\begin{minipage}{\mywidth}
	\begin{center}
	Using the Euclidean Algorithm to find that $\gcd(20, 14) = 2$: \par
		$20 = 14 \times 1 + 6$ \par
		$14 = 6 \times 2 + 2$ \par
		$6 = 2 \times 3 + 0$ \par
	\end{center}
\end{minipage}\par
\vspace{2mm}
We now want to write the 2 in the last equation in terms of 20 and 14.



\begin{solution}

	Using the output of the Euclidean Algorithm, we can use substitution and a bit of algebra to solve such problems. Consider the following example:

	\begin{multicols}{2}
		\begin{minipage}{\columnwidth}
			\textit{Euclidean Algorithm:} \par
			$20 = 14 \times 1 + 6$ \par
			$14 = 6 \times 2 + 2$ \par
			$6 = 2 \times 3 + 0$ \par
		\end{minipage}

		\columnbreak

		\begin{minipage}{\columnwidth}
			\textit{Rearranged:} \par
			$6 = 20 - 14 \times 1$ \par
			$2 = 14 - 6 \times 2 = \gcd(20, 14)$ \par
		\end{minipage}
	\end{multicols}

	Using the right table, we can replace $6$ in $2 = 14 - 6 \times 2$ to get
	$2 = 14 - (20 - 14) \times 2$, \par
	which gives us $2 = \gcd(20, 14) = (3)14 + (-2)20$. \par

	\linehack{}

	$\gcd(20, 14) = 20(-2) + 14(3)$ \par
	$\gcd(541, 34) = 541(11) + 34(-175)$
\end{solution}


\vfill
\pagebreak

