1. Home
  2. Explore
  3. Mathematics
  4. Modular Arithmetic

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Modular Arithmetic

Speaker 1Speaker 2

Modular Arithmetic

13:01
0:00-13:01
  1. Anna

    Today we’re learning modular arithmetic: a way to focus on remainders instead of carrying around whole numbers. It shows up in familiar ideas like even and odd numbers, the last digit of a product, and clocks. And we’ll build on two tools from divisibility: Bézout’s identity, which says the greatest common divisor of a and b can be written as as plus bt, and the fact that an integer is a linear combination of a and b exactly when it’s divisible by their greatest common divisor.

  2. Michael

    Let’s start with those everyday examples. An even number plus an odd number is odd because their remainders after division by 2 are 0 and 1, and 0 plus 1 gives remainder 1. For the last digit of 357 times 994, you can ignore the hundreds and thousands and multiply the last two digits: 57 times 94 ends in 8. The same idea works for time: 49 hours is two full days plus one hour, so from 2:39 you get 3:39.

  3. Anna

    And for the day of the week, 100 days is 14 full weeks plus 2 days, since 100 divided by 7 leaves remainder 2. So you can ignore those complete weeks and move ahead two days. That’s the basic theme: modulo n, we ignore multiples of n and pay attention to the remainder. People sometimes call it clock arithmetic because clocks make that wraparound idea easy to picture.

  4. Michael

    Formally, we say a is congruent to b modulo n if n divides a minus b. In symbols, a ≡ b mod n means that their difference is a multiple of n. You can think of this as saying they count as the same value when we’re working modulo n.

  5. Anna

    For example, modulo 5, 17 is congruent to 12 because their difference is 5. It’s also congruent to 32 because their difference is negative 15, another multiple of 5. But 17 is not congruent to 33 modulo 5: their difference is negative 16, which isn’t divisible by 5.

  6. Michael

    Why are there only five different values to consider modulo 5? The division theorem says every integer k can be written uniquely as 5q plus r, where r is one of 0, 1, 2, 3, or 4. Since k minus r is a multiple of 5, k is congruent to its remainder r. So every integer belongs to one of five groups, or congruence classes, represented by 0 through 4.

  7. Anna

    For instance, the class represented by 1 contains 1, 6, 11, negative 4, and negative 9, as well as all the other integers differing from 1 by a multiple of 5. Those classes partition all the integers: every integer is in one class, and its remainder tells you which one.

  8. Michael

    That gives us a useful theorem: a and b are congruent modulo n if and only if they have the same remainder when divided by n. The ‘if and only if’ matters—it means we need both directions. If the remainders are the same r, write a as qn plus r and b as q-prime n plus r. Subtracting gives a minus b equals n times q minus q-prime, so n divides the difference.

  9. Anna

    For the other direction, suppose a and b are congruent, so a equals b plus some multiple kn. Write b as qn plus r using the division theorem. Then a is q plus k, times n, plus that same r. Both numbers therefore have remainder r. So congruence and having the same remainder are two ways of describing the same relationship.

  10. Michael

    A note about notation: in this class, the remainder is always the nonnegative one, from 0 up to n minus 1. Computer languages don’t all handle remainders involving negative numbers the same way, so it’s worth checking their conventions. And be careful to distinguish a congruence from a remainder operation: ‘a is congruent to b modulo n’ describes a relationship between a and b, while ‘b rem n’ names the remainder you get from dividing b by n.

  11. Anna

    That distinction is one reason to avoid ambiguous writing such as ‘a equals b mod n’ until you’re comfortable with the notation. A clearer version of the relationship is a congruent to b modulo n; a clearer version of the remainder is rem of b and n. The first says two numbers differ by a multiple of n. The second gives one particular remainder.

  12. Michael

    Now for arithmetic. If a and b are congruent modulo n, then adding the same c to both keeps them congruent. Subtracting c from both works too, and so does subtracting a and b from c. Multiplying both by c also preserves congruence. In practical terms, you can replace a number by any congruent number during addition, subtraction, or multiplication.

  13. Anna

    For example, if a is congruent to 3 modulo 5 and b is congruent to 4 modulo 5, their sum is congruent to 3 plus 4, or 7. And 7 is congruent to 2 modulo 5, so a plus b is congruent to 2. This generalizes even plus odd: adding representatives of the remainder classes tells you the class of the sum.

  14. Michael

    You can also replace the base of a positive integer power. If a is congruent to b modulo n, then a to the c is congruent to b to the c for positive c. The reason is repeated multiplication: each time you multiply by another congruent factor, the multiplication rule preserves congruence. That argument can be made formally by induction on c.

  15. Anna

    But don’t make the same replacement with exponents. In general, congruent exponents don’t give congruent powers. Modulo 5, the exponents 1 and 6 are congruent, but negative 1 to the first power is negative 1, while negative 1 to the sixth power is 1. Those answers are not congruent modulo 5.

  16. Michael

    Here’s how the arithmetic rules help with a big computation. Suppose we want the remainder modulo 100 of 11335 to the 11111 times 6, plus 7799 to the 5000. Since 11335 is congruent to 35 modulo 100, and 7799 is congruent to negative 1, we can replace those bases and work with 35 to the 11111 times 6, plus negative 1 to the 5000.

  17. Anna

    The second power is easy: negative 1 to an even power is 1. So that term contributes 6 plus 1, or 7. For the first term, we can calculate powers of 35 while reducing modulo 100 at every step. The square is 1225, which has remainder 25; the cube is 25 times 35, or 875, which has remainder 75.

  18. Michael

    The fourth power is the square of the square, so it’s congruent to 25 times 25, or 625, which has remainder 25. From there the remainders alternate: multiplying 25 by 35 gives remainder 75, and multiplying 75 by 35 gives remainder 25. Thus an odd power above the first has remainder 75, and an even power has remainder 25. Since 11111 is odd, that power has remainder 75; 75 times 7 is 525, with remainder 25.

  19. Anna

    Why do powers eventually show a repeating pattern? Modulo 100, there are only 100 possible remainders. If you keep making powers, some remainder must eventually repeat, and that can lead to a repeating pattern. The pattern might be harder to spot than the short alternation we just found, but there are only finitely many possible values.

  20. Michael

    What about division? We can’t always cancel a factor in a congruence. Consider 3x congruent to 3 modulo 6. If we canceled the 3 and concluded x is congruent to 1 modulo 6, we’d be wrong: x equals 3 works in the original congruence, because 9 is congruent to 3 modulo 6, but 3 is not congruent to 1.

  21. Anna

    The underlying issue is that 3 has no multiplicative inverse modulo 6. An inverse of a modulo n is a number b such that ab is congruent to 1 modulo n. To cancel a, we need to multiply by such an inverse. For 3 modulo 6, no number b can make 3b congruent to 1, because 3b is always a multiple of 3, while 1 is not congruent to a multiple of 3 modulo 6.

  22. Michael

    There’s an exact test for when an inverse exists: a has a multiplicative inverse modulo n if and only if the greatest common divisor of a and n is 1. To see why, an inverse b means ab is congruent to 1, so n divides ab minus 1. That gives an equation 1 equals ab minus qn for some integer q. In other words, 1 is a linear combination of a and n, which happens exactly when their greatest common divisor is 1.

  23. Anna

    That connects directly to Bézout’s identity and the linear-combination fact from divisibility. If the gcd is greater than 1, every linear combination is divisible by that gcd, so it can’t equal 1. If the gcd is 1, Bézout’s identity guarantees a linear combination equal to 1, giving an inverse.

  24. Michael

    For example, 3 does have an inverse modulo 5: 2, because 3 times 2 is 6, congruent to 1 modulo 5. So from 3x congruent to 3 modulo 5, we can multiply both sides by 2 and conclude x is congruent to 1 modulo 5. Similarly, 7 and 30 have gcd 1, and 13 is an inverse of 7 modulo 30 because 7 times 13 is 91, congruent to 1. Thus 7x congruent to 14 modulo 30 gives x congruent to 2.

  25. Anna

    Now we can say more about exponents using Fermat’s Little Theorem. If p is prime and a is not congruent to 0 modulo p, then a to the p minus 1 is congruent to 1 modulo p. So, under those conditions, powers can be simplified by removing blocks of p minus 1 from the exponent—not blocks of p.

  26. Michael

    Here’s the idea behind the proof. Look at a, 2a, 3a, all the way through p minus 1 times a, with each value considered modulo p. None is congruent to zero: p divides neither a nor any of the coefficients 1 through p minus 1, and p is prime. Also, these values are all distinct modulo p. If ia and ja were congruent, we could cancel a because a has an inverse modulo p, giving i congruent to j; since both coefficients are between 1 and p minus 1, they must be equal.

  27. Anna

    So those p minus 1 values are just the nonzero remainders modulo p, rearranged. Multiply them all together. The product of the coefficients is the factorial of p minus 1, and the product of the values ia includes p minus 1 copies of a, so it’s congruent to that factorial times a to the p minus 1. The factorial is not divisible by p, so it has an inverse modulo p and can be canceled. What remains is a to the p minus 1 congruent to 1.

  28. Michael

    Let’s use the theorem to find the remainder of 7 to the 95 modulo 13. Since 13 is prime and 7 is not a multiple of 13, 7 to the 12 is congruent to 1 modulo 13. And 95 is 7 times 12 plus 11, so 7 to the 95 is congruent to 7 to the 11.

  29. Anna

    Because 7 to the 12 is 7 to the 11 times 7, that also tells us that 7 to the 11 is an inverse of 7 modulo 13. The inverse is 2, since 7 times 2 is 14, which is congruent to 1 modulo 13. So the remainder of 7 to the 95 upon division by 13 is 2.

  30. Michael

    The big picture is that modular arithmetic lets us simplify calculations by replacing numbers with their remainders or other congruent values. Addition, subtraction, and multiplication behave reliably under those replacements, and powers let us replace the base—but not generally the exponent. Division requires an inverse, which exists exactly when the number and the modulus are relatively prime; for a prime modulus, Fermat’s Little Theorem gives a powerful way to simplify exponents.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent