Bite-Sized Mathematics for Computer Science

Notes on MIT's 6.042J

Eda Eren
Table of Contents

Introduction

Prerequisites & Resources

Unit 1: Proofs

00. Introduction to Proofs

01. (Two) Proof Methods

02. The Well Ordering Principle

03. Logic & Propositions

04. Quantifiers and Predicate Logic

05. Sets

06. Binary Relations

07. Induction

08. State Machines — Invariants

09. Recursive Definitions

10. Infinite Sets

Unit 2: Structures

11. GCDs

12. Congruences

14. RSA Encryption

15. Digraphs: Walks and Paths

16. Directed Acyclic Graphs

17. Partial Orders and Equivalence

18. Degrees & Isomorphism

19. Coloring & Connectivity

20. Trees

21. Stable Matching

Unit 3: Counting

22. Sums & Products

23. Asymptotics

24. Counting with Bijections

25. Repetitions & Binomial Theorem

26. Pigeonhole Principle, Inclusion-Exclusion

Unit 4: Probability

27. Intro to Discrete Probability

28. Conditional Probability

29. Independence & Causality

30. Random Variables, Density Functions

31. Expectation

32. Deviation: Markov & Chebyshev Bounds

33. Sampling & Confidence

34. Random Walks & PageRank

Euler's Theorem

We're going to briefly look at Euler's totient function, ϕ(n)\phi(n).

It is defined as:

For n>0n \gt 0,

ϕ(n)=the number of integers in [0, n), that are relatively prime to n\phi(n) = \text{the number of integers in } [0, \ n), \text{ that are relatively prime to } n

Note: We can also use the interval (0, n)(0, \ n) because 00 is not relatively prime to anything.

In other words, ϕ(n)\phi(n) is the number of all positive integers up to nn that are relatively prime to nn.

We can also say that if kk and nn are relatively prime, then gcd(k, n)=1\text{gcd}(k, \ n) = 1.
As we've seen in the previous section, it also means that it'll be the numbers that have inverses and are cancellable mod(n)\text{mod}(n).

Let's call this set Zn\mathbb{Z}^*_n, and write it as: {k[0, n)  gcd(k, n)=1}\{k \in [0, \ n) \ | \ \text{gcd}(k, \ n) = 1\}.

Then, ϕ(n)=Zn\phi(n) = |\mathbb{Z}^*_n|.

A simple example is ϕ(7)=6\phi(7) = 6.
All 66 positive integers up to 77 ({1, 2, 3, 4, 5, 6}\{1, \ 2, \ 3, \ 4, \ 5, \ 6\}) are relatively prime to 77.
Another example is ϕ(12)=4\phi(12) = 4. All positive integers that are relatively prime to 1212 in that interval are 1, 5, 71, \ 5, \ 7, and 1111.


If pp is prime, everything in [1, p)[1, \ p) is relatively prime to pp.
Therefore, when pp is prime, ϕ(p)=p1\phi(p) = p - 1, as we've seen with ϕ(7)=6\phi(7) = 6.

Let's look at another example, ϕ(9)\phi(9).
We can also write it as a power of a prime, ϕ(32)\phi(3^2).

Here is the reasoning: a number is relatively prime to 99 iff it is relatively prime to 33. Because 33 divides every third number in that interval (11, 22, 3, 44, 55, 6, 77, 88), every third number will not be relatively prime to 99.
So, it will be the set of all numbers minus 13\frac{1}{3} of 99:
ϕ(9)=99(13)=6\phi(9) = 9 - 9(\frac{1}{3}) = 6.

We can generalize, and say that pkp\frac{p^k}{p} of the numbers in that interval will not be relatively prime to pkp^k.
Therefore, ϕ(pk)=pkpkp\phi(p^k) = p^k - \frac{p^k}{p}.
To write it more simply:

ϕ(pk)=pkpk1\phi(p^k) = p^k - p^{k - 1}

What if the number we're dealing with is not a power of a prime, like 1212 as we've just seen?

One fact is that if aa and bb are relatively prime, then

ϕ(ab)=ϕ(a)ϕ(b)\phi(ab) = \phi(a)\phi(b)

This is called multiplicativity; a function is multiplicative when its value at a product of relatively prime numbers is the product of the values at those two relatively prime numbers.
Again, if English is confusing, here is the same thing put more precisely: f(ab)=f(a)f(b)f(ab) = f(a) f(b).

That means ϕ\phi is a multiplicative function.

