Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

ExTeX --- Modular Arithmetic

Score:0/0/0
Actions:

Modular Arithmetic

Math Refresher 5, Bernd Gärtner(12 July, 2023)

1Around the clock

23+223+2 is not always 2525. Think of the 24‐hour clock. When it’s 23:00, then two hours later, it will be 1:00, not 25:00. This is because the clock only counts hours from 00 to 2323, and after that, it starts over from 00.

This is exactly the idea of modular arithmetic. The term sounds fancy, but it really works just like a clock, except that there is no reason for a mathematician to restrict attention to 24‐hour clocks; instead, modular arithmetic talks about mm‐hour clocks, where mm can be any positive integer. For example, a 5‐hour clock counts the hours as follows:

0, 1, 2, 3, 4, 0, 1, 2, 3, 4, ......\quad

When a 24‐hour clock is at hour 0, we know that 24 (or 48, or 72) hours later, it’s again at 0. And 36 hours later, it’s at 12, and 25 hours later, it’s at 1.

When an mm‐hour clock is at hour 00, where is it nn hours later? For this, we introduce the notation

nmodm,\displaystyle n \bmod m,
pronounced as “nn modulo mm”. We always assume that nn and mm are integers, and that m>0m>0.

As we have previously discussed, 24mod24=024\bmod 24 = 0, and also 48mod24=048\bmod 24=0 and 72mod24=072\bmod 24=0. Furthermore, 36mod24=1236\bmod 24=12 and 25mod24=125\bmod 24=1.

In general, the nmodmn \bmod m values tell us exactly how an mm‐hour clock counts. To illustrate this, we come back to the 5‐hour clock:

