Modular Arithmetic


Modular Arithmetic

Anna
Today we’re exploring modular arithmetic: arithmetic where, instead of keeping track of the whole number, we focus on its remainder after division. Corey, we already use a simple version of this idea when we talk about even and odd numbers.

Corey
Right. If an even number plus an odd number is odd, what matters is that their remainders when divided by 2 are 0 and 1. We don’t need to know the original numbers. That’s arithmetic modulo 2—and the same idea works for any positive modulus.

Anna
The key notation is “a is congruent to b mod n,” written a congruent to b modulo n. It means exactly that a and b differ by a multiple of n. So, for example, 12 is congruent to 5 modulo 7 because their difference is 7.

Corey
And there’s a basic fact from division that makes this definition practical. For any integer a and positive integer n, we can write a as q times n plus r, where q is an integer and the remainder r is an integer from 0 up to, but not including, n. That remainder is one of 0 through n minus 1.

Anna
So the quotient tells us how many whole multiples of n fit, and the remainder tells us what’s left. The quotient and remainder are unique. In modular arithmetic, we often care about the remainder and can set aside the quotient.

Corey
In fact, two integers are congruent modulo n if and only if they have the same remainder when divided by n. If a and b both have remainder r, write them as qn plus r and q-prime n plus r. Subtract them, and the remainders cancel, leaving a multiple of n.

Anna
And the converse matters too. Suppose a and b differ by a multiple of n, and write their remainders as r and r-prime. Their difference is a multiple of n plus r minus r-prime. But r minus r-prime is strictly between negative n and n. The only multiple of n in that range is zero, so the remainders must match.

Corey
That gives us a useful mental picture: clock math. On a seven-position clock, label the positions 0 through 6. Starting at 0 and moving forward 12 steps lands in the same place as moving 5 steps, because you’ve gone around once and then moved 5 more.

Anna
So modulo 7, 12 and 5 represent the same position, or the same remainder. We’re not saying the integers 12 and 5 are equal in ordinary arithmetic. We’re saying they’re equivalent for the purpose of keeping track of remainders modulo 7.

Corey
Addition and multiplication work much as you’d expect. For instance, 10 is congruent to 3 modulo 7, and 12 is congruent to 5 modulo 7. Multiply the original numbers and you get 120; multiply the remainders and you get 15. Both are congruent to 1 modulo 7.

Anna
That’s why you can add or multiply as usual, then reduce the answer to its remainder between 0 and n minus 1. The important point is that the result depends on the remainders of the numbers you started with, not on how many full laps around the clock they made.

Corey
Many familiar rules survive this change of perspective. Addition is associative and commutative, and multiplication distributes over addition. In other words, changing the parentheses or the order of addition doesn’t change the remainder you end up with.

Anna
You can even think of the remainders as the elements of a small arithmetic system. Modulo 7, the possible elements are 0 through 6, and you can make addition and multiplication tables by doing the operations and reducing the results modulo 7.

Corey
Why does this matter beyond a clock analogy? Modular arithmetic is important in computer science, including cryptography. The lecture also gave a playful illustration of a zero-knowledge proof: two people want to know whether they share a preference for an embarrassing movie without revealing their individual answers.

Anna
The demonstration used five cards: three red and two black. Each person gets one red card and one black card, and a red card goes in the middle. For a “yes,” a person places their red card nearest the middle and their black card farther away. For a “no,” they reverse that order.

Corey
Stack the cards and let the participants cut the deck, creating a random cyclic shift. If both answers are yes, the arrangement has three red cards in a row, considered cyclically. If either answer is no, it has only two red cards in a row.

Anna
The cuts make the arrangement’s starting point hard to track, so the person who said yes can preserve plausible deniability if the other person said no. It’s a simple illustration—not a full account of cryptographic security—but it hints at how modular arithmetic can help with private computations and cryptographic schemes.

Corey
Now for a question that makes modular multiplication more interesting: does every number have a multiplicative inverse? An inverse of 4 modulo 7, for example, is a number that you can multiply by 4 to get a remainder of 1.

Anna
In this case, 4 works: 2 times 4 is 8, which is congruent to 1 modulo 7. So 2 and 4 are multiplicative inverses of each other. Multiplying by 4 can be undone, modulo 7, by multiplying by 2.

Corey
Zero doesn’t have a multiplicative inverse, so we ask whether every nonzero element does. To answer that—and to find inverses efficiently—we need the greatest common divisor, or GCD: the largest integer that divides two given integers.

Anna
A brute-force way to find a GCD would be to test possible divisors one by one. That becomes impractical for large numbers. A much faster approach, known since ancient times, is the Euclidean algorithm, which relies on the fact that the GCD of a and b is the same as the GCD of a and b minus a, provided b is at least a.

