\documentclass[11pt]{article}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{latexsym}
\usepackage{verbatim}
\usepackage[pdftex]{graphicx}
\usepackage[all,arc]{xy}
\usepackage{url}
\usepackage{multicol}
\usepackage{color}
\usepackage{array}
\usepackage{mathtools}
\usepackage{epigraph}
\usepackage{mathrsfs}
\usepackage{hhline}
\usepackage{verbatimbox}
\usepackage{hyperref}
\hypersetup{colorlinks=true}
\setlength{\oddsidemargin}{.25in}
\setlength{\evensidemargin}{.25in}
\setlength{\textwidth}{6in}
\setlength{\topmargin}{-0.4in}
\setlength{\textheight}{8.5in}

\input{preamble.tex}

\begin{document}

\lecture{7: More Finite Fields}{Week 7}

\setlength{\epigraphwidth}{.6\textwidth}
\epigraph{We just don't recognize life's most significant moments while they're happening. Back then I thought, "Well, there'll be other days." I didn't realize that that was the only day.}{Dr. Graham, Field of Dreams}


\section{Irreducible Polynomials}
In our last class, we proved the following result:
\begin{prop}
Suppose that $h(x)$ is an irreducible polynomial in $\mathbb{F}_p[x]$ of degree $n$. Then $\mathbb{F}_p[x]/h(x)$ is a field of order $p^n$.
\end{prop}

Motivated by the above result, in this set of notes, we will attempt to prove the following:
\begin{thm}
For any $n$ and $p$, there is an irreducible polynomial of degree $n$ in $\mathbb{F}_p[x]$.
\end{thm}
This theorem, if we can prove it, will give us the following exciting corollary:
\begin{cor}
For any prime $p$ and positive integer $n$, there is a finite field of order $p^n$.
\end{cor}

If this was an abstract algebra class, we would likely attempt to discuss how we can create such polynomials.  Instead, because we are combinatorialists, we will attempt the simpler problem of simply \textbf{counting} the total number of irreducible polynomials!  That is: if we can show that there is at least one irreducible polynomial of every degree, we will have proven our desired result (even though we might not have a great way to actually find said polynomials!)  

While this might seem less useful of a result than an actual construction, this proof will have the advantages of (1) being \textbf{far} faster, (2) \textbf{far} easier to understand, and (3) actually giving you some ways to search for such irreducibles when you examine it closely!

We start with a few lemmas, that will help us understand how polynomials work in $\mathbb{F}_p$.  The first of these is designed to help us understand how to factor polynomials; this will help us when we look for irreducibles, as those will be precisely the polynomials that do not factor into smaller parts!

For integers, we had some useful tools to help factor them into smaller pieces: namely, we could use the Euclidean algorithm to find the GCD of any two numbers, which was often useful for breaking larger numbers into smaller parts!  We restate this process here:

\begin{algorithm}
The Euclidean algorithm is a method for taking two positive integers $a >b$ and calculating their GCD.  It does so as follows.

To initialize our algorithm, set $r_1 = a, r_2 = b$. Our algorithm will create a sequence of values $r_1, r_2, r_3\ldots r_k,$, where this last value $r_k$ will be the GCD of $a,b$.
\begin{enumerate}
\item Suppose that we have defined our sequence up to $r_i, r_{i+1}$, that $r_i > r_{i+1}$, and that $r_{i+1} > 0$.
\item If the remainder of $r_i$ when divided by $r_{i+1}$ is 0, quit our algorithm: $r_{i+1}$ is the GCD we were looking for.
\item Otherwise, to define $r_{i+2}$, simply set it equal to the remainder of $r_i$ when divided by $r_{i+1}$.  This always gives us a number smaller than $r_{i+1}$ that is positive, by definition.
\item Go to 1.
\end{enumerate}
\end{algorithm}

We claim that this process also works on polynomials!  That is, consider the following process for determining the GCD of two polynomials:
\begin{algorithm}
Take any two polynomials $p(x), q(x)$, where the degree of $p(x)$ is not smaller than the degree of $q(x)$.

To initialize our algorithm, set $r_1(x) = p(x), r_2(x) = q(x)$. Our algorithm will create a sequence of polynomials $r_1(x), r_2(x), r_3(x)\ldots r_k(x),$, where this last value $r_k(x)$ will be the GCD of $a,b$.
\begin{enumerate}
\item Suppose that we have defined our sequence up to $r_i(x), r_{i+1}(x)$, that the degree of $r_i(x)$ is greater than the degree of $r_{i+1}(x)$, and that $r_{i+1}(x) \neq 0$.
\item If the remainder of $r_i(x)$ when divided by $r_{i+1}(x)$ is 0, quit our algorithm: $r_{i+1}(x)$ is the GCD we were looking for.
\item Otherwise, to define $r_{i+2}(x)$, simply set it equal to the remainder of $r_i(x)$ when divided by $r_{i+1}(x)$. (Note that we are doing polynomial long division here!  If you are unsure how to do this, check out \href{http://en.wikipedia.org/wiki/Polynomial_long_division}{Wikipedia} for some background, or talk to me!)

This always gives us a polynomial with degree smaller than $r_{i+1}(x)$, by definition.
\item Go to 1.
\end{enumerate}
\end{algorithm}

Checking that this works is a problem we leave for the HW!  Instead, we run an example to illustrate how this works (and more generally, how polynomial arithmetic works over $\mathbb{F}_p[x]$:
\begin{exmp}
The GCD of $3x^5 + 4x^3 + 2x + 1$ and $4x^4 + 2x^2 + 4$ in $\mathbb{F}_5[x]$ is 
\end{exmp}
\begin{proof}
We simply run the algorithm above!  Set $r_1(x) = 3x^5 + 2x^3 + 2x + 3$ and $r_2(x) = 4x^4 + 2x^2 + 4$.  Then, by polynomial long division, using the fact that $2 \cdot 4 \equiv 3 \mod 5$, we have

\begin{align*}
\begin{array}{m{5.5em}rrrrrrrr}
&    &        &    & & 2x&    &  \\
\cline{2-7}
\multicolumn{2}{l}{4x^4 + 2x^2 + 3\big)}
     & 3x^5  & +2x^3 &  & +2x & + 3 \\
&    &-3x^5   &-4x^3& & - 3x   &     \\
\cline{3-7}
&    &    &    3x^3&&  + 4x  &   +3  \\
\end{array}
\end{align*}
and thus that $r_3(x) = 3x^3 + 4x  +3$.  We repeat the process on $r_2(x), r_3(x)$, using the fact that $3 \cdot 3 \equiv 4 \mod 5$:
\begin{align*}
\begin{array}{m{5.2em}rrrrrrrr}
&    &        &    & & 3x&    &  \\
\cline{2-7}
\multicolumn{2}{l}{3x^3 + 4x  +3\big)}
     & 4x^4  &  & +2x^2 &  & + 4 \\
&    &-4x^4  & &-2x^2&  - 4x   & &    \\
\cline{3-7}
&    &    &    &&  x  &   +4  \\
\end{array}
\end{align*}

Finally, we notice that 
\begin{align*}
\begin{array}{m{2em}rrrrrrrr}
&    &        &   3x^2 & & &     \\
\cline{2-6}
\multicolumn{2}{l}{x+4\big)}
     & 3x^3  &  & +4x & + 3  \\
&    &-3x^3  & - 2x^2 &&     & &    \\
\cline{3-6}
&    &    &  3x^2  &+ 4x & +3    \\
&    &    &  -3x^2  &- 2x &    \\
\cline{4-6}
&    &    &    &2x & + 3    \\
&    &    &    &-2x & - 3    \\
\cline{5-6}
&    &    &    & & 0    \\
\end{array}
\end{align*}
Therefore, we know that we're done at $r_4(x)$, and thus that the GCD of these two polynomials is $x+4$, which is perhaps more usefully written as $x-1$!  To check, you can notice that both
\begin{align*}
3x^5 + 2x^3 + 2x + 3 &= (x-1)(3x^4 + 3x^3 + 2),\\
4x^4 + 2x^2 + 4 &= (x-1)(4x^3 + 4x^2 + x+ 1),
\end{align*}
and so this is a factor of both polynomials.  (Checking that it's the largest such shared common factor I leave for the reader!  It's not interesting: just try to factor the remaining polynomials above into smaller ones, and see that they don't have factors in common.)
\end{proof}

We will use this polynomial-Euclidean-algorithm to prove the following theorem:
\begin{thm}
For any prime $p$ and positive integer $n$, $x^{p^n} - x$ is the product of all of the monic\footnote{A polynomial is called \textbf{monic} if the coefficient of its highest-degree term is 1.  So, $x^2+ 2x + 4$ is monic, while $6x^2 - 53$ is not monic.} irreducible polynomials in $\mathbb{F}_p[x]$ whose degree divides $n$.
\end{thm}
The reasons that we care about this theorem shouldn't be obvious yet; while it certainly tells us things about irreducibles, it's not obvious what things it tells us!  Don't worry about that for now; when we get to the next section of these notes, on the inclusion-exclusion principle, this will become very clear! 

For now, however, process it as a ``weird fact about irreducibles that we're interested in proving.''

To prove it, we use the following lemmas.  The first is simple, but will help us later:
\begin{lem}
Take any prime $p$ and any integers $n, d$.  Suppose that $n \equiv m \mod d$.  Then
\begin{align*}
p^n \equiv p^{m} \mod (p^d - 1).
\end{align*}
\end{lem}
\begin{proof}
Notice that by definition,
\begin{align*}
p^d - 1 \equiv 0 \mod (p^d - 1),
\end{align*}
and thus that
\begin{align*}
p^d \equiv 1 \mod (p^d - 1).
\end{align*}
If $n \equiv m \mod d$, then we can find some $k$ such that $n = kd + m$.  Consequently, we can use our observation above to see that
\begin{align*}
p^n  = p^m \cdot p^{kd} = p^m \cdot (p^d)^k \equiv p^m  \cdot 1  \equiv p^m \mod (p^d - 1).
\end{align*}
So we have proven our claim.
\end{proof}

The second lemma is not any harder, but is useful to point out for understanding how roots work in $\mathbb{F}_p[x]$:
\begin{lem}
Let $f(x)$ be a polynomial in $\mathbb{F}[x]$, for any field $\mathbb{F}$.  Then, if $f(a) = 0$ for some $a \in \mathbb{F}$, we can write $f(x) = (x-a) \cdot g(x)$, for some other polynomial $g(x) \in \mathbb{F}[x]$.
\end{lem}
\begin{proof}
On the HW!  (Basically: this is obvious when $a = 0$, as $f(0) = 0$ just means that the constant term of $f(x)$ is 0, which means we can factor out an $x$ from all remaining terms.  When $a \neq 0$, try looking at the polynomial $h(x) = f(x-a)$.  How can a factorization of $h(x)$ help you factor $f(x)$?)
\end{proof}

The third is much tricker, but will help us understand the idea of ``factors of $x^{p^n} - x$.''
\begin{lem}
Take any prime $p$ and any integers $n, d$.  Then in $\mathbb{F}_p[x]$, we have that
\begin{align*}
gcd\left(x^{p^n} - x, x^{p^d} - x\right) = x^{p^{gcd(n,d)}}.
\end{align*}
(When we talk about the GCD of two polynomials, we mean the highest-degree factor that divides both polynomials.  For example, the GCD of $x^2 - 2x + 1$ and $x^2 - 1$ is $(x-1)$, as it is a factor of both polynomials.)
\end{lem}
\begin{proof}
For convenience of notation, I will replace our $p(x) \equiv_{h(x)} q(x)$ notation with $p(x) \equiv q(x) \mod h(x)$, as this will let me use different $h(x)$'s with more ease / make the subscripts less messy.

First, notice that for any $k$, we can write $x^{p^n} - x$ as the following sum/product:
\begin{align*}
x^{p^n} - x = \left( \sum_{i=1}^k x^{p^n - i(p^d - 1) - 1}\right)\cdot \left( x^{p^d} - x\right) + \left( x^{p^n - k(p^d - 1)} - x\right).
\end{align*}
This is a standard telescoping-trick thing, that we verify here for the skeptical:
\begin{align*}
&\left( \sum_{i=1}^k x^{p^n - i(p^d - 1) - 1}\right)\cdot \left( x^{p^d} - x\right) + \left( x^{p^n - k(p^d - 1)} - x\right) \\
=& \left( \sum_{i=1}^k x^{p^n - i(p^d - 1) - 1 + p^d}\right) - \left( \sum_{i=1}^k x^{p^n - i(p^d - 1) - 1 + 1}\right)+ \left( x^{p^n - k(p^d - 1)} - x\right)\\
=& \left( \sum_{i=1}^{k} x^{p^n - (i-1)(p^d - 1)}\right) - \left( \sum_{i=1}^k x^{p^n - i(p^d - 1)}\right)+ \left( x^{p^n - k(p^d - 1)} - x\right)\\
=& \left( \sum_{i=0}^{k-1} x^{p^n - i(p^d - 1)}\right) - \left( \sum_{i=1}^k x^{p^n - i(p^d - 1)}\right)+ \left( x^{p^n - k(p^d - 1)} - x\right)\\
=& \left(  x^{p^n - 0(p^d - 1)} - x^{p^n - k(p^d - 1)}\right)+ \left( x^{p^n - k(p^d - 1)} - x\right)\\
=& x^{p^n} - x.
\end{align*}
Consequently, if we use this identity, we can see that
\begin{align*}
x^{p^n} - x \equiv \left( x^{p^n - k(p^d - 1)} - x\right) \mod x^{p^d} - 1.
\end{align*}

We showed earlier that if $n \equiv m \mod d$, then
\begin{align*}
p^n \equiv p^{m} \mod (p^d - 1);
\end{align*}
in other words, we can find some $k$ such that
\begin{align*}
p^n - k(p^d - 1)  =  p^{m}.
\end{align*}

But (if we set $m$ to be the remainder of $n$ divided by $d$, i.e. $\textrm{rem}(n/d)$) this means that we have
\begin{align*}
x^{p^n} - x \equiv \left( x^{p^{\textrm{rem}(n/d)}} - x\right) \mod x^{p^d} - 1.
\end{align*}
Notice that because adding copies of $x^{p^d} - 1$ to $ x^{p^{n \textrm{ mod } d}} - x$ will result in a polynomial of degree strictly larger than $p^{n \textrm{ mod } d}$, we have actually shown that the remainder of $x^{p^n} - x$ on division by $x^{p^d} - 1$ \textbf{is} $x^{p^{n \textrm{ mod } d}} - x$. In other words, we performed the first step of our Euclidean algorithm on polynomials!

For notational convenience, let's set
\begin{align*}
f_1(x) &= x^{p^n} - x = x^{p^{r_1}} - x,\\
f_2(x) &= x^{p^d} - x = x^{p^{r_2}} - x,\\
f_3(x) &= x^{p^{\textrm{rem}(r_1/r_2)}} - x = x^{p^{r_3}} - x.\\
\end{align*} 
Then, if we simply apply our above logic to $ x^{p^{r_2}} - x, x^{p^{r_3}} - x,$ we can get that
\begin{align*}
f_4(x) &= x^{p^{\textrm{rem}(r_2/r_3)}} - x = x^{p^{r_4}} - x,\\
\end{align*} 
and in general get that
\begin{align*}
f_k(x) &= x^{p^{\textrm{rem}(r_{k-2}/r_{k-1})}} - x = x^{p^{r_k}} - x.\\
\end{align*} 
When do we terminate our algorithm?  When we get to some $f_k(x) = 0$; that is, some $r_k = 0$, as $x^{p^0} - x = x-x = 0$.

So it suffices to examine the sequence of the $r_i$'s!  Look at what we've formed here:
\begin{itemize}
\item $r_1 = n, r_2 = d$,
\item $r_k = $ the remainder of $r_{k-2}$ on division by $r_{k-2}$,
\end{itemize}
This is just the Euclidean algorithm for integers applied to $n, d$!  So we know the last nonzero value of this sequence is $gcd(n,d)$; consequently, we also know that the last term of our $f_k$ sequences is
\begin{align*}
x^{p^{gcd(n,d)}} - x.
\end{align*}

In other words, we've proven our claim: that
\begin{align*}
gcd\left(x^{p^n} - x, x^{p^d} - x\right) = x^{p^{gcd(n,d)}} - x.
\end{align*}
\end{proof}

Think about this for a while; it is almost surely the trickiest proof we have done in this class so far. There is nothing really crazy here --- to find the GCD of polynomials, we used the Euclidean algorithm, and noticed a pattern that made it work like the Euclidean algorithm on integers if you just looked at the exponents!  But there were a lot of weird superscripts and it may be hard to keep track of it all.

It also may be hard to see why we care! That comes in when we look at the following promised theorem:
\begin{thm}
For any prime $p$ and positive integer $n$, $x^{p^n} - x$ is the product of all of the monic irreducible polynomials in $\mathbb{F}_p[x]$ whose degree divides $n$.
\end{thm}
\begin{proof}
Take any monic irreducible polynomial $\pi(x)$ of degree $d$, where $d | n$.  We will show that $\pi(x)$ divides $x^{p^n} - x$!

To see why: notice that because $\pi(x)$ is irreducible, then $\mathbb{F}_p[x]/\pi(x)$ is a field of order $p^d$, as shown in class!

Therefore, if you look at all of the nonzero elements in this field, you get a multiplicative group of order $p^d - 1$.  The order of any element in this group, by Lagrange's theorem, must divide the size of this group; consequently we know that for any $a$ in our field, we must have $a^{p^d - 1} = 1$ , and therefore that $a^{p^d} - a = 0$ for any $a$ in our field!

In other words: the expression $a^{p^d} - a$ is equal to 0 for every element $a$ in our field $\mathbb{F}_p[x]/\pi(x)$ (as plugging in 0 to this expression also yields 0!)

But what does this mean?  Well: consider two cases.
\begin{enumerate}
\item The degree of $\pi(x)$ is greater than 1.  This means that $x$ is an element of $\mathbb{F}_p[x]/\pi(x)$, as it has smaller degree than $\pi(x)$.

We just showed that plugging in any element $a$ of our field into the expression $a^{p^d} - a$ yields 0; so, let's do this with $x$!  We get that
\begin{align*}
x^{p^d} - x = 0;
\end{align*}
in other words, $x^{p^d} - x$ is a multiple of $\pi(x)$.  By our lemma earlier, this means that $x^{p^n} - x$ is also a multiple of $\pi(x)$, as claimed!
\item The degree of $\pi(x) = 1$.  This we can verify directly: write $\pi(x) = x+ c$ for some constant $c \in \mathbb{F}_p$ (any monic irreducible polynomial is in this form!), and note that the typical telescoping-sum trick gives us
\begin{align*}
x^{p^n} - x = (x+c)\Big(x^{p^n - 1} + (-c)x^{p^n - 2} &+ (-c)^2x^{p^n -3} + (-c)^3 x^{p^n - 4} + (-c)^4 x^{p^n - 5} +  \ldots\\
 \ldots &+(-c)^{p^n -3}x^2 +(-c)^{p^n -2}x^1 + (-c)^{p^n -1}\Big),
\end{align*}
because
\begin{align*}
(x+c)\left(\sum_{k=1}^{p^n-1} x^{p^n - k} (-c)^{k-1}\right) &= \left(\sum_{k=1}^{p^n-1} x^{p^n - k + 1} (-c)^{k-1}\right) - \left(\sum_{k=1}^{p^n-1} x^{p^n - k + 1} (-c)^{k}\right)\\
&=\left(\sum_{k=0}^{p^n-2} x^{p^n - k} (-c)^{k}\right) - \left(\sum_{k=1}^{p^n-1} x^{p^n - k + 1} (-c)^{k}\right)\\
&= x^{p^n}(-c)^0 \pm (\textrm{terms that all cancel out}) -  x^{1}(-c)^{p^n - 1}\\
&= x^{p^n} - x.
\end{align*}
(The last step here is justified by the observation that $(-c)^{p-1} = 1$ by Fermat's little theorem, and therefore that $(-c)^{p^n - 1}$ is also 1, because $p^n - 1 = (p-1)(p^{n-1} + p^{n-2} + \ldots + p + 1)$.)
\end{enumerate}

So we have shown that $\pi(x)$ divides $x^{p^n} - x$, whenever $\pi(x)$ is an irreducible polynomial of degree $d$ with $d | n$.  

We now do something very silly.  Observe that the derivative of $x^{p^n} - x$ is just $p^n \cdot x^{p^n - 1} - 1 = -1$ in $\mathbb{F}_p[x]$.  Now, consider any polynomial of the form $f(x))^2 g(x)$ in $\mathbb{F}_p[x]$; the derivative of this polynomial is just $2f(x)(f'(x)g(x) + f(x)g'(x))$, which is a multiple of $f(x)$!  Finally, observe that $-1$ is not a multiple of any nonconstant polynomial.  What can you conclude?  

\textbf{No factor of $x^{p^n} - x$ occurs more than once!}

This is a fun trick, and it comes up a lot!  Derivatives: they come in handy.

We are nearly done.  We know that if $d | n$ and $\pi(x)$ is an irreducible polynomial of degree $d$, it shows up exactly once in the factorization of $x^{p^n} - x$ into irreducibles.  To finish our proof, it suffices to show that no other irreducible polynomials show up in this factorization.  

We prove this by induction on $n$.  For $n = 1$, we know that $x^p - x$ satisfies this property, by our ``factoring out linear roots'' result earlier: because $a^p - a$ = 0 for any $a \in \mathbb{F}_p$ by Fermat's little theorem, we can factor out $p$ linear roots from $x^p - x$, and can see that there are no more because this is a polynomial of degree $p$.

For our induction step: take any irreducible polynomial $\pi(x)$ that divides $x^{p^n} - x$.  Suppose that the degree of $\pi(x)$ is $d$: then $\pi$ divides $x^{p^d} - x$ by our work above.  Consequently, $\pi(x)$ divides the GCD of $x^{p^n} - x, x^{p^d} - x$, which we showed was $x^{p^{gcd(n,d)}} - x$.

If the GCD of $n$ and $d$ is smaller than $d$, then by induction we know that it is impossible for $\pi(x)$ to divide $x^{p^{gcd(n,d)}} - x$!  But $x^{p^{gcd(n,d)}} - x$ divides $x^{p^n} - x$ by our earlier arguments; so this is a contradiction.

So we must have the GCD of $n$ and $d$ equal to $d$; in other words, $d$ divides $n$.
\end{proof}

Woo!  Hardest proof of the quarter.

\ldots what can we \textbf{do} with it?

\section{Inclusion-Exclusion}

The principle of \textbf{inclusion-exclusion} is a relatively simple method for determining the number of things in a collection, akin to our counting rules from earlier.
\begin{thm}
(\textbf{Principle of Inclusion-Exclusion}.)  Suppose that we have two sets $A, B$, and we want to count the total number of elements in $A \cup B$.  We can express this quantity in terms of the sets $A, B$ and $A \cap B$ as follows: 
\begin{align*}
|A \cup B| = |A| + |B| - |A \cap B|.
\end{align*}
The reasoning behind this is simple: if we want to count all of the elements in $A \cup B$, we can just count the elements in $A$, and add to this the number of elements in $B$.   However, doing this double-counts everything in their overlap; so we need to subtract off $|A \cap B|$ to insure that we've counted every element in $A \cup B$ exactly once.

We can extend this to three sets as well, by the same reasoning:
\begin{align*}
|A \cup B \cup C| = |A| + |B| - |A \cap B| - |A \cap C| - |B \cap C| + |A \cap B \cap C|.
\end{align*}
This is because counting $A \cup B \cup C$ can be done by the followingL
\begin{itemize}
\item First, count all of the elements in $A, B, C$ separately and add these quantities.
\item This process ``double-counted'' all of the elements in the overlap of those sets.  So, subtract off $|A \cap B|, |A \cap C|$ and $|B \cap C|$ to fix this.
\item Now, think about elements in $A \cap B \cap C$.  They were counted in each of $A, B, C,$ positively and  $|A \cap B|, |A \cap C|$ and $|B \cap C|$ negatively; so at the moment we're not counting them at all!  Fix this by adding back in $|A \cap B \cap C|$.
\item We are now counting each element in $A\cup B \cup C$ once!
\end{itemize}

In general, we can express the size of the union of $n$ sets $A_1, \ldots A_n$ by a similar process:
\begin{align*}
\biggl|\bigcup_{i=1}^n A_i\biggr| = \sum_{i=1}^n\left|A_i\right|\;
-\sum_{1 \le i < j \le n}\left|A_i\cap A_j\right|\; 
+ \sum_{1 \le i < j < k \le n}\left|A_i\cap A_j\cap A_k\right|\;-\ \ldots\ +\; \left(-1\right)^{n-1} \left|A_1\cap\cdots\cap A_n\right|.
\end{align*}
We can express this compactly as follows, if you like:
\begin{align*}
\Biggl|\bigcup_{i=1}^n A_i\Biggr| = \sum_{k = 1}^{n} (-1)^{k+1} \left( \sum_{1 \leq i_{1} < \cdots < i_{k} \leq n} \left| A_{i_{1}} \cap \cdots \cap A_{i_{k}} \right| \right).
\end{align*}
The justification for this formula is similar to the above; we leave it for the homework!
\end{thm}

Instead, we focus on what we can accomplish with this idea: clever counting tricks!  We start with a warmup:
\begin{question}
Take the collection of all numbers from 1 to 1000.  How many are multiples of $2,3$ or $5$?
\end{question}
\begin{answer}
We simply apply the principle of inclusion-exclusion to find all of the multiples of 2, 3 or 5.  We can ``over-count'' by simply finding all multiples of 2, adding this to all multiples of 3, and then adding this to all multiples of 5:
\begin{align*}
\left\lfloor \frac{1000}{2} \right\rfloor + \left\lfloor \frac{1000}{3} \right\rfloor + \left\lfloor \frac{1000}{5} \right\rfloor 
\end{align*}
However, this ``overcounts'' by counting multiples of 6, 10 and 15 twice, as all of these show up in two of the quantities above.  So fix this by subtracting one copy of all such numbers off:
\begin{align*}
\left\lfloor \frac{1000}{2} \right\rfloor + \left\lfloor \frac{1000}{3} \right\rfloor + \left\lfloor \frac{1000}{5} \right\rfloor - \left\lfloor \frac{1000}{6} \right\rfloor - \left\lfloor \frac{1000}{10} \right\rfloor - \left\lfloor \frac{1000}{15} \right\rfloor
\end{align*}
This last correction has left us without any multiples of 30, which we need to count!  So add those back in:
\begin{align*}
\left\lfloor \frac{1000}{2} \right\rfloor + \left\lfloor \frac{1000}{3} \right\rfloor + \left\lfloor \frac{1000}{5} \right\rfloor - \left\lfloor \frac{1000}{6} \right\rfloor - \left\lfloor \frac{1000}{10} \right\rfloor - \left\lfloor \frac{1000}{15} \right\rfloor + \left\lfloor \frac{1000}{30} \right\rfloor.
\end{align*}
This is the size of our set! For fun, let's evaluate it:
\begin{align*}
&\left\lfloor \frac{1000}{2} \right\rfloor + \left\lfloor \frac{1000}{3} \right\rfloor + \left\lfloor \frac{1000}{5} \right\rfloor - \left\lfloor \frac{1000}{6} \right\rfloor - \left\lfloor \frac{1000}{10} \right\rfloor - \left\lfloor \frac{1000}{15} \right\rfloor + \left\lfloor \frac{1000}{30} \right\rfloor\\
=& 500 + 333 + 200 - 166 - 100 - 66 + 33\\
=& 734.
\end{align*}
\end{answer}

A second, trickier example of inclusion-exclusion is the following puzzle:

\begin{exmp}
Make a $100 \times 200 \times 300$ box out of $1 \times 1 \times 1$ cubes, and place this box in $\mathbb{R}^3$ so that one corner is at the origin and the other is at $(60, 140, 210)$.  Draw the diagonal connecting $(0,0,0)$ to $(60, 140, 210)$.  How many $1 \times 1 \times 1$ cubes does this cross?
\end{exmp}
\begin{proof}
Our diagonal crosses over from one small cube to another precisely when it passes through a face, edge or vertex of one of our cubes.  This happens precisely at places where at least one of the coordinates of our line has integer co\"ordinates!  So it suffices to count all of these points, as doing this will tell us the total number of cubes that we enter.  (Note that we don't want to count the point $(60, 140, 210)$ in any such count, as at this point we don't ``cross over'' into a new cube.)

How can we do this?  Well: our diagonal line's co\"ordinates are all points of the form
\begin{align*}
(60t, 140t, 210t),
\end{align*}
for $t \in [0,1]$.  When is this integral and not $(60, 140, 210)$?

Well: the first co\"ordinate is integral for precisely 60 values of $t$, namely $t = \frac{0}{60}, \frac{1}{60}, \frac{2}{60}, \ldots \frac{59}{60}$.  Similarly, the second co\"ordinate is integral for precisely 140 values of $t$, and the third coordinate is integral for 210 values of $t$.  

However, this process overcounts some points!  For example, points with their first two co\"ordinates as integers are counted twice above; however, we only want to count such a point once!  So we need to remove one copy of all of the points with two integer co\"ordinates.  This is not hard to do: $(60t, 140t)$ are both integral iff $t = \frac{k}{gcd(60,140)} = \frac{k}{20}$, of which there are twenty possible values.  Similarly, $(60t, 210t)$ is integral at $gcd(60,210) = 30$ values, while $(140t, 210t)$ is integral at $gcd(140, 210) = 70$ values.

Finally, we've over-subtracted some points in this last step: namely, those for which all three coordinates are integral!  Those happen at values of $t$ such that $(60t, 140t, 210t)$ are all integral; i.e. points of the form $\frac{k}{gcd(60,140,210)} = \frac{k}{10}$, of which there are 10.

So, in total, we have that there are
\begin{align*}
60+140+210 - 20 - 30 - 70 + 10 = 300
\end{align*}
many such cubes intersected by our diagonal!
\end{proof}


\subsection{Applying inclusion-exclusion to irreducibles.}

Why mention this counting technique here?  The answer is that it lets us answer the problem we started this lecture with: namely, how to count irreducible polynomials!

In specific, we have the following result:
\begin{thm}
Let $M_n(p)$ denote the number of monic irreducible polynomials of degree $n$ in $\mathbb{F}_p[x]$.  Then we have the following equation:
\begin{align*}
M_n(p) = \frac{1}{n} \sum_{d | n} \mu(d) p^{n/d},
\end{align*}
where $\mu(d)$ is the \textbf{M\"obius function}, defined as follows:
\begin{align*}
\mu(d) := \left\{\begin{array}{cc} 
1, & d \textrm{ is squarefree and has an even number of prime factors},\\
-1, & d \textrm{ is squarefree and has an odd number of prime factors},\\
0, & d \textrm{ has a squared prime factor.}\\\end{array}\right.
\end{align*}
\end{thm}
\begin{proof}
We proved earlier in the notes the following result: for any prime $p$, natural number $n$,
\begin{align*}
x^{p^n} - x = \prod_{\substack{\pi(x) \textrm{ monic and}\\\textrm{irreducible},\\\textrm{s.t. }\deg(\pi(x)) \big| n}} \pi(x).
\end{align*}
So: if we just think about the degrees of all of the irreducibles in the right-hand-side product, we can see that the sum of their degrees must be $p^n$, because the product of their polynomials is a polynomial of degree $d^n$!  Consequently, we have
\begin{align*}
p^n &= \sum_{\substack{\pi(x) \textrm{ monic and}\\\textrm{irreducible},\\\textrm{s.t. }\deg(\pi(x)) \big| n}} \deg(\pi(x)).\\
\end{align*}
If we group irreducibles by their degrees, we can further refine this statement to the equation
\begin{align*}
p^n &= \sum_{d|n} d \cdot M_d(p),
\end{align*}
which we have proven holds for all primes $p$, natural numbers $n$! 

We want to solve the equations above for $M_n(d)$.  To do this, we can simply use inclusion-exclusion!  

Specifically: notice that in a sense, $p^n$ ``counts'' $M_n(n)$ in its sum, as we have
\begin{align*}
p^n &= n\cdot M_n(n) + \sum_{d|n, d < n} d \cdot M_d(p).
\end{align*}
The only issue is that we've inadvertently also counted the $M_d(p)$'s at the same time!  Conveniently, however, we know that we can count these with $p^d$'s as well, for appropriate values of $d$!

To get a feel for how this works, let's consider some sample values of $n$.  For $n=1$, for example, we don't even need to think about double-counting:
\begin{align*}
p^1 &= \sum_{d|1} d \cdot M_d(p) = M_1(p).
\end{align*}
So the number of degree-1 monic irreducibles is $p$!  This makes sense; we have that $x-c$ is a monic irreducible of degree 1 for every $c \in \mathbb{F}_p$, as these terms can't be broken down into smaller terms.

Let's consider $n = q$, for any prime $q$. Then, we have
\begin{align*}
p^n  = p^q &= \sum_{d|n} d \cdot M_d(p) = M_1(p) + qM_q(p).\\
\Rightarrow qM_q(p) &=   p^q - M_1(p)  =   p^q - p.
\end{align*}

In this case, we were able to ``correct'' for $M_q(p)$'s overcounting by $M_1(p)$ by just subtracting this term off!

Let's go a bit further, and consider $n = qr$, for any two primes $q, r$. Then, we have two possibilities: either $q = r$, in which case we have
\begin{align*}
p^n &= \sum_{d|n} d \cdot M_d(p) = M_1(p) + qM_q(p) + q^2 M_{q^2}(p),\\
\end{align*}
or $q \neq r$, in which case we have
\begin{align*}
p^n &= \sum_{d|n} d \cdot M_d(p) = M_1(p) + qM_q(p) + rM_r(p) + qr M_{qr}(p).\\
\end{align*}

These are different cases!  In the first, we would want to correct by subtracting off two terms;
\begin{align*}
q^2 M_{q^2}(p)  = p^{q^2} -qM_q(p) - M_1(p).
\end{align*}
Here, we could simply use our earlier knowledge of what $M_q(p)$ is for any primes $q,p$, but this is actually not the most useful observation to make here.  Instead, notice that we showed that $p^q$ counts $qM_q(p)$, with an overcount by $M_1(p)$: therefore, we have
\begin{align*}
q^2 M_{q^2}(p)  = p^{q^2} - qM_q(p) - M_1(p) = p^{q^2} - (p^q - M_1(p)) - M_1(p) = p^{q^2} - p^q.
\end{align*}

Similarly, in the second case, we could apply this knowledge as well: both $qM_q(p)$ and $rM_r(p)$ are counted by $p^q, p^r$ respectively, with each overcounting by $M_1(p)$.  Therefore, we have
\begin{align*}
qr M_{qr}(p) = p^{qr} - (p^q - M_1(p)) - (p^r - M_1(p)) + M_1(p) = p^{qr} - p^q - p^r + p.
\end{align*}

So far, this lines up with our claim: that
\begin{align*}
M_n(p) = \frac{1}{n} \sum_{d | n} \mu(d) p^{n/d},
\end{align*}
where $\mu(d)$ is the \textbf{M\"obius function}, defined as follows:
\begin{align*}
\mu(d) := \left\{\begin{array}{cc} 
1, & d \textrm{ is squarefree and has an even number of prime factors},\\
-1, & d \textrm{ is squarefree and has an odd number of prime factors},\\
0, & d \textrm{ has a squared prime factor.}\\\end{array}\right.
\end{align*}
In particular, we've proven this claim for all values of $n$ with at most two prime factors!

We will prove this claim by induction on the number $k$ of prime factors of $n$.  We have already accomplished our base cases above; now, let's assume that our claim works for all integers with $k$ prime factors, and take any $n$ with $k+1$ prime factors.  Label them $q_1, \ldots q_{k+1}$ for convenience.  

As proven earlier in class, we know that

\begin{align*}
p^n &= \sum_{d|n} d \cdot M_d(p).
\end{align*}
Solving for $M_n(p)$ gives us
\begin{align*}
nM_n(p) = p^n - \sum_{d|n, d < n} d \cdot M_d(p).
\end{align*}
Because $d | n$ and $d< n$, we know that each value of $d$ in the sum above has $k$ or fewer factors!  Therefore, we can apply our inductive claim to it, and get
\begin{align*}
nM_n(p) = p^n - \sum_{d|n, d < n} \left( \sum_{c | d} \mu(c) p^{d/c}\right)
\end{align*}
What do the individual terms of the sum above look like?  Well: take any factor $b$ of $n$.  In how many ways can $b$ occur as some expression of the form $d/c$?  Well:
\begin{itemize}
\item If $b$ has $k$ prime factors, then there is only one way in which this happens: when $d = b, c = 1$.  This is because $d$ is restricted to terms that are less than $n$, and therefore terms that have at most $k$ of $n$'s prime factors.

In this case, we have that the only term containing a $q^b$ is the $\mu(1)q^b = q^b$ term.
\item If $b$ has $k-1$ prime factors, then we can write $bq_1q_2 = n$, for two primes $q_1, q_2$.  We again look at all of the ways to write $b = d/c$:
\begin{itemize}
\item When $d = b, c = 1$.  $\mu(c)$ is 1 here.
\item When $d = b \cdot q_i, c = q_i$, for one of the two prime factors $q_1, q_2$.  $\mu(c)$ is -1 here.
\end{itemize}
So, in total, we have $1-2 = -1$ ways in which this happens, if we scale by the $\mu(c)$ terms.

\item In general, what happens?  Well: suppose that $n/b$ consists of $l$ \textbf{distinct} prime factors.  Call them $q_1, \ldots q_{l}$.  In this case, we can have $b = d/c$ in several different ways:
\begin{itemize}
\item Once, when $d = b$, $c = 1$.  $\mu(c)$ is 1 here.
\item We can have this happen $l$ different ways, when $d = bq_j, c = q_j$ for any of the $l$ distinct factors $q_j$ of $n/b$.  $\mu(c)$ is $-1$ here.
\item We can have this happen $\binom{l}{2}$ different ways, when $d = bq_iq_j, c = q_iq_j$.  $\mu(c)$ is 1 here.
\item We can have this happen $\binom{l}{3}$ different ways, when $d = bq_iq_jq_m, c = q_iq_jq_m$. $\mu(c)$ is $-1$ here.
\item \ldots
\item We can have this happen $\binom{l}{l-1}$ different ways, when $d = \frac{n}{q_j}, c = \frac{q_1\cdot \ldots \cdot q_{k-l}}{q_j}$.  $\mu(c) = (-1)^{l-1}$ here.
\end{itemize}
So, in total we have 
\begin{align*}
\sum_{i=0}^{l-1} (-1)^i\binom{l}{i}
\end{align*}
ways to make $b$, if we scale by the $\mu(c)$'s.

What is this sum?  I claim it is more recognizable if we throw in an additional term: namely, notice that
\begin{align*}
\sum_{i=0}^{l} (-1)^i\binom{l}{i} = (1-1)^l,
\end{align*}
by the binomial theorem!  We know that the RHS above is just 0; therefore, the LHS is 0 as well, and therefore the first $l-1$ terms of the LHS are equal to the last term; that is,
\begin{align*}
\sum_{i=0}^{l-1} (-1)^i\binom{l}{i} = \binom{l}{l}\cdot (-1)^l = (-1)^l.
\end{align*}
So the number of ways to make $b$ is just $(-1)^l$, if $n/b$ consists of $l$ distinct prime factors.
\item Finally, let's consider what happens if $n/b$ has a repeated prime $q$ in it.  In this situation, notice that we can ``pair'' ways to write $b = d/c$ as follows:
\begin{itemize}
\item If $b = d/c$ and $c$ has no factors of $q$ in it, pair this to the way of expressing $b$ as $(dq)/(cq)$.  Note that $\mu(c) = -\mu(cq)$, as we've gained exactly one new prime factor in this way.
\item This pairs off all pairs $d/c$ where $c$ contains no $q$'s or one $q$.  Any other $c$'s contain at least 2 factors of $q$, and thus have $\mu(c) = 0$.
\end{itemize}
So, in total, we have 0 such ways to make this $b$, if we scale by the $\mu(c)$'s.
\end{itemize}
But what does this mean?  We have shown that a term $q^{b}$ shows up in our sum iff $b$ has no repeated factors, and moreover that the sign with which it shows up in our sum is given by the number of distinct prime factors in $n/b$!  In other words, if we set $b=d$, we've proven our claim: that
\begin{align*}
M_n(p) = \frac{1}{n} \sum_{d | n} \mu(d) p^{n/d}.
\end{align*}
\end{proof}

As a nice corollary, we have the following:
\begin{cor}
There are irreducible polynomials of degree $n$ in $\mathbb{F}_p[x]$ for any $n, p$.
\end{cor}
\begin{proof}
It suffices to show that $M_n(p) \geq 1$ for any $n, p$. 


Doing this is not hard: if we have 
\begin{align*}
nM_n(p) = \sum_{d | n} \mu(d) p^{n/d},
\end{align*}
simply observe that the sum on the right is strictly larger than the sum
\begin{align*}
p^n - \sum_{k=0}^{n-1} p^k.
\end{align*}
But for any integer $p \geq 2$, this is always greater than 1, which we can observe by induction; $p-1 > 1$, and if 
\begin{align*}
p^n - \sum_{k=0}^{n-1} p^k &> 1,
\end{align*}
we can multiply both sides by $p$ to get
\begin{align*}
p^{n+1} - \sum_{k=0}^{n-1} p^{k+1} > p
\Rightarrow  p^{n+1} - \sum_{k=0}^{n-1} p^{k+1} - 1 &> p-1
\Rightarrow  p^{n+1} - \sum_{k=0}^{n} p^{k} &> p-1 > 1.
\end{align*}
So we've proven our claim!
\end{proof}
\end{document}


