Lecture 11 - Euler's Phi Function, Cont'd.


In today's class we began by talking about a few "magic tricks" one can do using modular arithmetic. These were motivated by a posting from the forum which showed a video of a "mathemagician" at work. Afterwards we returned to our discussion of Euler's $\phi$ function from Friday. We first calculated the value of the $\phi$ function for "special integers," and eventually noticed that the $\phi$ function obeys some rules which make the calculation of $\phi(n)$ easy — at least when you have a prime factorization of n.

Magic Tricks and Modular Arithmetic

As a fun beginning to class, we talked a little about some of the magic tricks which are shown in the video from one of the forum threads. Some of these tricks — particularly the presenters ability to square 3-, 4- or 5-digit numbers in his head — come from clever ways of thinking about multiplication together with the skill of performing several small computations without benefit of paper (or, seemingly, time). Others, though, are a bit simpler.

At one point, the presenter takes the number "8649" — a number which came up in the course of another calculation earlier in the show — and told people to multiply this by a random 3-digit number of their choosing. After calculating the product, they were then supposed to give him all but 1 of the digits of that product. He would then tell them the digit they had omitted. How could he manage such a trick?

In this case, notice that the first number — 8649 — is a multiple of 9. This means that no matter what number you multiply it by, you're destined to get back a number which is also a multiple of 9. Recall, however, that the sum of the digits of a number divisible by 9 is itself divisible by 9. So if the person tells you that the digits in the product — even after some random rearrangement — are "4,7,0,3,6,8," then you know the missing digit is whatever number you need to add to

\begin{equation} 4+7+0+3+6+8 = 28 \end{equation}