Corey
Here’s why. Any number that divides both a and b also divides their difference, b minus a. And any number that divides a and b minus a also divides their sum, b. So the common divisors are unchanged, which means the GCD is unchanged.

Anna
We can make bigger progress using division. If b equals q times a plus r, then the GCD of a and b equals the GCD of a and r. Repeatedly subtracting a from b would eventually get us to that remainder, so division just lets us jump directly there.

Corey
Then we repeat the process with the smaller numbers, alternating which number we divide by which. For 360 and 46, the successive remainders are 38, 8, 6, 2, and finally 0. Each step preserves the GCD, so the GCD of 360 and 46 is the GCD of 2 and 0, which is 2.

Anna
The extended Euclidean algorithm keeps track of more than the remainders. It also records how each remainder was built from the original numbers. That lets it find integers s and t such that s times a plus t times b equals the GCD of a and b.

Corey
For our example, it would find integers s and t with 46s plus 360t equal to 2. That equation is also a certificate that 2 really is the GCD. If a larger number divided both 46 and 360, it would divide the left-hand side—and therefore divide 2, which rules out a larger common divisor.

Anna
Now connect that result back to inverses. If the GCD of a number b and the modulus n is 1, the extended algorithm gives integers s and t such that bs plus nt equals 1. Modulo n, the nt term disappears, leaving bs congruent to 1. So s is a multiplicative inverse of b modulo n.

Corey
Modulo a prime p, every nonzero remainder b has GCD 1 with p. That means every nonzero element has a multiplicative inverse. And if you’re given a large prime p and a nonzero b, you can find that inverse with the extended Euclidean algorithm instead of building an entire multiplication table.

Anna
A prime modulus isn’t the only setting where inverses exist. Modulo 6, for example, 5 has an inverse because the GCD of 5 and 6 is 1; in fact, 5 times 5 is 25, which is congruent to 1 modulo 6. The key condition is that the number and modulus have no common factor greater than 1.

Corey
Let’s use these ideas for a puzzle. Suppose a treasure has a number n of coins, with 4 left over when the coins are divided into groups of 5, and 1 left over when divided into groups of 7. In congruence notation, that’s n congruent to 4 modulo 5 and n congruent to 1 modulo 7.

Anna
The smallest positive answer is 29: dividing 29 by 5 leaves 4, and dividing it by 7 leaves 1. The general principle behind this is the Chinese remainder theorem. If a and b are relatively prime, meaning their GCD is 1, then any pair of specified remainders modulo a and b corresponds to exactly one n between 0 and ab minus 1.

Corey
Why exactly one? There are ab possible values of n in that range, and there are also ab possible pairs of remainders: a choices modulo a and b choices modulo b. The crucial step is showing that two different values of n can’t produce the same pair.

Anna
Suppose x and y have the same remainder modulo a and the same remainder modulo b. Then a divides x minus y, and b divides x minus y. Since a and b are relatively prime, their product ab divides the difference.

Corey
But if x and y are both between 0 and ab minus 1, two different values can’t differ by a nonzero multiple of ab. So x must equal y. That proves there are no collisions; since the finite sets of possible values and possible remainder pairs have the same size, every pair occurs exactly once.

Anna
And the theorem can be constructive: we can actually find n. For the system n congruent to r modulo a and n congruent to s modulo b, write n as kb plus s. That automatically satisfies the condition modulo b.

Corey
To satisfy the other condition, we need kb plus s congruent to r modulo a. Rearranging gives kb congruent to r minus s modulo a. Because a and b are relatively prime, b has an inverse modulo a, so we can multiply by that inverse to find k. Choosing k as its remainder modulo a gives the desired n in the range from 0 to ab minus 1.

Anna
That’s where the earlier work on inverses and the extended Euclidean algorithm pays off: it gives us a way to solve the modular system, not just a proof that a solution exists.

Corey
There’s also an important computer-science application related to fountain codes. Imagine a very large file represented as an integer n. A sender can broadcast pieces consisting of a modulus a and the remainder of n when divided by a.

Anna
If a receiver collects enough pieces with relatively prime moduli, and the product of those moduli is larger than n, the Chinese remainder theorem lets the receiver reconstruct n. In this simplified picture, the sender can keep broadcasting pieces, and different receivers can gather enough of them at different times.

Corey
Real fountain codes are more sophisticated, including concerns such as errors in messages. But the example shows why modular arithmetic and the Chinese remainder theorem are useful: they can help encode and reconstruct information efficiently. And that’s a foundation for later work in cryptography and group theory.

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