\ie\cf
\newcommand{\eqdef}{\mathbin{\stackrel{\rm def}{=}}}  \newcommand{\R} % real numbers  \newcommand{\N}} % natural numbers  \newcommand{\Z} % integers  \newcommand{\F} % a field  \newcommand{\Q} % the rationals  \newcommand{\C}{\mathbb{C}} % the complexes  \newcommand{\poly}}  \newcommand{\polylog}}  \newcommand{\loglog}}}  \newcommand{\zo}{\{0,1\}}  \newcommand{\suchthat}  \newcommand{\pr}[1]{\Pr\left[#1\right]}  \newcommand{\deffont}{\em}  \newcommand{\getsr}{\mathbin{\stackrel{\mbox{\tiny R}}{\gets}}}  \newcommand{\Exp}{\mathop{\mathrm E}\displaylimits} % expectation  \newcommand{\Var}{\mathop{\mathrm Var}\displaylimits} % variance  \newcommand{\xor}{\oplus}  \newcommand{\GF}{\mathrm{GF}}  \newcommand{\eps}{\varepsilon}  \notag
\no
Back to MB3

Week 8 Notes

100%

This week's notes are about one of the most important topics in all of mathematics. It really is worth taking the time to understand it well.

Modular Arithmetic

Consider a standard clock, but with the \(12\) replaced by a \(0\).

What time will the clock point to in \(1\) hour? How about \(2\) hours?

Starting at noon, we can see that the hour hand will point to $$1,2,3,4,5,6,7,8,9,10,11,0,1,2,3,4,5,6,7,8,9,10,11,0,\ldots$$in order. If we went backwards in time, the hour hand would go $$11,10,9,8,7,6,5,4,3,2,1,0,11,\ldots$$

This is the way we count in mod \(12\). When we add \(n\), an integer, to \(k\), a number on our clock, we forward time (or go back in time if \(n\) is negative) by \(n\) hours, and see what number the clock will point to if it started at \(k\).

As you might guess, adding \(n+12\) to the clock is the same as adding \(n\) to the clock, since the clock doesn't measure how many rotations it has undergone. Because of this, we say that \(n+12\equiv n\bmod{12}\), or in English, \(n+12\) is equivalent to \(n\) modulo \(12\). All this says is that if I move time ahead by \(n\), it is the same as moving the clock ahead by \(n+12\).

So when are two integers equivalent? They are equivalent when their difference is a multiple of \(12\), since the hour hand points to the same number. In fact, this is how we will define mod.

Given some integer \(n\gt1\), which we call the modulus, and integers \(a,b\), we say that \(a\) is equivalent to \(b\) mod \(n\), or \(a\equiv b\bmod n\), iff \(a-b\) is a multiple of \(n\).

So by this definition, \(-13\equiv-1\equiv11\equiv23\bmod{12}\). Now, it is the case that every integer \(x\) is equivalent to exactly one non-negative integer \(r\) which is less than the modulus \(n\). We call \(r\) the remainder when \(x\) is divided by \(n\). \(r\) is also called the residue of \(x\) (with \(n\) implicit from the context). In the case of mod \(12\), there are \(12\) possible remainders: \(0,1,\ldots,11\).

Given \(x\) and \(n\), find the remainder of \(x\) when divided by \(n\).
  • \(x=12,\;n=5\)
  • \(x=3,\;n=25\)
  • \(x=-4,\;n=3\)


  • 2
  • 3
  • 2

Note that two numbers are equivalent exactly when their remainders are the same. Are you getting the hang of mods yet? Here are some (obvious) properties of equivalence (sometimes also called congruence)

Addition

Part of what makes modular arithmetic so useful is that it behaves really well with addition. In particular, we have the following result:

Given \(n\gt1\) as the modulus, and integers \(a,b,c,d\) with \(a\equiv c\bmod n\) and \(b\equiv d\bmod n\), then $$a+b\equiv c+d\bmod n$$


Since \(a\equiv c\bmod n\) and \(b\equiv d\bmod n\), there exists integers \(j,k\) such that \(a-c=kn\), \(b-d=jn\). So, \(a+b-c-d=(k+j)n\), and thus \(a+b\equiv c+d\bmod n\).
Suppose I had a problem that said, here's some complicated expression, find the last \(n\) digits of it. This is equivalent to saying, find the remainder when the complicated expression is divided by \(10^n\). Or in other words, find the complicated expression mod \(10^n\).

Compute the units digit of $$5811+23424+22+555$$How would you solve this problem? One approach might be to just add the numbers up: that seems rather painful, though. Instead, we observe that finding the units digit is just finding the sum \(\bmod{10}\). We know that \(5811\equiv1\bmod{10}\), \(23424\equiv4\bmod{10}\), \(22\equiv2\bmod{10}\), and \(555\equiv5\bmod{10}\). So, $$5811+23424+22+555\equiv1+4+2+5\equiv2\bmod{10}$$This gives us our answer.

Is \(124+55+3211+332\) a multiple of \(3\)? To solve this, we consider the numbers mod \(3\). We get $$124+55+3211+332\equiv1+1+1+2\equiv2\bmod3$$Since a number is a multiple of \(3\) if and only if it is equivalent to \(0\) mod \(3\), this sum is not a multiple of \(3\).

Mods also work well with subtraction, as we show in the next theorem.

For some modulus \(n\), and integers \(a,b,c,d\) such that \(a\equiv c\), \(b\equiv d\), we have $$a-b\equiv c-d\bmod n$$


Since \(a\equiv c\) and \(b\equiv d\), there exists integers \(j,k\) such that \(a-c=jn\), \(b-d=kn\). So, $$(j-k)n=(a-c)-(b-d)=(a-b)-(c-d)$$Thus, \(a-b\equiv c-d\bmod n\).

So, when we have equivalences, we can add and subtract to either side without worry.

Multiplication

We just learned that mods are well-behaved with addition and subtraction. What about multiplication?

The answer is still yes! This is what truly lends power to modular arithmetic, but we'll see that with some examples.

For some modulus \(n\), and integers \(a,b,c,d\) such that \(a\equiv c\), \(b\equiv d\), we have $$a\cdot b\equiv c\cdot d\bmod n$$


Since \(a\equiv c\) and \(b\equiv d\), there exists integers \(j,k\) such that \(a-c=jn\), \(b-d=kn\). So, we have \(ab-cb=bjn\) and \(cb-cd=ckn\). Thus, $$bjn+ckn=(ab-cb)+(cb-cd)=ab-cd$$But, \(bjn+ckn=n(bj+ck)\), so \(ad\equiv cd\bmod n\).
Find the remainder when the following product is divided by \(3\): $$124\cdot134\cdot23\cdot49\cdot235\cdot13$$To solve this, we simply replace each number by its residue. $$1\cdot2\cdot2\cdot1\cdot1\cdot1\equiv4\equiv1\bmod3$$So the remainder is \(1\).

Since exponentiation is repeated multiplication, it too is preserved by mods. In particular, if \(a\equiv b\bmod n\), then for any non-negative integer \(k\), \(a^k\equiv b^k\bmod n\). This is a very important observation in a common class of problems, an example of which is presented below.

What are the last two digits of \(7^{2021}\)? Well, \(7^2\equiv49\bmod{100}\), \(7^3\equiv 43\bmod{100}\), and \(7^4\equiv1\bmod{100}\). Aha! $$7^{2021}=7\cdot7^{2020}=7\cdot(7^4)^{505}$$$$\equiv7\cdot1^{505}\equiv7\bmod{100}$$So, the last two digits are \(07\).

These sorts of "find the remainder when \(a^b\) is divided by \(c\)" problems show up all the time on competitions. In the Challenge Section, I'll present a foolproof way to solving these problems.

Division

Suppose I know that \(ka\equiv kb\bmod n\). Do I know that \(a\equiv b\bmod n\)? On a cursory glance, you might say, of course! Just divide both sides by \(k\).

The issue here is that I don't know what it means to divide by \(k\)! What is \(3\) divided by \(5\)? \(0.6\)? But \(0.6\) is not a valid value in our mod system.

So what does it mean to divide by \(k\)? Well, it means to multiply by \(\frac1k\). \(\frac1k\) is the number such that \(k\cdot\frac1k=1\).

Our question becomes, how do we know such a \(\frac1k\) exists? The answer is, it doesn't always exist. However, if \(\gcd(k,n)=1\), it does exist. This is summarized in the hugely important lemma below.

For any non-zero \(n\) and modulus \(n\), there exists integer \(q\) such that \(qn\equiv\gcd(n,m)\bmod m\).


If such a \(q\) existed, by modulus properties, there'd exist an integer \(r\) such that \(qn+rm=\gcd(n,m)\). Thus, we seek to show that such a \(q,r\) exist.

Let \(n_1=\frac n{\gcd(n,m)}\) and \(m_1=\frac m{\gcd(n,m)}\). So, \(\gcd(n_1,m_1)=1\). Now suppose there exists integers \(0\leq a\lt b\lt k_1\) such that \(m_1a\equiv m_1b\bmod{n_1}\). Then, \(m_1(b-a)\) is a multiple of \(n_1\).

Since \(\gcd(m_1,n_1)=1\), this means that \(b-a\) is a multiple of \(n_1\). Since \(b-a\) is positive, this means that \(b-a\geq n_1\), a contradiction since \(b-a\lt b\lt n_1\).

So, we know that every \(m_1r\) for \(0\leq r\lt n_1\) has a different remainder mod \(n_1\). By the pigeonhole principle, since there are only \(n_1\) possible remainders, there must be some \(r\) for which \(m_1r\equiv1\bmod{n_1}\). That implies that there exists integer \(q\) such that \(rm_1+qn_1=1\).

Prove that the following expression is an integer for all integers \(n\geq m\geq1\)$$\frac{\operatorname{gcd}(m,n)}n{n\choose m}$$


By Bezout's Lemma, there exists integers \(x,y\) such that \(mx+ny=\operatorname{gcd}(m,n)\). Thus, $$\frac{\operatorname{gcd}(m,n)}n{n\choose m}=\frac{mx+ny}n{n\choose m}=x\cdot\frac mn{n\choose m}+y{n\choose m}=x{n-1\choose m-1}+y{n\choose m}$$Clearly, this is an integer.

If this proof was too complicated, don't worry. You won't need it for any competitions. But, for those of you who understood the proof, you'll notice that this lemma gives us no way of finding \(\frac1k\) when \(\gcd(k,n)=1\). So how do we find \(\frac1k\)?

The "easiest" way to find \(\frac1k\) is to use the Extended Euclidean Algorithm, although in most cases, trial and error can be faster than this algorithm.

Find integers \(x,y\) such that $$102\cdot x+38\cdot y=\operatorname{gcd}(102,38)$$


First, we apply the Euclidean algorithm to find the gcd of \(102\) and \(38\).$$\begin{aligned}102&=2\times38+26\\38&=1\times26+12\\26&=2\times12+2\\12&=6\times2+0\end{aligned}$$With this, we know that \(\operatorname{gcd}(102,38)=2\). Now, rearrange the equations to isolate the remainders$$\begin{aligned}26&=102-2\times38\\12&=38-1\times26\\2&=26-2\times12\end{aligned}$$Now, we start with the bottom equation, replacing any occurrence of \(12\) or \(26\) with the right sides of their corresponding equations.$$\begin{aligned}2&=26-2\times12\\&=(102-2\times38)-2\times(38-26)\\&=102-2\times38-2\times(38-(102-2\times38))\\&=102-2\times38+2\times102-6\times38\\&=3\times102-8\times38\end{aligned}$$

Thus, we have the following conclusion: if \(ka\equiv kb\bmod n\) and \(\gcd(k,n)=1\), then \(a\equiv b\bmod n\). In essence, this is a limited form of division.

\(\;ka\equiv kb\) does not always imply \(a\equiv b\), even if \(k\neq0\). For example, if \(n=25\), \(k=5\), then \(5\cdot 5\equiv0\) and \(5\cdot10\equiv0\), but \(5\neq10\).

Chinese Remainder Theorem

One of the most important implications of Bezout's Lemma is the Chinese Remainder Theorem. Since it is a large theorem, we will show it in parts. Assume that \(m_1,\ldots,m_r\) are moduli such that for any \(1\leq i\lt j\leq r\), \(\gcd(m_i,m_j)=1\) (we call these numbers pairwise relatively prime). Let \(a_1,\ldots, a_r\) be integers.

Suppose integers \(c,d\) satisfy the modulo equivalences $$x\equiv a_1\bmod{m_1}$$$$\vdots$$$$x\equiv a_r\bmod{m_r}$$Then, \(c\equiv d\bmod{m_1\cdot\ldots\cdot m_r}\)


Since \(c\equiv d\bmod{m_i}\) for each \(i\), \(c-d\) is a multiple of \(m_i\) for each \(i\). Since the moduli are all pairwise relatively prime, this implies that \(c-d\) is a multiple of \(m_1\cdot\ldots\cdot m_r\), so $$c\equiv d\bmod{m_1\cdot\ldots\cdot m_r}$$

This shows that mod \(M=m_1\cdot\ldots\cdot m_r\), there is at most one solution to the system of modulo equivalences. Now, we will show that there is a solution.

There exists an integer \(x\) such that \(x\equiv a\bmod n\), \(x\equiv b\bmod m\) for moduli \(n,m\) with \(\gcd(n,m)=1\), and integers \(a,b\).


By Bezout's Lemma, there exists an integer \(q\) such that \(qn\equiv1\bmod m\). Let \(x=a+(b-a)qn\). \(x\equiv a\bmod n\) trivially, and $$a+(b-a)jn\equiv a+(b-a)\cdot1\equiv b\bmod m$$
There exists an integer \(y\) such that \(x\equiv a_i\bmod{m_i}\) for each \(1\leq i\leq r\).


We prove the claim by induction on \(r\). For our base case, the Weakened CRT Theorem showed that this is the case when \(r\leq 2\).

For the inductive case, suppose that \(x\) is an integer that satisfies \(x\equiv a_i\bmod{m_i}\) for each \(1\leq i\lt r\). By weakened CRT, there exists an integer \(y\) such that \(y\equiv x\bmod{m_1\cdot\ldots\cdot m_{r-1}}\) and \(y\equiv a_r\bmod{m_r}\). I claim that for all \(1\leq i\lt r\), \(y\equiv a_i\bmod m_i\).

Since \(y\equiv x\bmod{m_1\cdot\ldots\cdot m_{r-1}}\), \(y-x\) is a multiple of this modulus, and thus, \(y-x\) is a multiple of \(m_i\). So, \(y\equiv x\equiv a_i\bmod{m_i}\). This completes the proof.

To summarize, this is the full statement of the Chinese Remainder Theorem.

There exists a unique \(0\leq x\lt M\) such that for each \(1\leq i\leq r\), \(x\equiv a_i\bmod{m_i}\).

How do you find this \(x\)? Well, if we follow the proof, we need apply Bezout's Theorem \(r-1\) times, and thus do the Extended Euclidean Algorithm \(r-1\) times. If \(r\) is small or the moduli are small, this is reasonable to do. Otherwise, guess and check is your friend.

Find an integer \(x\) that satisfies $$2x\equiv 5\bmod7$$$$3x\equiv4\bmod8$$Note that \(\frac12\) in \(\bmod7\) is \(4\), and \(\frac13\) in \(\bmod8\) is \(3\). So we have $$x\equiv10\equiv3\bmod7$$$$x\equiv12\equiv4\bmod8$$Thankfully, we don't need to apply the Extended Euclidean Algorithm here, since we can see that \(8-7=1\). So by CRT, \(x=3+(-7)=-4\) is a solution. The remainder when \(-4\) is divided by \(7\cdot8=56\) is \(52\), so that is our final answer.

Challenge: Totient Theorem

One of Leonhard Euler's many lasting contributions to mathematics is his Totient Function. It is defined as follows for positive integer \(n\): $$\varphi(n)=\text{Number of positive integers } m\lt n\text{ such that }\gcd(m,n)=1$$

For example, \(\varphi(4)=2\), since \(1,3\) are the two numbers below \(4\) relatively prime with \(4\).

How do we compute this for large inputs? We can use the Chinese Remainder Theorem!

Given \(m,n\) positive integers such that \(\gcd(m,n)=1\), show that \(\varphi(m)\cdot\varphi(n)=\varphi(mn)\) via the Chinese Remainder Theorem.

Thus, for a positive integer \(n\), we can prime factorize it as \(p_1^{e_1}\cdot\ldots\cdot p_k^{e_k}\). So, we have $$\varphi(n)=\prod\limits_{i=1}^k\varphi(p_i^{e_i})$$Note that $$\varphi(p_i^{e_i})=p_i^{e_i}\cdot\left(1-\frac1{p_i}\right)$$Since there are \(p_i^{e_i-1}\) positive integers below \(p_i^{e_i}\) that have \(p_i\) as a factor.

So, our final formula for the Totient Functions is $$\varphi(n)=n\cdot\prod\limits_{i=1}^k\left(1-\frac1{p_i}\right)$$

Now why do we care about the Totient Function? Well, I promised you earlier that I'd provide a foolproof way of finding multiplicative inverses and solving the problems of the form "what is the remainder when \(a^b\) is divided by \(c\)". The approach depends on the following theorem.

For all moduli \(n\) and any integer \(a\), if \(\gcd(a,n)=1\), \(a^{\varphi(n)}\equiv1\bmod n\).

This is a truly beautiful result. It tells us that \(\frac1a=a^{\varphi(n)-1}\). In the case that \(n\) is prime, we have the following result as a direct implication.

Prove using Euler's Totient Theorem that for any prime \(p\) and any integer \(a\), \(a^p\equiv a\bmod p\).
What is the period of digits in the decimal expansion of \(\frac17\)?


The period of a decimal expansion is the number of digits that needs to appear before the decimal pattern starts looping. For example, the period of \(\frac1{11}\) in base \(10\) is \(2\) since the decimal is \(0.090909\ldots\). In base \(10\), the period of \(\frac1{12}\) is \(1\) since the decimal is \(0.08333\ldots\).


In base \(b\), the period of \(\frac pq\) (in simplest form) is the smallest positive integer \(k\) such that there exists a natural number \(n\) with $$b^n\equiv b^{n+k}\bmod q$$\(n\) represents the number of digits before the decimal starts repeating, and \(k\) is the period.


In our problem, we know by Fermat's Little Theorem that \(10^6\equiv10^0\bmod7\), so the period is \(6\).

To find the remainder of \(a^b\) when divided by \(n\), assuming \(\gcd(a,n)=1\), we note that \(a^{\varphi(n)}\equiv1\bmod n\), so if \(c\) is the remainder of \(b\) when divided by \(\varphi(n)\), then \(a^b\equiv a^c\bmod n\). This will simplify the problem to one we can handle.

For a final interesting result that follows from the Totient Theorem, consider the following exercise.

Prove using the Euler Totient Theorem that the greatest integer that can't be written as \(am+bn\) for some non-negative integers \(a,b\) is \(mn-m-n\).

For some context about the name of the last theorem, McDonald's sold its nuggets in boxes of \(9\) and \(20\). So people wondered, what is the largest amount of nuggets that is impossible to get. This sort of problem does occasionally appear on MathCounts.

Top