Modulo · Teknikguide
Modulo Made Simple
Av Robert Hansson, Kung Fu Math
Modulo is the remainder after division. 17 mod 5 is 2, because 5 fits three times into 17 and leaves 2. It sounds technical, but you use it every day: a clock works modulo 12, and the days of the week work modulo 7.
Modulo is also the secret behind divisibility rules, check digits on bank cards and ISBNs, and much of computer science. The techniques below make it quick to calculate in your head.
01
Clock arithmetic
Picture a clock with n hours. Counting past the top wraps back to zero. To find a mod n, remove as many whole n's as you can and keep what is left.
Löst exempel
What time is it 30 hours after 9 o'clock?
- → 30 mod 12 = 6
- → 9 + 6 = 15
- → 15 mod 12 = 3
Svar: 3 o'clock
Öva: What day is it 100 days after a Monday? (100 mod 7 = 2.)
02
Remainder by subtraction
Subtract the largest multiple of the divisor that you know. For 87 mod 9, the nearest multiple below is 81, so the remainder is 6.
Löst exempel
87 mod 9
- → 9 × 9 = 81
- → 87 − 81 = 6
Svar: 6
Öva: Try 53 mod 7, 100 mod 8 and 145 mod 12.
03
Last digits
Any number mod 10 is its last digit. Mod 100 is its last two digits. Mod 2 and mod 5 depend only on the last digit, and mod 4 only on the last two digits.
Löst exempel
3 716 mod 4
- → Look at the last two digits: 16
- → 16 mod 4 = 0
Svar: 0
Öva: Try 9 873 mod 10, 4 538 mod 4 and 1 237 mod 5.
04
Digit sums for 3 and 9
A number has the same remainder mod 9 as the sum of its digits. The same holds for mod 3. So 4 725 mod 9 is (4 + 7 + 2 + 5) mod 9 = 18 mod 9 = 0, and the number is divisible by 9.
Löst exempel
5 381 mod 9
- → 5 + 3 + 8 + 1 = 17
- → 17 mod 9 = 8
Svar: 8
Öva: Is 7 236 divisible by 9? What is 4 001 mod 3?
05
Reduce first, then calculate
When adding or multiplying, you may reduce each number modulo n first. (a × b) mod n is the same as ((a mod n) × (b mod n)) mod n. Big numbers become small ones.
Löst exempel
(47 × 53) mod 5
- → 47 mod 5 = 2
- → 53 mod 5 = 3
- → 2 × 3 = 6
- → 6 mod 5 = 1
Svar: 1
Öva: Try (38 × 41) mod 7 and (123 + 456) mod 9.
Vanliga misstag
- Confusing the quotient with the remainder. 17 ÷ 5 is 3 with remainder 2; 17 mod 5 is 2.
- Getting a remainder larger than the divisor. A remainder mod n is always between 0 and n − 1.
- Using the digit-sum rule for numbers other than 3 and 9.
Sammanfattning
- Modulo is the remainder: think of a clock that wraps around.
- Last digits give mod 10, mod 2, mod 5 and mod 4; digit sums give mod 3 and mod 9.
- Reduce numbers before adding or multiplying them.
In i dojon
Läsning ger förståelse, träning ger snabbhet. Träna detta i Kung Fu Math – inget konto behövs.