What time is it 100 hours from now?
Modular arithmetic is arithmetic that wraps round, like the hours on a clock. After 12 comes 1 again.
Computers use it everywhere. A hash table picks a slot with it, and a check digit catches a typing mistake with it. This lesson covers remainders, the greatest common divisor (gcd) and prime numbers.
By the end of this lesson
Note
I can use remainders, the gcd and primes.
Key words
| Word | What it means | Example |
|---|---|---|
| Remainder | what is left after dividing as far as you can | |
| Modulo | the operation that gives the remainder | 15 % 12 |
| Divisor | a number that divides another with no remainder | |
| Greatest common divisor | the largest divisor two numbers share, written gcd | |
| Prime | a number with exactly two divisors, 1 and itself | |
| Check digit | an extra digit that lets a program spot a mistake |
The cells students fill in are left blank in this preview.
A remainder is what is left over
Divide 15 by 12. It goes in once, with 3 left over. That 3 is the remainder, and the operation that finds it is called modulo. Python writes it as %.
So 15 % 12 is 3, which is why 10 o'clock plus 5 hours is 3 o'clock. Days of the week work the same way, with 7 in place of 12.
Worked example: days after a Monday
| Days later | Days later % 7 | Day of the week |
|---|---|---|
| 0 | 0 | Monday |
| 7 | 0 | Monday |
| 8 | 1 | Tuesday |
| 15 | 1 | Tuesday |
| 100 | 2 | Wednesday |
The cells students fill in are left blank in this preview.
Check the table and the clock
python3 -c "print([n % 7 for n in (0, 7, 8, 15, 100)], (10 + 5) % 12)"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A check digit catches typing mistakes
Many codes end with a check digit. Multiply the digits by weights 3, 1, 3, 1 and so on, and add them up. The check digit is the number that brings the total up to a multiple of 10.
If a person swaps two digits, the total changes and the check digit no longer fits. The program sees the mistake.
Worked example: the code 12345
| Code | Digits times weights | Total | Total % 10 | Check digit |
|---|---|---|---|---|
| 12345 | 3, 2, 9, 4, 15 | 33 | 3 | 10 - 3 = 7 |
| 12354 (two digits swapped) | 3, 2, 9, 5, 12 | 31 | 1 | 10 - 1 = 9 |
The cells students fill in are left blank in this preview.
Work out the two totals
python3 -c "w = [3, 1, 3, 1, 3]; print([sum(int(d) * x for d, x in zip(c, w)) for c in ('12345', '12354')])"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Euclid's method finds the gcd with remainders
The greatest common divisor, or gcd, of 48 and 18 is the biggest number that divides both. Here it is 6.
Euclid's method finds it by repeating one step. Replace the pair (a, b) with (b, a % b). Stop when the second number is 0. The first number is then the gcd.
Worked example: gcd of 48 and 18
| Step | a | b | a % b |
|---|---|---|---|
| 1 | 48 | 18 | 12 |
| 2 | 18 | 12 | 6 |
| 3 | 12 | 6 | 0 |
| Stop | 6 | 0 | The gcd is 6 |
The cells students fill in are left blank in this preview.
Try it: watch Euclid's method
a = 48
b = 18
while b != 0:
remainder = a % b
print(a, "mod", b, "is", remainder)
a = b
b = remainder
print("gcd is", a)
# Change a and b to 35 and 12. What is the gcd now?
An inverse undoes a multiplication
Two numbers are coprime when their gcd is 1. Working mod 7, find x so that 3 times x leaves remainder 1. Here x is 5, because 15 % 7 is 1. We call 5 the modular inverse of 3 mod 7.
An inverse exists only when the number and the modulus are coprime. Mod 6, the number 4 has no inverse, because gcd(4, 6) is 2.
Worked example: looking for an inverse
| x | 3 times x mod 7 | 4 times x mod 6 |
|---|---|---|
| 1 | 3 | 4 |
| 2 | 6 | 2 |
| 3 | 2 | 0 |
| 4 | 5 | 4 |
| 5 | 1: found, so 5 is the inverse | 2 |
| 6 | 4 | 0 |
The cells students fill in are left blank in this preview.
Print both columns of the table
python3 -c "print([3 * x % 7 for x in range(1, 7)], [4 * x % 6 for x in range(1, 6)])"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A prime has exactly two divisors
A prime has exactly two divisors: 1 and itself. 13 is prime. 12 is not, because 2, 3, 4 and 6 divide it.
To test a number n, try dividing by 2, 3, 4 and on up. You can stop when the divisor squared passes n, because a divisor above that has a partner below it. If none divides n, it is prime.
Worked example: is it prime?
| Number | A divisor found | Prime? |
|---|---|---|
| 12 | 2 | No |
| 13 | None | Yes |
| 15 | 3 | No |
| 17 | None | Yes |
| 21 | 3 | No |
| 1 | Only one divisor in all | No |
The cells students fill in are left blank in this preview.
List the primes up to 20
python3 -c "print([n for n in range(2, 21) if all(n % d for d in range(2, n))])"Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A common mistake
Warning
Many people think 1 is a prime. A prime has exactly two divisors, and 1 has only one. So the first prime is 2, which is also the only even prime.