Okay, let's go back to ϕ(12)\phi(12). We can now write it as ϕ(34)\phi(3 \cdot 4). That also means, ϕ(12)=ϕ(3)ϕ(4)\phi(12) = \phi(3) \cdot \phi(4).
Our job is easy, 33 is a prime already, and we can write 44 as a power of a prime, 222^2.
If we use the formulas we've just seen, then we can write the whole thing as:
(31)(22221)(3 - 1) \cdot (2^2 - 2^{2 - 1}).
That means 2(42)2 \cdot (4 - 2), which is 44.
So, ϕ(12)=4\phi(12) = 4 — the size of the set {1, 5, 7, 11}\{1, \ 5, \ 7, \ 11\}. And, we're done.

Isn't it elegant?

So, to summarize, aside from ϕ(p)=p1\phi(p) = p - 1 when pp is a prime, we've seen two other rules:

If pp is a prime, then

ϕ(pk)=pkpk1 for k1\phi(p^k) = p^k - p^{k-1} \text{ for } k \geq 1

If aa and bb are relatively prime, then

ϕ(ab)=ϕ(a)ϕ(b)\phi(ab) = \phi(a)\phi(b)


Let's look at a wonderful thing.

We used ϕ(7)\phi(7) example, the result was 66 as it was the size of the set of those that are relatively prime to 77: {1, 2, 3, 4, 5, 6}\{1, \ 2, \ 3, \ 4, \ 5, \ 6\}.

We called this set Zn\mathbb{Z}^*_n.
Now, if we take any number kk from Zn\mathbb{Z}^*_n, and multiply it by each number in Zn\mathbb{Z}^*_n, we'll still get the same set (mod n\text{mod }n), but in a different order:

Zn=kZn for kZn\mathbb{Z}^*_n = k \mathbb{Z}^*_n \text{ for } k \in \mathbb{Z}^*_n

So, in our example, Z7\mathbb{Z}^*_7 is {1, 2, 3, 4, 5, 6}\{1, \ 2, \ 3, \ 4, \ 5, \ 6\}.
Let's pick any kk, for example, 55.
Multiplying each item in Z7\mathbb{Z}^*_7, we have {5, 10, 15, 20, 25, 30}\{5, \ 10, \ 15, \ 20, \ 25, \ 30\}, and of course, since it has to be in mod(7)\text{mod}(7), it is the set {5, 3, 1, 6, 4, 2}\{5, \ 3, \ 1, \ 6, \ 4, \ 2\}. It is the same set!

We can try it with a different number in the set, say 22.
This time we have {2, 4, 6, 8, 10, 12}\{2, \ 4, \ 6, \ 8, \ 10, \ 12\}, and since this is Z7\mathbb{Z}^*_7, it is {2, 4, 6, 1, 3, 5}\{2, \ 4, \ 6, \ 1, \ 3, \ 5\}. The same set again!


In terms of congruence, we can also say that if kk and nn are relatively prime, then:

kϕ(n)1   mod(n)k^{\phi(n)} \equiv 1 \ \ \ \text{mod}(n)

Let's say that kk is 22, and nn is 55.

2ϕ(5)1   mod(5)2^{\phi(5)} \equiv 1 \ \ \ \text{mod}(5)

Those that are relatively prime to 55 less than 55 is the set {1, 2, 3, 4}\{1, \ 2, \ 3, \ 4\}. Its size is 44, so ϕ(5)=4\phi(5) = 4:

241   mod(5)2^4 \equiv 1 \ \ \ \text{mod}(5)

And, it is correct:

161   mod(5)16 \equiv 1 \ \ \ \text{mod}(5)


Fermat's Little Theorem states,

kp11  mod(p)k^{p - 1} \equiv 1 \ \ \text{mod}(p)

So, for example, the remainder of dividing 247824^{78} by 7979 will be 11 — that is, rem(2478, 79)=1\text{rem}(24^{78}, \ 79) = 1, or 24781  mod(79)24^{78} \equiv 1 \ \ \text{mod}(79).


For primes pp and qq,

ϕ(pq)=(p1)(q1) for primes pq\phi(pq) = (p - 1)(q - 1) \text{ for primes } p \neq q

A simple example, let's say pp is 22, and qq is 55.
So, this has to be true: ϕ(10)=14\phi(10) = 1 \cdot 4.

The set of those relatively prime to 1010 that are less than 1010 is {1, 3, 7, 9}\{1, \ 3, \ 7, \ 9\}.
Indeed, ϕ(10)=4\phi(10) = 4.


An example. We're asked

How many numbers between 11 and 37803780 (inclusive) are relatively prime to 37803780?

We know that 37803780 is not a prime. One thing to do is to either write 37803780 as a power of a prime, or write it as the product of two numbers that are relatively prime.
We can write it as the product of 77 and 540540.
Then, we can solve this using the gcd function we used before, and newly defined ring and phi functions:

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def ring(n):
    return [i for i in range(1, n) if gcd(i, n) == 1]


def phi(n):
    return len(ring(n))


a = 7
b = 540

print(phi(a) * phi(b)) # -> Answer: 864

The answer is indeed 864864.