7.5: The Euler phi-Function
After reading the two previous sections, you're probably wondering why we stated the Principle of Inclusion-Exclusion in such an abstract way, as in those examples \(N(S)\) depended only on the size of \(S\) and not its contents. In this section, we produce an important example where the value of \(N(S)\) does depend on \(S\). Nevertheless, we are able to make a reduction to obtain a useful end result. In what follows, let \(\mathbb{N}\) denote the set of positive integers.
For a positive integer \(n \geq 2\), let
\(\phi(n)=|\{m \in \mathbb{N}:m \leq n,gcd(m,n)=1\}|\).
This function is usually called the Euler \(\phi\) function or the Euler totient function and has many connections to number theory. We won't focus on the number-theoretic aspects here, only being able to compute \(\phi(n)\) efficiently for any \(n\).
For example, \(\phi(12)=4\) since the only numbers from \(\{1,2,…,12\}\) that are relatively prime to 12 are 1, 5, 7 and 11. As a second example, \(\phi(9)=6\) since 1, 2, 4, 5, 7 and 8 are relatively prime to 9. On the other hand, \(\phi(p)=p−1\) when \(p\) is a prime. Suppose you were asked to compute \(\phi(321974)\). How would you proceed?
In Chapter 3 we discussed a recursive procedure for determining the greatest common divisor of two integers, and we wrote code for accomplishing this task. Let's assume that we have a function
gcd(m,n)
that returns the greatest common divisor of the integers
m
and
n
. (Conveniently enough, SageMath comes such a function built in.) Then we can calculate \(\phi(n)\) with this code snippet:
//code
Running the code above answers almost immediately that \(\phi(321974)=147744\). (As usual, in the web version of the text, you can change the value
321974
to calculate the value of \(\phi\) for other integers. However, if you try to increase the value of
n
to be too large, you may run into memory issues imposed by the Sage Cell Server used by the text. For instance, attempting to calculate \(\phi(319572943)\) results in an error at the time of writing. (You may have better luck running the code directly in the
SageMath Cloud
or a local installation of SageMath.)
Given these difficulties, how could we find \(\phi(1369122257328767073)\)?
Clearly, the program is useless to tackle this beast! It not only iterates \(n−2\) times but also invokes a recursion during each iteration. Fortunately, Inclusion-Exclusion comes to the rescue.
Let \(n \geq 2\) be a positive integer and suppose that \(n\) has \(m\) distinct prime factors: \(p_1, p_2,…,p_m\) . Then
\(\phi(n) = n\displaystyle \prod_{i=1}^m \dfrac{p_i-1}{p_i}\)
Our proof of Theorem 7.14 requires the following elementary proposition whose proof we leave as an exercise.
Let \(n \geq 2, k \geq 1\) , and let \(p_1,p_2,…,p_k\) be distinct primes each of which divide \(n\) evenly (without remainder). Then the number of integers from \(\{1,2,…,n\}\) which are divisible by each of these \(k\) primes is
\(\dfrac{n}{p_1p_2 \cdot \cdot \cdot p_k}\)
- Proof
-
We present the argument when \(m=3\). The full result is an easy extension.
In light of Proposition 7.15 , the Principle of Inclusion-Exclusion yields:
\(\phi(n) = n - (\dfrac{n}{p_1} + \dfrac{n}{p_2} + \dfrac{n}{p_3}) + (\dfrac{n}{p_1p_2} + \dfrac{n}{p_1p_3} + \dfrac{n}{p_2p_3}) - \dfrac{n}{p_1p_2p_3}\)
\( = n \dfrac{p_1p_2p_3 - (p_2p_3 + p_1p_3 + p_1p_2) + (p_3+p_2+p_1) -1}{p_1p_2p_3}\)
\( = n \dfrac{p_1 -1}{p_1} \dfrac{p_2 - 1}{p_2} \dfrac{p_3 - 1}{p_3}\).
SageMath reports that
\(1369122257328767073=(3)^3(11)(19)^4(31)^2(6067)^2\)
is the factorization of \(1369122257328767073\) into primes. It follows that
\(\phi(1369122257328767073)=1369122257328767073\) \(\dfrac{2}{3} \dfrac{10}{11} \dfrac{18}{19} \dfrac{30}{31} \dfrac{6066}{6067}\).
Thus SageMath quickly reports that
\(\phi(1369122257328767073)=760615484618973600\).
Amanda and Bruce receive the same challenge from their professor, namely to find \(\phi(n)\) when
\(n=314849727861997688894791078609643681715439846090179313900192215985166853104070853972232932490281335924101693211209710523\).
However the Professor also tells Amanda that \(n=p_1p_2\) is the product of two large primes where
\(p_1=470287785858076441566723507866751092927015824834881906763507\)
and
\(p_2=669483106578092405936560831017556154622901950048903016651289\).
Is this information of any special value to Amanda? Does it really make her job any easier than Bruce's? Would it level the playing field if the professor told Bruce that \(n\) was the product of two primes?