Keep a list
Lists to keep track of values
Where do we keep them?
To find the gcd of two numbers, we must remember all the divisors of each number we find, so we can compare them later.
The book writes a list with square brackets, for instance [1, 4, 9, 16].
When we start looking for the divisors of 18, what does the list hold?
What this lesson covers
The idea
A list is a sequence that starts empty and to which each newly found value is added at the end.
Where do we keep them?
To find the gcd of two numbers, we must remember all the divisors of each number we find, so we can compare them later. The book writes a list with square brackets, for instance [1, 4, 9, 16].
When we start looking for the divisors of 18, what does the list hold?
- Nothing yet: it is empty [ ]
- [18]
- [1, 18]
Build the list
Check 1, 2, 3, …, 18 one at a time. Each time a number divides 18, it is added at the end of the list. Watch how the list grows.
Start empty, add at the end
A list is a sequence of values. It starts empty, and each newly found value is added at the end.
For 18: [ ] → [1] → [1, 2] → [1, 2, 3] → [1, 2, 3, 6] → [1, 2, 3, 6, 9] → [1, 2, 3, 6, 9, 18]. When 4, 5, 7, 8, 10, …, 17 are checked, the list stays unchanged. So divisors(18) = [1, 2, 3, 6, 9, 18]. The list is in increasing order, because we checked from 1 to 18 in order.
Notes
A list starts empty; each new value found is added at the end. Built by checking 1, 2, 3, … in order, it comes out in increasing order.
Check yourself
The list is [1, 2, 3] and we find that 6 divides 18. What is the list now?
Why is divisors(18) in increasing order?
How many values are in the list divisors(18)?
Answer: 6
divisors(18) = [1, 2, 3, 6, 9, 18] has 6 values.
- [1, 2, 3, 6] — correct. Yes. The new value goes at the end.
- [6, 1, 2, 3]. New values are added at the end, not the front.
- [6]. The earlier values stay in the list.
- Because we checked 1, 2, 3, …, 18 in order and added each divisor at the end — correct. Yes. Smaller numbers are checked, and added, first.
- Because we sorted the list at the end. No sorting step is needed.
- Because 18 is even. The order comes from how we built the list, not from 18 itself.