\chapter{Primes and Congruences}   
\label{ch:primes}


\section{Prime Factorization}
\subsection{Prime Numbers}
We call positive whole numbers the {\em natural numbers} and denote
them by $\N$. Thus
     $$\N = \{1,2,3,4,\ldots\}.$$
We call all the whole numbers, both positive and negative, the
{\em integers}, and write
$$\Z = \{\ldots, -2, -1, 0, 1,2,\ldots\}.$$
They are denoted by $\Z$ because the German word 
for the integers is ``{\bf Z}ahlen'' (and $19$th century 
German number theorists rocked).

\begin{definition}
If $a, b\in \Z$ then ``$a$ divides $b$''
if $ac=b$ for some $c\in\Z$.
\end{definition}
To save time, we write
$$a \mid b.$$
For example, $2 \mid 6$ and $389\mid 97734562907$.
Also, everything divides $0$.

\begin{definition}
A natural number $p>1$ is a {\em prime} if~$1$ and~$p$
are the only divisors of~$p$ in $\N$.  I.e., if $a\mid p$ with
$a\in\N$ implies $a=1$ or $a=p$.  
A number $n>1$ is {\em composite} if it is not prime.
\end{definition}
Thus the primes are
$$
  2,3,5,7,11,\ldots,389,\ldots,2003,\ldots.
$$
and the composites are
$$4,6,8,9,10,12,\ldots,666=2\cdot 3^2\cdot 37, \ldots, 
2001=3\cdot 23\cdot 29,\ldots.$$

Primes are ``primal'', in the sense that 
every natural number is built out of prime numbers.
\begin{theorem}[Fundamental Theorem of Arithmetic]\label{thm:fundamental}
Every positive integer can be written as a product of primes,
and this expression is unique (up to order).
\end{theorem}
This theorem is harder to prove than 
you might first think, and we will
wait until Section~\ref{sec:proof_fundamental} below to prove it.
Why?   First, we are lucky that there are any primes at all: if
the natural numbers are replaced by the positive rational numbers then
there are no primes; e.g., $2 = \frac{1}{2}\cdot 4$, so
$\frac{1}{2}\mid 2$.
Second, we are fortunate to have unique
factorization in~$\Z$. In other ``rings'', such as 
$$
 \Z[\sqrt{-5}] = \{a + b\sqrt{-5} : a, b\in\Z\},
$$ 
unique factorization can fail.  
In $\Z[\sqrt{-5}]$, the number~$6$ factors in two different ways:
$$2\cdot 3 = 6 = (1+\sqrt{-5})(1-\sqrt{-5}).$$

\begin{remark}
Are worried about whether or not~$2$ and~$3$ should be considered
``prime'' in $\Z[\sqrt{-5}]$?
If $2 = (a+b\sqrt{-5})(c+d\sqrt{-5})$ with neither factor equal
to $\pm 1$, then taking norms implies that
$$4 = (a^2 + 5b^2)(c^2 + 5d^2),$$ 
with neither 
factor~$1$.  Theorem~\ref{thm:fundamental} implies that
$2=a^2 + 5b^2$, which is impossible.
Thus~$2$ is ``prime'' in the (nonstandard!) sense that it
has no divisors besides $\pm 1$ and $\pm 2$.   
A similar argument shows that~$3$
has no divisors besides $\pm 1$ and $\pm 3$. 
On the other hand, as you will learn
in an abstract algebra course,~$2$ should not be 
considered prime, because the ``ideal'' generated by~$2$ 
in $\Z[\sqrt{-5}]$ is not prime, in the following
sense:   we have
$(1+\sqrt{-5})(1-\sqrt{-5}) = 6\in (2)$, but
neither $1+\sqrt{-5}$ nor $1-\sqrt{-5}$ is in $(2)$.
We also note that $(1+\sqrt{-5})$ does not factor.
If $(1+\sqrt{-5}) = (a+b\sqrt{-5})(c+d\sqrt{-5})$, 
then, upon taking norms, 
$$2\cdot 3 = (a^2 + 5b^2)(c^2 + 5d^2),$$
which is impossible.

\end{remark}



\subsection{The Greatest Common Divisor}
We will use the notion of greatest common divisor 
of two integers to prove that if~$p$ is a prime
and $p\mid ab$, then $p\mid a$ or $p\mid b$.
This is the key step in our proof of Theorem~\ref{thm:fundamental}.

\begin{definition}
Let
$
  \gcd(a,b)=\max\{d : d \mid a\text{ and } d\mid b\},
$
unless both $a$ and $b$ are $0$ in which case
$\gcd(0,0)=0$.
\end{definition}
For example,
$\gcd(1,2)=1$, 
$\gcd(3,27)=3$, and
for any~$a$, 
$\gcd(0,a)=\gcd(a,0)=a$.