in order to get a multiple of 9. In this case, you know that the missing number is 8. (Notice that if they had instead told you that the digits included "4,8,7,8,3,6" — which add up to 36 — you wouldn't have known whether the missing digit is a 0 or a 9!).

Another one of his tricks was to compute on which day of the week certain dates fell. Someone would say, for instance, "January 11 1980" and he would say "That was a Friday." To see one way he might have computed the day of the week, check out problem 2 in the "student projects" of Chapter 2. You might also rewatch that portion of the video and keep your eyes peeled on his hands — he uses them to keep track of certain calculations once people give him bits of information.

Calculating Euler's Phi

Last class period we introduced Euler's $\phi$ function and saw that for any number a with the property that $(a,n) = 1$, we must have $a^{\phi(n)} \equiv 1 \mod{n}$. This was a generalization of Fermat's Little Theorem. Notice that this gives us a method for computing the multiplicative inverse of a given number modulo n without a guess-and-check technique; it provides a systematic way to find the inverse.

Corollary: If $(a,n) = 1$, then then the multiplicative inverse of a modulo n is $a^{\phi(n)-1}$.

Last class period we also calculated $\phi(n)$ for a handful of (small) values of n. Our technique was just to write down all the numbers between 1 and n and then compute — one by one — which ones were relatively prime to n. This is an effective method, of course, but it is also pretty time consuming. We want a faster way to compute the value of $\phi(n)$. So let's try to come up with some formulas for "special values" of n.

Lemma: If p is a prime number, then $\phi(p) = p-1$.

Proof: We know that any integer $1 \leq a < p$ must be relatively prime to p because p is a prime number. Hence all $p-1$ of these numbers are relatively prime to p. Since p certainly isn't relatively prime to itself, this means that there are $p-1$ integers between 1 and p which are relatively prime to p. $\square$

Lemma: If p is a prime number, then $\phi(p^a) = p^a-p^{a-1}$.

Proof: Notice that any number n for which $(n,p^a) > 1$ must be a multiple of p. Hence if we can list the multiples of p which fall between 1 and $p^a$, we'll have a complete list of elements in that range which aren't relatively prime to $p^a$; this will then let us compute home many numbers are relatively prime to $p^a$ by just subtracting these "bad elements" from the total number of integers between 1 and $p^a$.

So let's list the multiples of p:

\begin{align} p,2p,3p,\cdots,p^{a-1}p. \end{align}

We can see that there are $p^{a-1}$ such elements, and so the total number of integers between 1 and $p^a$ which aren't relatively prime to $p^a$ is $p^{a-1}$. This leaves $p^a-p^{a-1}$ elements which are relatively prime to $p^a$, and this gives us $\phi(p^a)$. $\square$

Lemma: If p and q are distinct prime numbers, then $\phi(pq) = (p-1)(q-1) = \phi(p)\phi(q)$.

Proof: We'll proceed like last time, noting that numbers which aren't relatively prime to $pq$ are either multiples of p or multiples of q. This means that the numbers which aren't relatively prime sit in the lists:

\begin{align} \begin{split} p,2p,3p,\cdots,qp &\quad \longleftarrow \mbox{multiples of }p\\ q,2q,3q,\cdots,qp &\quad \longleftarrow \mbox{multiples of }q. \end{split} \end{align}

The total number of terms in both lists is clearly $q+p$, but we need to account for overlap. For this, notice that elements which are in both lists are common multiples of p and q. We already know that the least common multiple of p and q is pq, and so the only element common to both lists is qp. Hence the total number of distinct elements between the two lists is $p+q-1$. This means that, in total, we have

\begin{equation} pq - (p+q-1) = pq - p - q + 1 = (p-1)(q-1) \end{equation}

elements which are relatively prime to pq. The fact that this number is the same as $\phi(p)\phi(q)$ follows from our formula for $\phi$ evaluated at primes. $\square$

Refining Our Calculations

Now that we have some rules for calculating $\phi$, we can evaluate this function for lots of integers. For instance, we get

n $\phi(n)$
1 1
2 1 = 2-1
3 2 = 3-1
4 2 = 42-41
5 4 = 5-1
6 2 = (2-1)(3-1)
7 6 = 7-1
8 4 = 23-22
9 6 = 32-31
10 4 = (2-1)(5-1)
11 10 = 11 - 1

Notice, that when we get to $12 = 2^2 \cdot 3$, however, we no longer have a clean formula for calculating $\phi(12)$. We can list the numbers between 1 and 12 easily enough and test for relative primality, however, and when we do so we see that $\phi(12) = 4$. Notice we have

\begin{align} \phi(12) = \phi(4)\phi(3) \end{align}

but that

\begin{align} \phi(12) \neq \phi(6)\phi(2). \end{align}

Similar calculations will verify that $\phi(18) = 6$, from which we can see that

\begin{align} \phi(18) = \phi(9)\phi(2) \end{align}

but that

\begin{align} \phi(18) \neq \phi(3)\phi(6). \end{align}

What is happening which determines which of these equations work and which don't? This behavior is explained by the following

Theorem: If n and m are relatively prime, then $\phi(nm) = \phi(n)\phi(m)$.

Before we prove this theorem, though, note that it tells us that for an integer n which factors as $n = p_1^{a_1} \cdots p_k^{a_k}$ we get

\begin{align} \begin{split} \phi(n) &= \phi\left(\prod_{i=1}^k p_1^{a_1}\cdots p_k^{a_k}\right)\\ &= \phi\left(p_1^{a_1}\right)\cdots\phi\left(p_k^{a_k}\right)\\ &= (p_1^{a_1}-p_1^{a_1-1})\cdots(p_k^{a_k}-p^{a_k-1})\\ &= p_1^{a_1-1}\cdots p_k^{a_k-1} (p_1-1)\cdots (p_k-1)\\ &= p_1^{a_1}\cdots p_k^{a_k} \left(1-\frac{1}{p_1}\right)\cdots \left(1-\frac{1}{p_k}\right)\\ &= n \prod_{i=1}^k \left(1-\frac{1}{p_i}\right). \end{split} \end{align}

Now we'll move on to think about the proof of the theorem. For this, we'll need the following

Lemma: Suppose that $(n,m)=1$ and that a satisfies $1 \leq a \leq nm$. Then there exists unique integers $a_1,a_2$ with $1 \leq a_1 \leq n$ and $1 \leq a_2 \leq m$ so that a is a solution to the simultaneous congruences

$x \equiv a_1 \mod{n}$
$x \equiv a_2 \mod{m}.$

Moreover, we have that $(a,nm) > 1$ if and only if $(a_1,n) > 1$ or $(a_2,m) > 1$.

Proof: The first part of the theorem is just the Chinese Remainder Theorem, so there's nothing to prove there. For the second, we'll start by assuming that $(a,nm)>1$, and we'll show that either $(a_1,n) > 1$ or $(a_2,m) > 1$. Now let d be a common divisor of a and nm, and let p be a prime divisor of d. Then we have that p is a common divisor of both a and nm. Since $p \mid nm$ we know by Euclid's Lemma that either $p \mid n$ or $p \mid m$. Hence we have either p is a common divisor of a and n or p is a common divisor of a and m.

Assume we're in the former case. Now because $a \equiv a_1 \mod{n}$ there exists some $r \in mathbb{Z}$ so that $a - a_1 = rn$. Hence we have $a_1 = a-rn$. Since p divides both a and n we therefore have $p \mid a_1$, and so p is a common divisor of both $a_1$ and $n]]$. Hence we have [[$(a_1,n) \geq p > 1$, as desired. In the case we haven't considered — that p is a common divisor of a and m — we can use a similar argument to prove $(a_2,m) \geq p > 1$. Hence we get the desired conclusion.

Now let's prove the opposite direction, so that we want to prove that if either $(a_1,n) > 1$ or $(a_2,m) > 1$ holds, then we have $(a,nm) > 1$ as well. For this, assume we're in the case where $(a_1,n) > 1$. Then we have a common divisor $d>1$ of both $a_1$ and $n$. Because $a = rn + a_1$, this means that d is a common divisor of a as well. Hence we have $(a,n) \geq d$, from which we conclude $(a,nm) \geq d> 1$, as desired. In the remaining case — where $(a_2,m) > 1$ — a similar argument shows that $(a,m) > 1$, from which $(a,nm) > 1$ is easy. $\square$

With this lemma in hand, we can go on to prove our big theorem.

Proof that $phi(nm) = \phi(n)\phi(m)$ whenever $(n,m) = 1$

To prove the result, the previous result says that a number a with $1 \leq a \leq nm$ is represented uniquely by numbers $a_1,a_2$ with $1 \leq a_1 \leq n$ and $1 \leq a_2 \leq m$. Hence we ask the following question: for which choices of $a_1,a_2$ can we conclude that a is relatively prime to nm? Well, we know that $(a,nm) = 1$ if and only if both $(a_1,n) = 1$ and $(a_2,m) = 1$. The number of choices $a_1$ which satisfy $(a_1,n) = 1$ is precisely $\phi(n)$, since $\phi(n)$ counts — by definition — the number of elements between 1 and n which are relatively prime to n. Similarly, we know that the number of choices of $a_2$ so that $(a_2,m) = 1$ is given by $\phi(m)$.

Since our choices for $a_1$ and $a_2$ can be made independent of each other, this means that we have a total of $\phi(n)\phi(m)$ choices for $a_1,a_2$ which produce an integer relatively prime to nm. Hence we conclude $\phi(nm) = \phi(n)\phi(m)$. $\square$

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License