n0123456789... hours laternmod50123401234... o’clock\displaystyle \begin {array}{l|r|r|r|r|r|r|r|r|r|r|l}n & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & ...\text { hours later} \\ \hline n\bmod 5 & 0 & 1 & 2 & 3 & 4 & 0 & 1 & 2 & 3 & 4 & ...\text { o'clock}\end {array}

Exercise 1.

What is 1527mod351527 \bmod 35? Do not compute the number by yourself! Instead, go through the list of possible answers and try to argue for each why it cannot be the correct answer. Based on Sherlock Holmes, whatever remains must be the truth.

Input (Single Choice)

  • 4242
  • 2222
  • 00
  • 15-15

Decide which of the provided answers is correct and select it by clicking on the corresponding box, thus marking it with a blue border like so:

  • Unselected
  • Selected
  • Unselected

Exactly one of the provided answers is correct.

2Computing modulo mm

How can we compute nmodmn\bmod m, without simulating the passing of nn hours on an mm‐hour clock? For example, what is 753mod24753\bmod 24? This is asking where a 24‐hour clock is after 753753 hours. Well, as 753753 hours are exactly 753/24=31.375753/24=31.375 days, we know that 753753 hours are 3131 full days (equal to 3124=74431\cdot 24=744 hours), plus 99 extra hours (the difference between 753753 and 744744). Since the clock is at 00 after each full day, it is therefore at 99 after the 753753 hours. So,

753mod24=9.\displaystyle 753\bmod 24 = 9.

Here is the general recipe for computing nmodmn\bmod m:

  1. Compute how many full “mm‐hour days” are passing within nn hours, by taking n/mn/m and rounding down. The mathematical symbol for this is n/m\lfloor n/m\rfloor .

  2. Multiply the result with mm to get the hours passing in these full days. The result is mn/mm \cdot \lfloor n/m\rfloor , and after this many hours, the clock is again at 00.

  3. We still have nmn/mn-m \cdot \lfloor n/m\rfloor extra hours to go, and this is also where the clock is after the total of nn hours.

So the formula is

nmodm=nmnm.\displaystyle n \bmod m = n-m \left \lfloor \frac {n}{m}\right \rfloor .
Recall the rule “multiplication and division first, then addition and subtraction.” We have also (usual practice) omitted the multiplication symbol \cdot between mm and n/m\lfloor n/m\rfloor . Also note that we need m>0m>0: a 0‐hour‐clock makes no sense, and you cannot compute n/mn/m when m=0m=0. The number nn may be 00, though.

While we’re at it: there is no reason to exclude negative values of nn. We can also ask where the clock was some hours ago. For example, (6)mod24=18(-6)\bmod 24= 18, because when the clock is now at 00, then 66 hours ago, it was at 1818. The formula still works. If we compute 6/24=0.25-6/24=-0.25 and round down, we get 1-1. Multiplying this with 2424 gives 24-24. And finally, the extra hours still to go are 6(24)=6+24=18-6-(-24)=-6+24=18.

We can also think of it like this: 66 hours ago, 1-1 full day and 1818 extra hours have passed.

Exercise 2.

Calculate 1527mod341527\bmod 34.

Input (Number)

Enter a number in the box. You can enter

  • a natural number, such as 22,

  • an integer, such as ‐7,

  • a quotient, such as 22/7, or

  • a terminating decimal, such as 3.14285.

There may be more than one correct solution.

Just to get some more practice...

Exercise 3.

Calculate the following numbers.

Inputs (Number)
311mod14=311\bmod 14 =
(123)mod19=(-123)\bmod 19 =
4mod17=4\bmod 17 =
(234)mod13=(-234)\bmod 13 =
(25)mod01=(-25)\bmod \phantom {0}1 =

Enter a number in the box. You can enter

  • a natural number, such as 22,

  • an integer, such as ‐7,

  • a quotient, such as 22/7, or

  • a terminating decimal, such as 3.14285.

There may be more than one correct solution.

3Division with remainder

You may have encountered modular arithmetic back in school, in the context of division with remainder. For example, 16 divided by 5 is 3, with a remainder of 1. By this we mean that you can “fit" the number 5 three times into 16, but doing this, you only reach 15, so you have a remainder of 1. In the clock metaphor, a 5‐hour‐clock makes 3 full turns within 16 hours, after which 15 hours have passed. So after 16 hours, the clock is at 1. In formulas, 16mod5=116\bmod 5=1. The result of the division with remainder also has a name, we call it 16div516 \mathop {\textrm {div}}5, and it has the value 3. We also know how to compute ndivmn \mathop {\textrm {div}}m for arbitrary integers nn and m>0m>0: take n/mn/m and round down. So the following holds:

ndivm=nm.\displaystyle n \mathop {\textrm {div}}m = \left \lfloor \frac {n}{m}\right \rfloor .
Our previous formula for computing nmodmn\bmod m was
nmodm=nmnm.\displaystyle n \bmod m = n-m \left \lfloor \frac {n}{m}\right \rfloor .
With the div\mathop {\textrm {div}} notation, it becomes
nmodm=nm(ndivm),\displaystyle n \bmod m = n-m\cdot (n\mathop {\textrm {div}}m),
and we can again rewrite this as
n=m(ndivm)+(nmodm).\displaystyle n = m\cdot (n\mathop {\textrm {div}}m) + (n \bmod m).
This is known as the div‐mod identity.

Exercise 4.

  • Suppose you know that ndiv14=3n\mathop {\textrm {div}}14=3 and nmod14=8n\bmod 14=8. What is the number nn?

    Input (Number)

    Enter a number in the box. You can enter

    • a natural number, such as 22,

    • an integer, such as ‐7,

    • a quotient, such as 22/7, or

    • a terminating decimal, such as 3.14285.

    There may be more than one correct solution.

  • Suppose you know that ndiv14=0n\mathop {\textrm {div}}14=0. What can you conclude about the integer nn?

    Input (Multiple Choice)
    • nn is divisible by 1414 without remainder.
    • 0n130\leq n\leq 13.
    • n=nmod14n = n\bmod 14.

    Decide which of the provided answers are correct and select them by clicking on the corresponding boxes, thus marking them with a blue border like so:

    • Unselected
    • Selected
    • Unselected
    • Selected

    Any number of the provided answers can be correct, including zero.

  • Suppose you know that nmod14=0n\bmod 14=0. What can you conclude about the integer nn?

    Input (Multiple Choice)
    • nn is divisible by 1414 without remainder.
    • 0n130\leq n\leq 13.
    • n=14(ndiv14)n = 14\cdot (n\mathop {\textrm {div}}14).

    Decide which of the provided answers are correct and select them by clicking on the corresponding boxes, thus marking them with a blue border like so:

    • Unselected
    • Selected
    • Unselected
    • Selected

    Any number of the provided answers can be correct, including zero.