Modular Operations

The Core Idea

Addition, subtraction, and multiplication all work the same way:

Do the operation, then take mod.


Addition

(7+8)mod  12=15mod  12=3(7 + 8) \mod 12 = 15 \mod 12 = 3

Clock intuition: 7 o’clock plus 8 hours lands on 3 o’clock.


Subtraction

(3−7)mod  12=−4mod  12=8(3 - 7) \mod 12 = -4 \mod 12 = 8

Clock intuition: 3 o’clock minus 7 hours, go backwards to 8 o’clock.


What about negative results?

Mod always gives a result between 00 and n−1n-1, where nn is the modulus. Negative numbers aren’t in that range.

The fix: add the modulus until positive.


Think about the clock. What’s −1mod  12-1 \mod 12?

Go 1 step backwards from 0. You land on 11.

So −1mod  12=11-1 \mod 12 = 11. We get there by computing −1+12=11-1 + 12 = 11.


What about −4mod  12-4 \mod 12?

Go 4 steps backwards from 0: 0→11→10→9→80 \to 11 \to 10 \to 9 \to 8.

So −4mod  12=8-4 \mod 12 = 8. Or just compute −4+12=8-4 + 12 = 8.


More examples:

ExpressionCalculationResult
−3mod  7-3 \mod 7−3+7-3 + 74
−5mod  8-5 \mod 8−5+8-5 + 83
−13mod  5-13 \mod 5−13+5+5+5-13 + 5 + 5 + 52

If one addition isn’t enough, keep adding until you’re positive.


Multiplication

(5×7)mod  12=35mod  12=11(5 \times 7) \mod 12 = 35 \mod 12 = 11

Same pattern: multiply, then take the remainder.


The Key Insight

Here’s something important:

Numbers with the same remainder are interchangeable.

What does this mean? Since 17mod  12=517 \mod 12 = 5, we can swap 17 for 5 in any mod 12 calculation.

They’re the same position on the clock. They behave identically.


The Shortcut

Since we can swap any number for its remainder, we can simplify before computing.

Take (17×25)mod  12(17 \times 25) \mod 12:

The hard way: 17×25=42517 \times 25 = 425, then 425mod  12=5425 \mod 12 = 5

The easy way: 17mod  12=517 \mod 12 = 5 and 25mod  12=125 \mod 12 = 1. Now 5×1=55 \times 1 = 5.

Same answer. Much smaller numbers.


This works for addition too.

(56+37)mod  10(56 + 37) \mod 10

The hard way: 56+37=9356 + 37 = 93, then 93mod  10=393 \mod 10 = 3

The easy way: 56mod  10=656 \mod 10 = 6 and 37mod  10=737 \mod 10 = 7. Now 6+7=136 + 7 = 13, and 13mod  10=313 \mod 10 = 3.


Why This Matters

In cryptography, we compute things like:

M65537mod  nM^{65537} \mod n

If you computed M65537M^{65537} first, you’d get a number with millions of digits.

Instead, take mod after each multiplication. The numbers stay small throughout, and you get the same answer.