Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Modular arithmetic, gcd and primes

After this lesson: I can use remainders, the gcd and primes.

60 min lesson4 commandsCompilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

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

WordWhat it meansExample
Remainderwhat is left after dividing as far as you can
Modulothe operation that gives the remainder15 % 12
Divisora number that divides another with no remainder
Greatest common divisorthe largest divisor two numbers share, written gcd
Primea number with exactly two divisors, 1 and itself
Check digitan 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 laterDays later % 7Day of the week
00Monday
70Monday
81Tuesday
151Tuesday
1002Wednesday

The cells students fill in are left blank in this preview.

Check the table and the clock

Command
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

CodeDigits times weightsTotalTotal % 10Check digit
123453, 2, 9, 4, 1533310 - 3 = 7
12354 (two digits swapped)3, 2, 9, 5, 1231110 - 1 = 9

The cells students fill in are left blank in this preview.

Work out the two totals

Command
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

Stepaba % b
1481812
218126
31260
Stop60The gcd is 6

The cells students fill in are left blank in this preview.

Try it: watch Euclid's method

Code: python
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

x3 times x mod 74 times x mod 6
134
262
320
454
51: found, so 5 is the inverse2
640

The cells students fill in are left blank in this preview.

Print both columns of the table

Command
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?

NumberA divisor foundPrime?
122No
13NoneYes
153No
17NoneYes
213No
1Only one divisor in allNo

The cells students fill in are left blank in this preview.

List the primes up to 20

Command
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.

About this lab and its source: Compilers and computer architecture lab: gcc, flex, bison and RISC-V in one VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.