\subsubsection{Euclid's Algorithm for Computing GCDs}
It is initiall surprising that
we can easily compute $\gcd(2261,1275)$.
Watch closely:
 $$2261 = 1\cdot 1275 + 986.$$
Notice that if a number~$d$ divides both $2261$ and $1275$, then~$d$
automatically divides their difference $986$,
and of course~$d$ still divides $1275$.  Also, if a number
divides both $1275$ and $986$, then it has got to divide their
sum $2261$ as well!
So we have made progress:
$$\gcd(2261,1275) = \gcd(1275,986).$$
Let's try again:
$$1275 = 1\cdot 986 + 289,$$
so $\gcd(1275,986)=\gcd(986,289)$.
Just keep at it:
\begin{align*}
986&=3\cdot 289 + 119\\
289&=2\cdot 119 + 51\\
119&=2\cdot 51 + 17.
\end{align*}
Thus $\gcd(2261,1275)=\cdots=\gcd(51,17)$, which is $17$ 
because $17\mid 51$, so
$$\gcd(2261,1275)=17.$$
Aside from tedious arithmetic, that was quick and very mechanical.

\hd{The Algorithm:}
That was an illustration of {\em Euclid's algorithm}.
More formally, fix $a, b\in\N$ with $a>b$.
Using ``divide with quotient and remainder'',
write $a=bq+r$, with $0\leq r<b$.
Then, just as above,
 $$\gcd(a,b) = \gcd(b,r).$$
Let $a_1=b$, $b_1=r$, and repeat until $r=0$.
Soon enough we have computed $\gcd(a,b)$.  

Here are two more examples:
\begin{example}
Set $a=15$ and $b=6$.
\begin{eqnarray*}
  15 &=& 6\cdot 2 + 3 \qquad\gcd(15,6)=\gcd(6,3)\\
   6 &=& 3\cdot 2 + 0 \qquad\gcd(6,3) =\gcd(3,0)=3
\end{eqnarray*}
\end{example}
\noindent{}We can just as easily do an example that is ``$10$ times 
as big'':
\begin{example}
Set $a=150$ and $b=60$.
\begin{eqnarray*}
  150 &=& 60\cdot 2 + 30 \qquad\gcd(150,60)=\gcd(60,30)\\
   60 &=& 30\cdot 2 + 0 \qquad\,\,\,\gcd(60,30) =\gcd(30,0)=30
\end{eqnarray*}
\end{example}

With Euclid's algorithm in hand, we can prove that if a prime divides the
product of two numbers, then it has got to divide one of them.  This
result is the key to proving that prime factorization
is unique.

\begin{theorem}[Euclid]\label{thm:euclid}
Let~$p$ be a prime and $a, b\in \N$.
If $p\mid ab$ then $p\mid a$ or $p\mid b$.  
\end{theorem}
\begin{proof}
If $p\mid a$ we are done.  If $p\nmid a$ then $\gcd(p,a)=1$, since
only~$1$ and~$p$ divide~$p$.  Stepping through the Euclidean algorithm 
from above, we see that $\gcd(pb,ab) = b.$  At each step, we simply
multiply the equation through by~$b$.  Since $p\mid pb$ and, 
by hypothesis, $p\mid ab$, it follows that $p\mid \gcd(pb,ab) = b$.
\end{proof}






\subsection{Numbers Do Factor}
Let $n=1275$, and recall from above that $17\mid 1275$, so~$n$
is definitely composite, $n=17 \cdot 75$.  Next, $75$ is $5\cdot 15=5\cdot 5\cdot 3$.
So, finally, $1275 = 3\cdot 5\cdot 5\cdot 17$.  

Now suppose~$n$ is any positive number.   
Then, just as above,~$n$ can be written as a product of primes:
\begin{itemize}
\item If $n$ is prime, we are done.
\item If $n$ is composite, then $n=ab$ with $a,b<n$. By induction,
$a,b$ are products of primes, so~$n$ is also a product of primes.
\end{itemize}

What if we had done something differently when breaking $1275$ apart
as a product of primes?  Could the primes that show up be different?
Why not just try?  We have $1275 = 5\cdot 255$.  Now $255=5\cdot 51$ and
$51=17\cdot 3$, so everything turned out the same.  Will it always?

Incidently, there's an open problem nearby, which is closely related to the
famous ``P = NP'' problem that some consider the most important
open problem in computer science:

\hd{Unsolved Question:} {\em Is there an algorithm which can factor any
given integer~$n$ so quickly that its ``running time'' is bounded by
a polynomial function of the number of decimal digits of~$n$?}
\vspace{1ex}

(By ``algorithm'', we mean an algorithm in the sense of
classical computer science; Peter Shor \cite{shor}
has devised a polynomial time algorithm for factoring 
integers on quantum computers.)

Most mathematicians would guess that the answer is ``no'', but nobody
has yet rigorously proved that it can not be done (and told
everyone...).  If there were such an algorithm, then the cryptosystem
that we use to buy books online would be easily broken.

\subsubsection{A \$10,000 Challenge}
As of February 2002, If you factor the following 174-digit number,
affectionality known as ``RSA-576'', then the RSA company will give
you TEN THOUSAND DOLLARS!!!
\begin{quote}
1881988129206079638386972394616504398071635633794173827007
6335642298885971523466548531906060650474304531738801130339
6716199692321205734031879550656996221305168759307650257059
\end{quote}
This number is called RSA-576, since it has 576 binary digits.
See
\begin{center}
\sf http://www.rsasecurity.com/rsalabs/challenges/factoring/index.html
\end{center}
for more details.

\subsection{The Fundamental Theorem of Arithmetic}\label{sec:proof_fundamental}
We can now prove Theorem~\ref{thm:fundamental}, as follows.
Suppose we have two factorizations of~$n$. Using Theorem~\ref{thm:euclid} 
we cancel common primes from each factorization, 
one prime at a time.  At the end,
we discover that the factorizations must 
consist of exactly the same primes.  The 
technical details, with all the $p$'s and $q$'s 
are given below:

\begin{proof}
We have 
$$n = p_1\cdot p_2 \cdots p_d,$$
with each $p_i$ prime. 
Suppose that
$$n = q_1\cdot q_2 \cdots q_m$$
is another expression of~$n$ as a product of primes.
Since 
$$p_1 \mid n = q_1\cdot (q_2 \cdots q_m),$$
Euclid's theorem implies that $p_1 = q_1$ or 
$p_1 \mid q_2\cdots q_m$.  By induction, we see that $p_1 = q_i$ for some~$i$.

Now cancel $p_1$ and $q_i$, and repeat the above argument.  Eventually,
we find that, up to order, the two factorizations are the same.
\end{proof}





\section{The Sequence of Prime Numbers}
This section is concerned with the following three questions:
\begin{enumerate}
\item Are there infinitely many primes? Answer: yes
\item Are there infinitely many primes of the form $ax+b$?  Answer: yes, provided
that $\gcd(a,b)=1$.
\item How many primes are there? 
Answer: asymptotically $x/\log(x)$ primes less than~$x$.
\end{enumerate}

\subsection{There are Infinitely Many Primes}\label{sec:inf_primes}
\begin{theorem}[Euclid]\label{thm:euclid_primes}
There are infinitely many primes.
\end{theorem}
\begin{proof}
Suppose that there are only finitely many primes in~$\Z$.  
Let $p_1=2, p_2=3, \ldots, p_n$ be all of them.  
Let 
$$
    N=2\times 3\times 5 \times \cdots \times p_n + 1
$$
Then $N\neq 1$ so by Theorem~\ref{thm:fundamental}
$$N = q_1\times q_2 \times \cdots \times q_m$$
with each~$q_i$ prime and $m\geq 1$.
If $q_1\in\{2,3,5,\ldots,p_n\}$, then
$N = q_1 a + 1$ for some $a\in\Z$, so $q_1\nmid N$, a contradiction.
Thus our assumption that
$\{2,3,5,\ldots,p_n\}$
are all of the primes is false, which proves
that there must be infinitely many primes. 
\end{proof}

This theorem is not obvious.  There are completely reasonable rings
where it is false.  For example
$$
   R = \left\{\frac{a}{b} : a, b\in\Z \text{ and }\gcd(b,30)=1\right\}
$$
contains exactly three primes.
If we try the proof of Theorem~\ref{thm:euclid_primes} 
in~$R$, we run into trouble.
We would let $N=2\cdot 3\cdot 5 + 1 = 31$, which is a unit,
hence not a nontrivial product of primes.

\vspace{1ex}
\hd{Joke (Hendrik Lenstra).}  ``There are infinitely many composite
numbers.  Proof: Multiply together the first~$n$ primes and don't add~$1$.''
\vspace{1ex}\\

According to 
\begin{verbatim}
    http://www.utm.edu/research/primes/largest.html
\end{verbatim}
the largest known prime, as of September 2001, is
 $$p = 2^{6972593}-1,$$
which is a number having exactly $2098960$ decimal digits.  
Euclid's theorem implies that there definitely is a bigger prime number.
However, nobody has yet found it and proved that they are right.  
Deciding whether or not a number is prime is 
surprisingly interesting, both as a motivating problem and for applications
to cryptography, as we will see in Section~\ref{sec:prob_prime_test} and
Chapter~\ref{ch:crypto}.


\subsection{Primes of the Form $ax+b$}
Next we turn to primes of the form $ax+b$, where~$a$ and~$b$ are 
fixed integers with $a>0$ and~$x$ varies over $\N$.
We assume that $\gcd(a,b)=1$, because otherwise there is no
hope that $ax+b$ is prime infinitely often.  
For example, $2x+2$ is never prime for $x\in\N$.

\begin{proposition}
There are infinitely many primes of the form $4x-1$.
\end{proposition}
Why might this be true?   We list numbers of the form $4x-1$ and underline
those that are prime:
$$\ul{3},\, \ul{7},\, \ul{11},\, 15,\, \ul{19},\, \ul{23},\, 27,\, \ul{31},\, 35,\, 39,\,
   \ul{43},\, \ul{47},\, \ldots$$
It certainly looks plausible that underlined numbers will continue
to appear every so often.  
%The following PARI program can be used to further convince you:
%\begin{verbatim}
%   f(n, s=0) = for(x=1, n, if(isprime(4*x-1), s++); s
%\end{verbatim}

\begin{proof}
This proof is similar to the proof of Thereom~\ref{thm:euclid_primes},
but for variety we will explain it in a slightly more constructive
way, which we illustrate below in Example~\ref{ex:inf_prime_form}.

Suppose $p_1, p_2,\ldots, p_n$ are primes of the form $4x-1$.  Consider
the number
$$
      N = 4p_1\times p_2 \times \cdots \times p_n - 1.$$
Then $p_i \nmid N$ for any~$i$.  Moreover, not every prime $p\mid N$ is
of the form $4x+1$; if they all were, then $N$ would also be of the
form $4x+1$, which it is not.
Thus there is a $p\mid N$ that is of the form $4x-1$.  Since $p\not= p_i$
for any~$i$, we have found another prime of the form $4x-1$.  We can repeat
this process indefinitely, so the set of primes of the form $4x-1$ 
is infinite.
\end{proof}

\begin{example}\label{ex:inf_prime_form}
Set $p_1=3$, $p_2=7$.   Then
$$
    N = 4\times 3 \times 7 - 1 = \ul{83}
$$
is a prime of the form $4x-1$.
Next 
$$
    N = 4\times 3 \times 7\times 83 - 1 = \ul{6971},
$$
which is a again a prime of the form $4x-1$.
Again:
$$
    N = 4\times 3 \times 7\times 83\times 6971 - 1 = 48601811 = 61 \times \ul{796751}.
$$
This time $61$ is a prime, but it is of the form $4x+1 = 4\times 15+1$.
However, $796751$ is prime and $(796751-(-1))/4 = 199188$.
We are unstoppable:
$$
    N = 4\times 3 \times 7\times 83\times 6971 \times 796751 - 1 = \ul{5591}\times 6926049421.
$$
This time the small prime, $5591$, is of the form $4x-1$ and the large
one is of the form $4x+1$.
Etc!
\end{example}

\begin{theorem}[Dirichlet]\label{thm:dirichlet}
Let~$a$ and~$b$ be integers with $\gcd(a,b)=1$.
Then there are infinitely many primes of the form
$ax+b$.  
\end{theorem}
You can find a proof, which uses sophisticated tools of algebraic
number theory, in \cite[VIII.4]{frohlich_taylor}.

\subsection{How Many Primes are There?}
There are infinitely many primes, as we saw
in Section~\ref{sec:inf_primes}.  
Can we say something more precise, more satisfying? 
Consider a similar question:
\begin{question}
How many integers are even?
\end{question}
\hd{Answer:} {\em Half} of all integers.

\begin{question}
How many integers are there of the form $4x-1$?
\end{question}
\hd{Answer:} {\em One fourth} of all integers.

\begin{question}
How many integers are perfect squares?
\end{question}
\hd{Answer:} Zero percent of all positive integers, in the sense that
the limit of the proportion of perfect squares to all positive integers
converges to~$0$.
More precisely,
$$\lim_{x\ra \infty} 
   \frac{\# \{n\in\N : 
         n \leq x \text{ and $n$ is a perfect square}\}}{x} = 0,$$
since the numerator is roughly $\sqrt{x}$ and 
$\lim_{x\ra\infty}\frac{\sqrt{x}}{x} = 0$.

The following question is even more precise:
\begin{question}
How many positive integers $\leq x$ are 
perfect squares?
\end{question}
\hd{Answer:} Up to a small error, the answer is $\sqrt{x}$.

So a good question is:
\begin{question}
How many positive integers $\leq x$ are prime?
\end{question}
Let 
$$
   \pi(x) = \#\{\text{primes } p\leq x\}.
$$
For example, 
$$\pi(6) =\#\{2,3,5\} = 3.$$
Here are some more values of $\pi(x)$, that we
found using a computer:
%? pi(x, c=0) = forprime(p=2,x,c++); c; 
\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|}\hline
$x$ & 100 & 200 & 300 & 400 & 500 & 600 & 700 & 800 & 900 &1000\\\hline
$\pi(x)$ & 25&46&62&78&95&109&125&139&154&168\\\hline
\end{tabular}
\end{center}

The graph of $\pi(x)$ in Figure~\ref{fig:pix} look like a straight line.

\begin{figure}
\psset{unit=0.0045in}
\pspicture(-1,1)(1000,200)
%\psgrid[gridcolor=gray]
\psline[linewidth=0.03]{->}(-2,0)(1000,0)\rput(1010,0){$x$}
\psline[linewidth=0.03]{->}(0,-5)(0,200)\rput(0,205){$y$}
\pscircle*[linecolor=red](100,25){5}\rput(100,50){\mbox{\tiny$(100,25)$}}
\pscircle*[linecolor=red](200,46){5}\rput(200,70){\mbox{\tiny$(200,46)$}}
%(300,62)
%(400,78)
%(500,95)
%(600,109)
%(700,125)
%(800,139)
%(900,154)
\pscircle*[linecolor=red](900,154){5}\rput(900,175){\mbox{\tiny$(900,154)$}}
\pscircle*[linecolor=red](1000,168){5}\rput(1000,180){\mbox{\tiny$(1000,168)$}}
\rput(200,150){Graph of $\pi(x)$}

\psline[linecolor=blue]
(10,4)
(20,8)
(30,10)
(40,12)
(50,15)
(60,17)
(70,19)
(80,22)
(90,24)
(100,25)
(110,29)
(120,30)
(130,31)
(140,34)
(150,35)
(160,37)
(170,39)
(180,41)
(190,42)
(200,46)
(210,46)
(220,47)
(230,50)
(240,52)
(250,53)
(260,55)
(270,57)
(280,59)
(290,61)
(300,62)
(310,63)
(320,66)
(330,66)
(340,68)
(350,70)
(360,72)
(370,73)
(380,75)
(390,77)
(400,78)
(410,80)
(420,81)
(430,82)
(440,85)
(450,87)
(460,88)
(470,91)
(480,92)
(490,93)
(500,95)
(510,97)
(520,97)
(530,99)
(540,99)
(550,101)
(560,102)
(570,104)
(580,106)
(590,107)
(600,109)
(610,111)
(620,114)
(630,114)
(640,115)
(650,118)
(660,120)
(670,121)
(680,123)
(690,124)
(700,125)
(710,127)
(720,128)
(730,129)
(740,131)
(750,132)
(760,134)
(770,136)
(780,137)
(790,138)
(800,139)
(810,140)
(820,141)
(830,145)
(840,146)
(850,146)
(860,149)
(870,150)
(880,151)
(890,154)
(900,154)
(910,155)
(920,157)
(930,158)
(940,159)
(950,161)
(960,162)
(970,163)
(980,165)
(990,166)
(1000,168)
\endpspicture
\caption{Graph of $\pi(x)$ for $x<1000$}\label{fig:pix}
\end{figure}

Gauss spent some of his free time counting primes.  By the end
of his life, he had computed $\pi(x)$ for $x$ up to $3$ million.
$$\pi(3000000)=216816.$$
(I don't know if Gauss got the right answer.)
Gauss conjectured the following:

\begin{theorem}[Hadamard, Vall\'ee Poussin, 1896]
$\pi(x)$ is asymptotic to $x/\log(x)$, in the sense that
   $$\lim_{x\ra \infty}  \frac{\pi(x)}{ x/\log(x)} = 1.$$
\end{theorem}
I will not prove this theorem in this class.
The theorem implies that $x/(\log(x)-a)$ can be used
to approximate $\pi(x)$, for any~$a$.  In fact, $a=1$
is the best choice.
\begin{verbatim}
? pi(x, c=0) = forprime(p=2,x,c++); c; 
? for(n=1,10,print(n*1000,"\t",pi(n*1000),"\t",n*1000/(log(n*1000)-1)))
1000	168	169.2690290604408165186256278
2000	303	302.9888734545463878029800994
3000	430	428.1819317975237043747385740
4000	550	548.3922097278253264133400985
5000	669	665.1418784486502172369455815
6000	783	779.2698885854778626863677374
7000	900	891.3035657223339974352567759
8000	1007	1001.602962794770080754784281
9000	1117	1110.428422963188172310675011
10000	1229	1217.976301461550279200775705
\end{verbatim}
\begin{remark}
\end{remark}

\subsubsection{Counting Primes Today}
People all over the world are counting primes, probably even as we speak.
See, e.g., 
\begin{verbatim}
http://www.utm.edu/research/primes/howmany.shtml

http://numbers.computation.free.fr/Constants/Primes/Pix/pixproject.html
\end{verbatim}

A huge computation:
$$
  \pi(10^{22}) = 201467286689315906290
$$
(I don't know for sure if this is right...)

\subsubsection{The Riemann Hypothesis}
The function
$$
  \Li(x) = \int_{2}^{x} \frac{1}{\log(x)} dx.
$$
is also a good approximation to $\pi(x)$. 

The famous {\bf Riemann Hypothesis} is equivalent to the
assertion that
$$
 \pi(x) = \Li(x) +O(\sqrt{x}\log(x)).
$$
(This is another \$1000000 prize problem.)

\begin{verbatim}
      pi(10^22)     = 201467286689315906290
      Li(10^22)     = 201467286691248261498.1505...    (using Maple)
      Log(x)/(x-1)  = 201381995844659893517.7648...    (pari)
\end{verbatim}


\section{Congruences}



\hd{The point of this lecture:}\\
Define the ring $\Z/n\Z$ of integers modulo~$n$.
Prove Fermat's little theorem, which asserts
that if $\gcd(x,n)=1$, then $x^{\vphi(n)} \con 1\pmod{n}$.

\subsection{Notation}
\begin{definition}[Congruence]
Let $a,b\in\Z$ and $n\in\N$.  Then 
$$
      a\con b\pmod{n}
$$
if $n\mid a-b$.
\end{definition}
That is, there is $c\in\Z$ such that
$$
  nc = a-b.
$$
One way I think about it: $a$ is congruent to $b$ modulo $n$, if 
we can get from $b$ to $a$ by adding multiples of~$n$.

Congruence modulo~$n$ is an {\em equivalence relation}.
Let 
$$
  \Z/n\Z = \{\text{ the set of equivalence classes }\}
$$
The set $\Z/n\Z$ is a {\em ring}, the ``ring of integers
modulo $n$''.  It is the quotient of the ring $\Z$ by 
the ideal generated by~$n$. 

\begin{example}
$$
  \Z/3\Z = \{ \{\ldots, -3, 0, 3, \ldots\}, 
              \{\ldots, -2, 1, 4, \ldots\},
              \{\ldots, -1, 2, 5, \ldots\}\}
         = \{[0], [1], [2]\}
$$
where we let $[a]$ denote the equivalence class of~$a$.
\end{example}


\subsection{Arithmetic Modulo~$N$}
Suppose $a,\,a',b\,b'\in\Z$ and
$$
 a\con a'\pmod{n}, \qquad b\con b'\pmod{n}.
$$
Then 
\begin{align}
  a + b &\con a' + b'\pmod{n}\\
  a\times b &\con a'\times b'\pmod{n}
\end{align}
So it makes sense to define $+$ and $\times$ by 
$[a]+[b]=[a+b]$ and $[a]\times[b]=[a\times b]$.

\subsubsection{Cancellation}
\begin{proposition}\label{prop:cancel}
If $\gcd(c,n)=1$ and 
$$
   ac\con bc\pmod{n}
$$
then $a \con b\pmod{n}$.
\end{proposition}
\begin{proof}
By definition
$$
  n \mid ac - bc = (a-b)c.
$$
Since $\gcd(n,c)=1$, it follows that $n\mid a-b$, so
$$
   a \con b\pmod{n},
$$
as claimed.
\end{proof}

\subsubsection{Rules for Divisibility}
\begin{proposition}
A number $n\in\Z$ is divisible by $3$ if and only if
the sum of the digits of~$n$ is divisible by~$3$.
\end{proposition}
\begin{proof}
Write 
 $$n=a+10b+100c+\cdots.$$
Since $10\con 1\pmod{3}$,
$$
  n = a + 10b + 100c+\cdots \con a + b + c+\cdots \pmod{3},
$$
from which the proposition follows.
\end{proof}

Similarly, you can find rules for divisibility by~$5$, $9$ and $11$.
What about divisibility by~$7$?


\subsection{Linear Congruences}
\begin{definition}[Complete Set of Residues]
A {\em complete set of residues} modulo~$n$ is a subset $R\subset\Z$
of size~$n$ whose reductions modulo~$n$ are distinct.  In other words,
a complete set of residues is a choice of representive for each
equivalence class in $\Z/n\Z$.
\end{definition}
Some examples:
$$
   R=\{0,1,2,\ldots,n-1\}
$$
is a complete set of residues modulo~$n$.
When $n=5$, a complete set of residues is
$$
  R = \{0,1,-1,2,-2\}.
$$

\begin{lemma}\label{lem:residues}
If~$R$ is a complete set of residues modulo~$n$ and $a\in\Z$ with
$\gcd(a,n)=1$, then $aR = \{ax : x \in R\}$
is also a complete set of residues.
\end{lemma}
\begin{proof}
If $ax\con ax'\pmod{n}$ with $x, x'\in R$, then 
Proposition~\ref{prop:cancel} implies that $x\con{}x'\pmod{n}$.
Because $R$ is a complete set of residues, this implies
that $x=x'$.  Thus the elements of
$aR$ have distinct reductions modulo~$n$.
It follows, since $\#aR=n$, that $aR$ is a 
complete set of residues modulo~$n$.
\end{proof}


\begin{definition}[Linear Congruence]
A {\em linear congruence} is an equation of the form
$$
 ax\con b\pmod{n}.
$$
\end{definition}
\begin{proposition}
If $\gcd(a,n)=1$, then the equation
$$
   ax\con b\pmod{n}
$$
must have a solution.
\end{proposition}
\begin{proof}
Let $R$ be a complete set of residues modulo~$n$ (for
example, $R=\{0,1,\ldots,n-1\}$).
Then by Lemma~\ref{lem:residues}, 
$aR$ is also a complete set of residues.
Thus there is an element $ax\in aR$ such
that $ax\con b\pmod{n}$, which proves
the proposition.
\end{proof}
The point in the proof is that left multiplication by $a$ defines
a map $\Z/n\Z\hookrightarrow \Z/n\Z$, which must be surjective
because $\Z/n\Z$ is finite.

\hd{Illustration:}\\
$$
  2x\con 3\pmod{7}
$$
Set $R = \{0,1,2,3,4,5,6\}$.
Then 
$$
  2R = \{0,2,4,6,8\con 1, 10\con 3, 12\con 5\},
$$
so $2\cdot 5\con 3\pmod{7}$.

\hd{Warning:}\\
Note that the equation $ax\con b\pmod{n}$
might have a solution even if $\gcd(a,n)\neq 1$.  To construct
such examples, let~$a$ be any divisor of~$n$,~$x$ 
any number, and set $b=ax$.
For example, $2x\con 6\pmod{8}$ has a solution!


\subsection{Fermat's Little Theorem}

\begin{definition}[Order]
Let $n\in\N$ and $x\in\Z$ with $\gcd(x,n)=1$.
The {\em order} of $x$ modulo~$n$ is the smallest $m\in\N$
such that 
$$
  x^m \con 1\pmod{n}.
$$
\end{definition}
We must show that this definition makes sense.  To do so, we verify
that such an~$m$ exists.  Consider $x, x^2, x^3, \ldots$ modulo~$n$.
There are only finitely many residue classes modulo~$n$, so we must
eventually find two integers $i, j$ with $i<j$ such that
$$
  x^i\con x^j\pmod{n}.
$$
Since $\gcd(x,n)=1$, Proposition~\ref{prop:cancel} implies that 
we can cancel~$x$'s and conclude that
$$
  x^{j-i}\con 1\pmod{n}.
$$


\begin{definition}[Euler Phi function]
Let 
$$
 \vphi(n) = \#\{a \in \N : a \leq n \text{ and } \gcd(a,n)=1\}.
$$
\end{definition}
For example,
\begin{align*}
 \vphi(1) &= \#\{1\} = 1,\\
 \vphi(5) &= \#\{1,2,3,4\} = 4,\\
 \vphi(12) &= \#\{1,5,7,11\} = 4.\\
\end{align*}
If~$p$ is any prime number then
$$
   \vphi(p) = \#\{1,2,\ldots,p-1\} = p-1.
$$

\begin{theorem}[Fermat's Little Theorem]\label{thm:fermatlittle}
If $\gcd(x,n)=1$, then
$$
   x^{\vphi(n)} \con 1\pmod{n}.
$$
\end{theorem}
\begin{proof}
Let 
$$
  P = \{ a : 1\leq a \leq n \text{ and } \gcd(a,n) = 1\}.
$$
In the same way that we proved Lemma~\ref{lem:residues},
we see that the reductions modulo~$n$ of the elements of $xP$ 
are exactly the same as the reductions of the elements of $P$.
Thus
$$
 \prod_{a\in P} (xa) = \prod_{a \in P} a \pmod{n},
$$
since the products are over exactly 
the same numbers modulo~$n$.
Now cancel the $a$'s on both sides to get
$$x^{\#P} \con 1\pmod{n},$$
as claimed.
\end{proof}

\subsubsection{Group-theoretic Interpretation}
The set of invertible elements of $\Z/n\Z$ is a group
$$
  (\Z/n\Z)^*
    = \{ [a] \in \Z/n\Z : \gcd(a,n) = 1\}.
$$
This group has order~$\vphi(n)$.
Theorem~\ref{thm:fermatlittle} asserts that the
order of an element of $(\Z/n\Z)^\star$ divides the order $\vphi(n)$
of $(\Z/n\Z)^\star$.  This is a special case of the
more general theorem that if~$G$ is a finite group and $g\in G$,
then the order of~$g$ divide $\#G$.

\subsection{What happened?}
Take out a piece of paper and answer the following two questions:
\begin{enumerate}
\item What is a central idea that you learned in this lecture?
\item What part of this lecture did you find murky?
\end{enumerate}





\section{Congruences, Part II}


\hd{Key Ideas Today}
\begin{itemize}
\item Wilson's theorem
\item Chinese Remainder Theorem
\item Multiplicativity of $\vphi$
\end{itemize}

\subsection{Wilson's Theorem}
\begin{theorem}[John Wilson's theorem, from the 1770s]
An integer $p>1$ is prime if and only if
$$(p-1)! \con -1 \pmod{p}.$$
\end{theorem}
\begin{example}
\begin{verbatim}

? p=3
%1 = 3
? (p-1)! % 3
%2 = 2
? p=17
%3 = 17
? (p-1)!
%4 = 20922789888000
? (p-1)! % p
%5 = 16
\end{verbatim}
\end{example}

\begin{proof}
We first {\bf assume that~$p$ is prime} and prove that 
$(p-1)! \con -1\pmod{p}$.  If $a\in\{1,2,\ldots,p-1\}$ then
the equation 
$$
  ax\con 1\pmod{p}
$$
has a unique solution $a'\in\{1,2,\ldots,p-1\}$.
If $a=a'$, then $a^2\con 1\pmod{p}$, so 
$p\mid a^2-1 = (a-1)(a+1)$, so 
$p\mid (a-1)$ or $p\mid (a+1)$, so $a\in\{1,-1\}$.  
We can thus pair off the elements of 
$\{2,3,\ldots,p-2\}$,
each with its inverse. 
Thus
$$
  2\cdot 3 \cdot \cdots \cdot (p-2) \con 1\pmod{p}.
$$
Multiplying both sides by $p-1$ proves that
$(p-1)! \con -1\pmod{p}$.


Next we {\bf assume that $(p-1)! \con -1\pmod{p}$} and
prove that~$p$ must be prime.  Suppose not, so that~$p$ 
is a composite number $\geq 4$.  Let $\ell$ be a prime divisor
of~$p$.  Then $\ell<p$, so $\ell\mid (p-1)!$.  Also,
$$
  \ell \mid p \mid ((p-1)! - 1).
$$
This is a contradiction, because a prime can't divide a number $a$ and
also divide $a-1$, since it would then have to divide $a-(a-1)=1$.
\end{proof}

\begin{example}
When $p=17$, we have
$$
2\cdot 3\cdot \cdots \cdot 15
  = (2\cdot 9)\cdot(3\cdot 6)\cdot(4\cdot 13)\cdot
    (5\cdot 7)\cdot(8\cdot 15)\cdot(10\cdot 12)\cdot(14\cdot 11)
  \con 1\pmod{17},$$
where we have paired up the numbers $a, b$ for 
which $ab\con 1\pmod{17}$.
\end{example}

Let's test Wilson's Theorem in PARI:
\begin{verbatim}
? wilson(n) = Mod((n-1)!,n) == Mod(-1,n)   
? wilson(5)
%9 = 1
? wilson(10)
%10 = 0
? wilson(389)
%11 = 1
? wilson(2001)
%12 = 0
\end{verbatim}

\hd{Warning:} In practice, this is a horribly inefficient way to check
whether or not a number is prime.

\subsection{The Chinese Remainder Theorem}
Sun Tsu Suan-Ching (4th century AD):
\begin{quote}
There are certain things whose number is unknown. Repeatedly divided
by 3, the remainder is 2; by 5 the remainder is 3; and by 7 the
remainder is 2. What will be the number?
\end{quote}
In modern notation, Sun is asking us to solve the following system of
equations:
\begin{align*}
x &\con 2 \pmod{3}\\
x &\con 3 \pmod{5}\\
x &\con 2 \pmod{7}
\end{align*}
The Chinese Remainder Theorem asserts that a solution to Sun's
question exists, and the proof gives a method to find a solution.

\begin{theorem}[The Chinese Remainder Theorem]
Let $a, b\in\Z$ and $n,m\in\N$ such that
$\gcd(n,m)=1$.  Then there exists $x\in\Z$ such that
\begin{align*}
x&\con a\pmod{m}\\
x&\con b\pmod{n}
\end{align*}
\end{theorem}
\begin{proof}
The equation
$$
  tm \con b-a\pmod{n}
$$
has a solution $t$ since $\gcd(m,n)=1$.
Set $x=a+tm$.  We next verify that $x$ is a solution to the two equations.  
Then
$$
 x \con a + (b-a) \con b \pmod{n},
$$
and 
$$
 x = a+tm \con a\pmod{m}.
$$
\end{proof}

Now we can solve Sun's problem:
\begin{align*}
x &\con 2 \pmod{3}\\
x &\con 3 \pmod{5}\\
x &\con 2 \pmod{7}.
\end{align*}
First, we use the theorem to find a solution to the pair
of equations 
\begin{align*}
x &\con 2 \pmod{3}\\
x &\con 3 \pmod{5}.
\end{align*}
Set $a=2$, $b=3$, $m=3$, $n=5$.
Step 1 is to find a solution to $t\cdot 3 \con 3-2\pmod{5}$.
A solution is $t=2$.  Then $x=a+tm=2+2\cdot 3 = 8$.
Since any $x'$ with $x'\con x\pmod{15}$ is also a solution to
those two equations, we can solve all three equations by
finding a solution to the pair of equations
\begin{align*}
x &\con 8 \pmod{15}\\
x &\con 2 \pmod{7}.
\end{align*}
Again, we find a solution to $t\cdot 15 \con 2-8\pmod{7}$.
A solution is $t = 1$, so 
$$x=a+tm=8+15=23.$$
Note that there are other solutions.  Any $x'\con x\pmod{3\cdot 5\cdot 7}$
is also a solution; e.g., $23+3\cdot 5\cdot 7 = 128$.

We can also solve Sun's problem in PARI:
\begin{verbatim}
? chinese(Mod(2,3),Mod(3,5))
%13 = Mod(8, 15)
? chinese(Mod(8,15),Mod(2,7))
%14 = Mod(23, 105)
\end{verbatim}


\subsection{Multiplicative Functions}
\begin{definition}
A function $f:\N\ra \Z$ is 
{\em multiplicative} if, whenever $m, n\in\N$ and $\gcd(m,n)=1$, we have
$$
   f(mn) = f(m)\cdot f(n).
$$
\end{definition}
Recall that the {\em Euler $\vphi$-function} is
$$
  \vphi(n) = \#\{a : 1\leq a \leq n\text{ and }\gcd(a,n)=1\}.
$$
\begin{proposition}
$\vphi$ is a multiplicative function.
\end{proposition}
\begin{proof}
Suppose that $m,n\in\N$ and $\gcd(m,n)=1$.
Consider the map
\begin{align*}\{c : & \,1 \leq c \leq mn \text{ and }\gcd(c,mn)=1\}
  \xrightarrow{\quad f\quad }\\
  &\{a : 1 \leq a \leq m \text{ and }\gcd(a,m)=1\}\cross
  \{b : 1 \leq b \leq n \text{ and }\gcd(b,n)=1\}\}
\end{align*}
defined by 
$$
 f(c) = (c\text{ mod } m, \quad{}c \text{ mod }n).
$$

\hd{The map~$f$ is injective:}  If $f(c)=f(c')$, then
$m\mid c-c'$ and $n\mid c-c'$, so, since $\gcd(n,m)=1$,
$nm\mid c-c'$, so $c=c'$.

\hd{The map~$f$ is surjective:} Given $a, b$ with $\gcd(a,m)=1$,
$\gcd(b,n)=1$, the Chinese Remainder Theorem implies that there
exists $c$ with $c\con a\pmod{m}$ and $c\con b\pmod{n}$.  We
may assume that $1\leq c\leq nm$, ans since $\gcd(a,m)=1$ and
$\gcd(b,n)=1$, we must have $\gcd(c,nm)=1$. Thus $f(c)=(a,b)$.

Because $f$ is a bijection, the set on the left has the same
size as the product set on the right.  Thus
$$
  \vphi(mn) = \vphi(m)\cdot \vphi(n).
$$
\end{proof}


\begin{example}
The proposition makes it easier to compute $\vphi(n)$. 
For example,
$$
   \vphi(12) = \vphi(2^2)\cdot \vphi(3) = 2\cdot 2 = 4.
$$
Also, for $n\geq 1$, we have
$$
   \vphi(p^n) = p^n - \frac{p^n}{p},
$$  
since $\vphi(p^n)$ is the number of numbers less than $p^n$
minus the number of those that are divisible by $p$.
Thus, e.g.,
$$
   \vphi(389\cdot 11^2) = 388 \cdot (11^2 - 11) = 388\cdot 110 = 42680.
$$
The $\vphi$ function is also available in PARI:
\begin{verbatim}
  ? eulerphi(389*11^2)
  %15 = 42680
\end{verbatim}
\end{example}

\begin{question}
Is computing $\vphi(\text{1000 digit number})$ really easy or really hard?
\end{question}





\section{Congruences, Part III}



\hd{Key Ideas}
\begin{enumerate}
\item How to solve $ax\con 1\pmod{n}$ efficiently.
\item How to compute $a^m\pmod{n}$ efficiently.
\item A probabilistic primality test.
\end{enumerate}

\subsection{How to Solve $ax\con 1\pmod{n}$}
Let $a, n\in\N$ with $\gcd(a,n)=1$.  Then
we know that $ax\con 1\pmod{n}$ has a solution.  How can we find~$x$?

\subsubsection{More About GCDs}
\begin{proposition}
Suppose $a,b\in\Z$ and $\gcd(a,b)=d$. Then
there exists $x,y\in\Z$ such that 
$$
   ax + by = d.
$$
\end{proposition}
I won't give a formal proof of this proposition, though there
are many in the literature.  Instead I will show you how to
find~$x$ and~$y$ in practice, because that's what you will
need to do in order to solve equations like
$ax\con 1\pmod{n}$.
\begin{example}
Let $a=5$ and $b=7$.
The steps of the Euclidean $\gcd$ algorithm are:
\begin{align*}
 \ul{7}&=1\cdot \ul{5} + \underline{2} & \text{so } \ul{2} &= \ul{7} - \ul{5}\hfill\\
 \ul{5}&=2\cdot \ul{2} + \ul{1}    & \text{so }     \ul{1}&= \ul{5} - 2\cdot \ul{2} = 3\cdot\ul{5}-2\cdot\ul{7}\hfill
\end{align*}
On the right, we have written each partial remainder as a 
linear combination of~$a$ and~$b$.  In the last step, we write
$\gcd(a,b)$ as a linear combination of~$a$ and~$b$, as desired.
\end{example}

That example wasn't too complicated, next we try a much longer example.
\begin{example}
Let $a=130$ and $b=61$.  We have
\begin{align*}
\ul{130} &= 2\cdot \ul{61} + \ul{8} & \text{so } \ul{8} &= \ul{130}-2\cdot\ul{61}\\
 \ul{61} &= 7\cdot \ul{8} + \ul{5}   & \text{so } \ul{5} &= -7\cdot\ul{130}+15\cdot\ul{61}\\
  \ul{8} &= 1\cdot \ul{5} + \ul{3}    & \text{so } \ul{3} &= 8\cdot\ul{130}-17\cdot\ul{61}\\
  \ul{5} &= 1\cdot \ul{3} + \ul{2}    & \text{so } \ul{2} &=-15\cdot\ul{130}+32\cdot\ul{61}\\
  \ul{3} &= 1\cdot \ul{2} + \ul{1}& \text{so } \ul{1} &= 23\cdot\ul{130}-49\cdot\ul{61}
\end{align*}
Thus $x=130$ and $y=-49$.
\end{example}

\begin{remark}
For our present purposes it will always be sufficient to find one
solution to $ax+by=d$.  In fact, there are always 
infinitely many solutions.  If $x, y$ is a solution to 
$$
  ax + by = d,
$$ 
then for any $\alpha\in\Z$, 
$$
 a\left(x+\alpha\cdot\frac{b}{d}\right) + b\left(y - \alpha\cdot\frac{a}{d}\right) = d,
$$
is also a solution, and all
solutions are of the above form for some~$\alpha$.
\end{remark}

It is also possible to compute~$x$ and~$y$ using PARI.
\begin{verbatim}
? ?bezout
bezout(x,y): gives a 3-dimensional row vector [u,v,d] such that 
             d=gcd(x,y) and u*x+v*y=d.
? bezout(130,61)
%1 = [23, -49, 1]
\end{verbatim}


\subsubsection{To solve $ax\con 1\pmod{n}$}
Suppose $\gcd(a,n)=1$.  To solve
$$
 ax\con 1\pmod{n},
$$ 
find $x$ and $y$ such that 
$ax+ny = 1$.  Then
$$
  ax \con ax + ny \con 1 \pmod{n}.
$$

\begin{example}
Solve $17x \con 1\pmod{61}$.
First, we use the Euclidean algorithm to find $x, y$ such that
$17x+61y=1$:
\begin{align*}
\ul{61}&=3\cdot\ul{17}+\ul{10} & \text{so }\ul{10} &=\ul{61} - 3\cdot\ul{17}\\
\ul{17}&=1\cdot\ul{10}+\ul{7}  & \text{so }\ul{7}  &=-\ul{61} + 4\cdot\ul{17}\\
\ul{10}&=1\cdot \ul{7}+\ul{3}  & \text{so }\ul{3}  &=2\cdot\ul{61} - 7\cdot\ul{17}\\
\ul{3} &=2\cdot\ul{3} +\ul{1}  & \text{so }\ul{1}  &=-5\cdot\ul{61}+18\cdot\ul{17}
\end{align*}
Thus $x=18$ is a solution to $17x \con 1\pmod{61}$.
\end{example}

\subsection{How to Compute $a^m\pmod{n}$ Efficiently}
As we will see on Friday, a quick method to compute
$a^m\pmod{n}$ is absolutely {\em essential} to 
public-key cryptography.

\hd{Naive Algorithm:} Compute $a\cdot a \cdot \cdots \cdot a\pmod{n}$ by
repeatedly multiplying by $a$ and reducing modulo~$m$.  This is {\em BAD}
because it takes $m-1$ multiplications.

\hd{Clever Algorithm:} The following observation is the key idea which 
makes the clever algorithm work.  Write $m=\sum_{i=1}^r \eps_i 2^i$ with
each $\eps_i\in\{0,1\}$, i.e., write~$m$ in base~$2$ (binary).
Then 
$$
   a^m  = \prod_{\eps_i = 1} a^{2^i}\pmod{n}.
$$
It is straightforward to write a number~$m$ in binary,
as follows:
{\sf If $m$ is odd, then $\eps_0=1$, otherwise $\eps_0=0$.
Replace $m$ by $\text{floor}({\frac{m}{2}})$.  If the new $m$ is
odd then $\eps_1=1$, otherwise $\eps_1=0$.  Keep repeating
until $m=0$.}
\begin{example}\mbox{}\\
\hd{Problem:} {\em Compute the last $2$ digits of $6^{91}$.} \\
\hd{Solution:} We compute $6^{91}\pmod{100}$.
\begin{center}
\begin{tabular}{|cccc|}\hline
\quad$i$\quad\quad  & \quad $m$\quad\quad  
     & \quad $\eps_i$\quad \quad  & \quad $6^{2^i}$ \text{mod} 100\\\hline
0   & 91 & 1 &  6 \\\hline
1   & 45 & 1  & 36\\\hline
2   & 22 & 0 &  96\\\hline
3   & 11 & 1 &  16\\\hline
4   & 5 & 1 &   56\\\hline
5   & 2 & 0 &   36\\\hline
6   & 1 & 1 &   96\\\hline
\end{tabular}
\end{center}
As a check, note that $91 = 1011011_2 = 2^6+2^4+2^3+2+2^0$.
Finally, we have
$$6^{91} = 6^{2^6}\cdot 6^{2^4} \cdot 6^{2^3}\cdot 6^2 \cdot 6
         \con 96 \cdot 56 \cdot 16 \cdot 36 \cdot 6
         \con 56\pmod{100}.$$

\hd{Summary of above table:}  The first column,
labeled $i$, 
is just to keep track of $i$.  The second column, labeled $m$, 
is got by dividing
the entry above it by~$2$ and taking the integer part of the result.
The third column, labeled $\eps_i$, 
simply records whether or not the second column is
odd.  The forth column is computed by squaring, modulo 100, the entry above
it.
\end{example}

Some examples in PARI to convince you that powering isn't
too difficult:
\begin{verbatim}
? Mod(17,389)^5000
%13 = Mod(330, 389)
? Mod(2903,49084098)^498494
%14 = Mod(13189243, 49084098)
\end{verbatim}
These both take no noticeable time.

\subsection{A Probabilistic Primality Test}\label{sec:prob_prime_test}
Recall,
\begin{theorem}
A natural number~$p$ is prime if and only if
for {\em every} $a\not\con 0\pmod{p}$,
$$
      a^{p-1}\con 1\pmod{p}.
$$
\end{theorem}

Thus if $p\in\N$ and, e.g., $2^{p-1}\not\con 1\pmod{p}$, then
we have proved that~$p$ is {\em not} prime.  If, however,
$a^{p-1}\con 1\pmod{p}$ for a couple of~$a$, then it is ``highly likely''
that~$p$ is prime.  I will not analyze this probability here, but
we might later in this course. 

\begin{example}
Let $p=323$.  Is~$p$ prime?
Let's compute 
$2^{322}$ modulo~$323$.  Making a table as above, we have
\begin{center}
\begin{tabular}{|cccc|}\hline
\quad$i$\quad\quad  & \quad $m$\quad\quad  
     & \quad $\eps_i$\quad \quad  & \quad $2^{2^i}$ \text{mod} 323\\\hline
0   & 322 & 0 &  2 \\\hline
1   & 161 & 1 &  4 \\\hline
2   & 80  & 0 & 16 \\\hline
3   & 40  & 0 & 256 \\\hline
4   & 20  & 0 & 290 \\\hline
5   & 10  & 0 & 120 \\\hline
6   & 5   & 1 & 188 \\\hline
7   & 2   & 0 & 137 \\\hline
8   & 1   & 1 & 35  \\\hline
\end{tabular}
\end{center}
Thus 
  $$2^{322} \con 4\cdot 188\cdot 35 \con 157\pmod{323},$$
so $323$ is not prime.  In fact, $323 = 17\cdot 19$.
\end{example}

It's possible to prove that a large number is composite, but yet
be unable to (easily) find a factorization!
For example if
$$n = 95468093486093450983409583409850934850938459083,$$
then $2^{n-1}\not\con 1\pmod{n}$, so $n$ is composite.
This is something one could verify in a reasonable amount
of time by hand.  (Though finding a factorization by hand 
would be very difficult!)

\subsubsection{Finding large numbers that are probably prime}
\begin{verbatim}
? probprime(n, a=2) = Mod(a,n)^(n-1) == Mod(1,n)
? x = 0948609348698406983409580934859034509834095809348509834905809345
%36 = 948609348698406983409580934859034509834095809348509834905809345
? for(i=0,100,if(probprime(x+2*i,2),print(i)))
27
? p = x + 2*27
%37 = 948609348698406983409580934859034509834095809348509834905809399
? probprime(p,3)
%39 = 1
\end{verbatim}


\begin{exercises}
\item \label{ex:binomdiv}
Let~$p$ be a prime number and~$r$ and integer such that 
$1\leq r < p$. Prove that~$p$ divides the binomial
coefficient 
$$
   \frac{p!}{r!(p-r)!}.
$$
You may not assume that this coefficient is a integer.

\item\label{ex:gcds} Compute the following gcd's using a pencil and 
the Euclidean algorithm:
$$
  \gcd(15,35),\quad
  \gcd(247,299),\quad
  \gcd(51,897), \quad 
  \gcd(136,304)
 $$

\item\label{ex:sumn} 
  \begin{enumerate}
   \item Using mathematical induction to prove that
    $$1+2+3 + \cdots + n = \frac{n(n+1)}{2}.$$
   \item Find a formula for 
    $$1 - 2 + 3 - 4 + \cdots \pm n = \sum_{a=1}^{n}(-1)^{a-1} a.$$
  \end{enumerate}

\item\label{ex:primeyear} What was the most recent prime year?
I.e., which of $\ldots, 2002, 2001, 2000, \ldots$ was it?

\item\label{ex:gcdrep} 
Use the Euclidean algorithm to find integers $x,\, y\in\Z$
such that
$$2261x + 1275y = 17.$$

\item\label{ex:polrepconj}
 Let $f(x)\in\Z[x]$ be a polynomial with integer coefficients.
Formulate a conjecture about when the set
$\{ f(a) : a \in \Z \text{ and $f(a)$ is prime }\}$
is infinite.  Give computational evidence for your conjecture.

\item\label{ex:howhard} Is it easy or hard for a computer 
to compute the gcd of two random 2000-digit numbers?

\item\label{ex:primesform} Prove that there are infinitely 
many primes of the form $6x-1$.

\item\label{ex:pnt}
 \begin{enumerate}
\item Let $y$ be the current year (e.g., $2002$).
Use a computer to compute
$$\pi(y) = \#\{ \text{ primes } p \leq y\}.$$

\item The prime number theorem predicts that $\pi(x)$ is
asymptotic to $x/\log(x)$.  How close is $\pi(2001)$ to 
$2001/\log(2001)$?
\end{enumerate}

\item\label{ex:residues} 
Find complete sets of residues modulo $7$, all of whose elements
are (a) nonnegative, (b) odd, (c) even, (d) prime.

\item\label{ex:invmod} Find an integer $x$ such that $37x \con 1\pmod{101}$.

\item\label{ex:ordmod} What is the order of $5$ modulo $37$?

\item\label{ex:facphi} 
Let $n=\vphi(7!)$.  Compute the prime factorization of~$n$.


\item\label{ex:crt} Find an $x\in\Z$ such that
$x \con -4 \pmod{17}$ and $x\con 3\pmod{23}$.

\item\label{ex:pow} Compute 
$7^{100}\pmod{389}$.

\item\label{ex:pow2} Find a number~$a$ such that $0\leq a < 111$ and
$$
  (102^{70}+1)^{35} \con a\pmod{111}.
$$ 
%Problem 2.05 on page 217 of Davenport.


\item\label{ex:wilson2} Prove that if $n>4$ is composite then 
$$
   (n-1)! \con 0 \pmod{n}.
$$

\item\label{ex:phiodd} For what values of~$n$ is $\vphi(n)$ odd?

\comment{\item\label{ex:thieves} 
Seven thieves try to share a hoard of gold bars equally between
themselves.  Unfortunately, six bars are left over, and in the fight
over them, one thief is killed.  The remaining six thieves, still
unable to share the bars equally since two are left over, again fight,
and another is killed.  When the remaining five share the bars, one
bar is left over, and it is only after yet another thief is killed
that an equal sharing is possible.  What is the minimum number of bars
which allows this to happen?

\item\label{ex:woman}
 An elderly woman goes to a market where a horse tramples her basket
 crushing her eggs. The horse's honest rider offers to pay for the
 damages and asks her how many eggs she had brought. She doesn't
 remember the exact number, but recalls that when she had taken them
 out two at a time, there was one egg left. The same happened when she
 picked them out three, four, five, and six at a time, but when she
 took them out seven at a time two were left. What is the smallest
 number of eggs she could have had?
}

\item\label{ex:div11} 
Prove that a positive number~$n$ is divisible by~$11$ if and only
if the alternating sum of the digits of~$n$ is divisible by~$11$.

\item\label{ex:phimult} Let $\vphi:\N\ra\N$ be the Euler~$\vphi$ function.  
\begin{enumerate}
   \item Find all natural numbers~$n$ such that $\vphi(n)=1$.
   \item Do there exist natural numbers~$m$ and~$n$ such that
     $\vphi(mn)\neq \vphi(m)\cdot \vphi(n)$?
      \end{enumerate}


\end{exercises}




