Think and Reflect (Textbook Pages 39 and 41)
The two reflection boxes of the chapter: why the column-addition algorithm is correct, how it extends to decimals, and how the divisor-listing algorithm behaves as \(n\) grows.
2. How would you modify the algorithm to add two decimal fractions?
The algorithm adds digits column by column and carries 1 whenever a column reaches 10. Counting dots is the definition of addition, so we must show that the digit procedure produces exactly the same total as counting all the dots. The bridge is place value: every number is a sum of groups of units, tens, hundreds, and so on.
Group each number into hundreds, tens and units, then add group by group: \[ \begin{aligned} 473 &= 4\cdot 100 + 7\cdot 10 + 3 \\ 695 &= 6\cdot 100 + 9\cdot 10 + 5 \\ 473 + 695 &= (4+6)\cdot 100 + (7+9)\cdot 10 + (3+5) \\ &= 10\cdot 100 + 16\cdot 10 + 8 \\ &= 1000 + 160 + 8 \\ &= \mathbf{1168} \end{aligned} \] Adding the groups separately is legitimate because addition is commutative and associative: the order in which we combine the dots does not change how many dots there are in total.
A column total such as \(16\) tens is not yet written in standard form, because a single place may hold only one digit. Regrouping fixes this without changing the value: \[ \begin{aligned} 16\cdot 10 &= 100 + 60 \\ &= 1\cdot 100 + 6\cdot 10 \\ 10\cdot 100 + 1\cdot 100 &= 1100 \\ &= 1000 + 100 \end{aligned} \] So the \(6\) is written in the tens place and the newly formed hundred is carried into the hundreds place. In the same way, ten units make one ten, ten tens make one hundred, and ten hundreds make one thousand. Each carry moves exactly one place to the left, which is precisely what the algorithm does. Since regrouping only rewrites the same total in a different way, the final digit string equals the counted sum.
In short: the algorithm is correct because (a) the digits of a number are its groups of \(1, 10, 100, \dots\), (b) groups of the same size may be added separately, and (c) ten items of one group may always be exchanged for one item of the next larger group.
The only new idea is that the decimal point separates whole-number places from fractional places, so the point itself must stay in one column and is never added.
1. Write the numbers one below the other so that the decimal points sit in the same
column (equivalently, align the units digits).
2. If the decimal parts have different lengths, pad with zeros on the right so that both
numbers have the same number of decimal places.
3. Start at the rightmost decimal place (tenths, hundredths, …) and add each column exactly as
before, including the carry from the column on its right.
4. If a column total is less than 10, write it and set the carry to 0; if it is 10 or more,
write its units digit and set the carry to 1.
5. Place the decimal point in the answer directly below the column of decimal points.
6. If a carry survives the leftmost column, write it on the left.
Pad \(3.6\) to \(3.60\) so that both numbers have two decimal places:
| Column | Digits added | Total | Written | Carry |
|---|---|---|---|---|
| Hundredths | \(5 + 0\) | 5 | 5 | 0 |
| Tenths | \(7 + 6\) | 13 | 3 | 1 |
| Units | \(2 + 3 + 1\) | 6 | 6 | 0 |
| Tens | \(1 + 0\) | 1 | 1 | 0 |
\[ \begin{aligned} 12.75 + 3.60 &= (12 + 3) + (0.75 + 0.60) \\ &= 15 + 1.35 \\ &= \mathbf{16.35} \end{aligned} \] Check: \(12.75 + 3.60 = \frac{1275}{100} + \frac{360}{100} = \frac{1635}{100} = 16.35\).
• (1) Digits are groups of \(1, 10, 100, \dots\); adding group by group and exchanging every ten items for one item of the next group reproduces the counted total, so \(473 + 695 = \mathbf{1168}\).
• (2) Align the decimal points, pad with zeros to equal length, add columns from the right with carries, and place the point in the answer under the other points: \(12.75 + 3.6 = \mathbf{16.35}\).
1. Start with an empty list-of-divisors \([\,]\).
2. For each number \(j\) in the sequence \(1, 2, 3, \dots, n\): if \(j\) divides \(n\), add
\(j\) to the end of list-of-divisors.
3. When \(j\) has reached \(n\), report the list.
The list is built from left to right, so it comes out in increasing order, and the last entry is always \(n\) itself.
| \(j\) | Does \(j\) divide 15? | list-of-divisors after the check |
|---|---|---|
| 1 | yes | \([1]\) |
| 2 | no | \([1]\) |
| 3 | yes | \([1, 3]\) |
| 4 | no | \([1, 3]\) |
| 5 | yes | \([1, 3, 5]\) |
| 6 | no | \([1, 3, 5]\) |
| 7 | no | \([1, 3, 5]\) |
| 8 | no | \([1, 3, 5]\) |
| 9 | no | \([1, 3, 5]\) |
| 10 | no | \([1, 3, 5]\) |
| 11 | no | \([1, 3, 5]\) |
| 12 | no | \([1, 3, 5]\) |
| 13 | no | \([1, 3, 5]\) |
| 14 | no | \([1, 3, 5]\) |
| 15 | yes | \([1, 3, 5, 15]\) |
Number of divisibility checks performed: \(15\). Number of divisors found: \(4\).
Only the rows where the list changes are shown; for every other \(j\) from 1 to 135 the list stays exactly as it was.
| \(j\) | Check | list-of-divisors after the check |
|---|---|---|
| 1 | \(135 = 135 \times 1\) | \([1]\) |
| 3 | \(135 = 45 \times 3\) | \([1, 3]\) |
| 5 | \(135 = 27 \times 5\) | \([1, 3, 5]\) |
| 9 | \(135 = 15 \times 9\) | \([1, 3, 5, 9]\) |
| 15 | \(135 = 9 \times 15\) | \([1, 3, 5, 9, 15]\) |
| 27 | \(135 = 5 \times 27\) | \([1, 3, 5, 9, 15, 27]\) |
| 45 | \(135 = 3 \times 45\) | \([1, 3, 5, 9, 15, 27, 45]\) |
| 135 | \(135 = 1 \times 135\) | \([1, 3, 5, 9, 15, 27, 45, 135]\) |
All \(j\) not listed (2, 4, 6, 7, 8, 10, 11, 12, 13, 14, 16, …, 134) leave the list unchanged. Number of checks: \(135\). Number of divisors found: \(8\).
| \(j\) | Check | list-of-divisors after the check |
|---|---|---|
| 1 | \(775 = 775 \times 1\) | \([1]\) |
| 5 | \(775 = 155 \times 5\) | \([1, 5]\) |
| 25 | \(775 = 31 \times 25\) | \([1, 5, 25]\) |
| 31 | \(775 = 25 \times 31\) | \([1, 5, 25, 31]\) |
| 155 | \(775 = 5 \times 155\) | \([1, 5, 25, 31, 155]\) |
| 775 | \(775 = 1 \times 775\) | \([1, 5, 25, 31, 155, 775]\) |
Number of checks: \(775\). Number of divisors found: \(6\). The divisors come in pairs: \[ \begin{aligned} 1 \times 775 &= 775 \\ 5 \times 155 &= 775 \\ 25 \times 31 &= 775 \end{aligned} \]
The algorithm always makes exactly \(n\) divisibility checks, because it never skips a number between 1 and \(n\). Comparing the three runs: \[ \begin{aligned} \text{checks for } 15 &= 15 \\ \text{checks for } 135 &= 135 = 9 \times 15 \\ \text{checks for } 775 &= 775 \approx 5.7 \times 135 \end{aligned} \] So the work grows in proportion to the value of \(n\), not to the number of digits used to write it: adding one digit multiplies the work by about 10.
Notice also that more work does not mean more divisors. \(135\) needs nine times as many checks as \(15\) and has twice as many divisors (\(8\) instead of \(4\)), but \(775\) is checked \(775\) times and has only \(6\) divisors. The checks grow with \(n\); the number of divisors grows much more slowly.
• \(\text{divisors}(15) = \mathbf{[1, 3, 5, 15]}\), using 15 checks.
• \(\text{divisors}(135) = \mathbf{[1, 3, 5, 9, 15, 27, 45, 135]}\), using 135 checks.
• \(\text{divisors}(775) = \mathbf{[1, 5, 25, 31, 155, 775]}\), using 775 checks.
• The work is proportional to \(n\) (about 10 times more for each extra digit); a larger number does not necessarily have more divisors.
Exercise Set 11.1 (Textbook Page 38)
Five questions testing the digit-by-digit addition algorithm of Section 11.1.1 — whether it is complete, how it handles unequal lengths, why alignment and carries behave as they do, and what the final carry step is for.
1. Write the numbers one below the other in the Indian base-ten place-value system so that the
digits are aligned from right to left.
2. Add the rightmost digits: if the sum is less than 10, write it below and set carry to 0; if
the sum is more than 10, write the units digit and set carry to 1.
3. Move one column left, add the two digits and the current carry, and apply the same rule.
4. Repeat Step 3 until there are no digits left.
5. If the carry is 1 at the end, write 1 to the left of the bottom row.
| Column | Digits added | Total | Digit written | Carry after the column |
|---|---|---|---|---|
| Units | \(8 + 7\) | 15 | 5 | 1 |
| Tens | \(6 + 5 + 1\) | 12 | 2 | 1 |
| Hundreds | \(4 + 3 + 1\) | 8 | 8 | 0 |
| Thousands | \(2 + 1 + 0\) | 3 | 3 | 0 |
No digits remain on the left and the carry is 0, so Step 5 does nothing: \[ \begin{aligned} 2468 + 1357 &= 3825 \\ \text{Check: } 3825 - 1357 &= 2468 \end{aligned} \] Here the steps give the correct result.
In the units column, \(6 + 4 = 10\). Step 2 gives an instruction for a sum less than 10 and another for a sum more than 10, but says nothing about a sum exactly equal to 10. A person who performs only the actions explicitly mentioned cannot decide what to write. So, followed perfectly literally, the algorithm is incomplete and stops here.
In Steps 2 and 3 read “more than 10” as “10 or more”. For a column total of 10 we then write 0 and carry 1. Applying the repaired algorithm:
| Column | Digits added | Total | Digit written | Carry after the column |
|---|---|---|---|---|
| Units | \(6 + 4\) | 10 | 0 | 1 |
| Tens | \(8 + 1 + 1\) | 10 | 0 | 1 |
| Hundreds | \(5 + 4 + 1\) | 10 | 0 | 1 |
| Thousands | \(4 + 3 + 1\) | 8 | 8 | 0 |
\[ \begin{aligned} 4586 + 3414 &= 8000 \\ \text{Check: } 8000 - 3414 &= 4586 \end{aligned} \]
Take \(12345 + 321\). After the units, tens and hundreds columns have been added, the 3-digit number has no digits left, while two columns of the 5-digit number remain. The written Step 3 says “add the two digits and the current value of carry” — but there is no second digit to add. Strictly, the instruction cannot be carried out, so the algorithm as printed does not handle this case.
Add: “If one number has no digit in the current column, treat that missing digit as 0.” Writing 321 as 00321 makes the two rows the same length:
| Column | Digits added | Total | Digit written | Carry |
|---|---|---|---|---|
| Units | \(5 + 1\) | 6 | 6 | 0 |
| Tens | \(4 + 2\) | 6 | 6 | 0 |
| Hundreds | \(3 + 3\) | 6 | 6 | 0 |
| Thousands | \(2 + 0\) | 2 | 2 | 0 |
| Ten-thousands | \(1 + 0\) | 1 | 1 | 0 |
\[ \begin{aligned} 12345 + 321 &= 12666 \\ \text{Check: } 12666 - 321 &= 12345 \end{aligned} \]
Take \(98765 + 678\). Writing \(678\) as \(00678\): \[ \begin{aligned} \text{units: } 5 + 8 &= 13 \Rightarrow 3, \text{ carry } 1 \\ \text{tens: } 6 + 7 + 1 &= 14 \Rightarrow 4, \text{ carry } 1 \\ \text{hundreds: } 7 + 6 + 1 &= 14 \Rightarrow 4, \text{ carry } 1 \\ \text{thousands: } 8 + 0 + 1 &= 9 \\ \text{ten-thousands: } 9 + 0 &= 9 \end{aligned} \] \[ \begin{aligned} 98765 + 678 &= 99443 \\ \text{Check: } 99443 - 678 &= 98765 \end{aligned} \] The pad-with-zeros rule works together with the “10 or more” repair from Question 1.
In a whole number the rightmost digit is the units digit, the next one to its left counts tens, then hundreds, and so on. Right alignment therefore puts units under units, tens under tens, hundreds under hundreds. Only digits with the same place value may be added together, because \(3\) tens plus \(5\) tens is \(8\) tens but \(3\) tens plus \(5\) units is not \(8\) of anything.
In \(12345 + 321\) the digit \(1\) of \(321\) is a unit, so it must stand below the \(5\) of \(12345\), which is also a unit. Right alignment is exactly the rule that guarantees this, for numbers of any length.
Suppose \(1234 + 56\) is aligned from the left instead of the right: \[ \begin{aligned} 1234 + 56 &= 1290 \quad \text{(correct, units under units)} \\ \text{left-aligned } 1234 + 56 &\rightarrow 6834 \quad \text{(wrong)} \end{aligned} \] In the wrong version the \(6\) of \(56\) lands under the hundreds digit \(2\), so it is read as \(6\) hundreds instead of \(6\) tens, and the \(5\) is read as \(5\) thousands. The digits were added correctly; the place values were not.
The same misalignment also destroys the carry: a carry must move to the next column on the left, which is the next higher place value only when the columns themselves are place-value columns.
Only two numbers are being added, so a column contains at most two digits. The largest digit is \(9\), and the carry arriving from the column on the right is at most \(1\). Hence the largest total a column can reach is \[ \begin{aligned} 9 + 9 + 1 &= 19 \end{aligned} \]
A total between 10 and 19 is written as \(1\) ten plus a units digit: \[ \begin{aligned} 10 &= 10 + 0 \\ 11 &= 10 + 1 \\ 12 &= 10 + 2 \\ &\;\;\vdots \\ 19 &= 10 + 9 \end{aligned} \] In every case the tens part is exactly \(1\), so the carry passed to the next column is \(1\). A total below 10 contributes no ten at all, so the carry is \(0\). Therefore the carry can only ever be \(0\) or \(1\).
In the units column there is no incoming carry, so the total is at most \(9 + 9 = 18\), again producing a carry of at most \(1\). Since the first carry is \(0\) or \(1\), and any carry that is \(0\) or \(1\) produces a new carry that is \(0\) or \(1\), every column of the whole sum has carry \(0\) or \(1\). The bound never breaks down.
Step 5 says: if the value of carry is 1 after the leftmost column, write 1 to the left of the bottom row. That carry is a genuine new place value — for example the thousands digit of a sum of two 3-digit numbers, or a new leading digit of a sum of two 4-digit numbers. Omitting Step 5 throws that digit away, so the answer is correct exactly when the final carry is 0.
\(1234 + 4321\): \[ \begin{aligned} \text{units: } 4 + 1 &= 5 \\ \text{tens: } 3 + 2 &= 5 \\ \text{hundreds: } 2 + 3 &= 5 \\ \text{thousands: } 1 + 4 &= 5 \end{aligned} \] No column reaches 10, the final carry is 0, so Step 5 would do nothing: \[ \begin{aligned} 1234 + 4321 &= \mathbf{5555} \end{aligned} \] Leaving Step 5 out changes nothing here.
\(9999 + 9999\):
| Column | Digits added | Total | Digit written | Carry |
|---|---|---|---|---|
| Units | \(9 + 9\) | 18 | 8 | 1 |
| Tens | \(9 + 9 + 1\) | 19 | 9 | 1 |
| Hundreds | \(9 + 9 + 1\) | 19 | 9 | 1 |
| Thousands | \(9 + 9 + 1\) | 19 | 9 | 1 |
After the thousands column the written digits are \(9998\) and a carry of \(1\) is still waiting. With Step 5: \[ \begin{aligned} 9999 + 9999 &= 19998 \end{aligned} \] Without Step 5 the carry is simply lost and the algorithm reports \(9998\), which is wrong by exactly \(10000\). Note also that the carry here is \(1\), never \(2\), which confirms the answer to Question 4.
Exercise Set 11.2 (Textbook Page 43)
Algorithms built on the divisor-listing idea of Section 11.2: comparing two sorted lists, computing the lcm from lists of multiples, and the factor-pair shortcut that replaces \(n\) checks by about \(\sqrt{n}\) checks.
(i) Write an algorithm to find elements in List 1 that are not present in List 2.
(ii) Write an algorithm to find elements in List 2 that are not present in List 1.
We need the two “set differences” of the lists: the elements of List 1 with no copy in List 2, and the elements of List 2 with no copy in List 1. Both lists are given in increasing order, and we use the names result for the output list, and \(i\), \(j\) for the positions we are currently looking at in List 1 and List 2.
1. Start with an empty list called result.
2. Put a marker on the first element of List 1, call its value \(x\), and a marker on the first
element of List 2, call its value \(y\).
3. If the marker on List 1 has passed the last element, stop and report result.
4. While the marker on List 2 has not passed the last element and \(y < x\), move the
List 2 marker one step right (these \(y\) values are too small to equal \(x\), and because the
list increases they can never equal \(x\) later).
5. If the marker on List 2 has passed the last element, add \(x\) to result, move the
List 1 marker one step right, and go to Step 3.
6. If \(y = x\), then \(x\) occurs in List 2: move both markers one step right and go to
Step 3.
7. If \(y > x\), then \(x\) does not occur in List 2: add \(x\) to result, move only
the List 1 marker one step right, and go to Step 3.
| Step | \(x\) (List 1) | \(y\) (List 2) | Comparison | Action | result after the step |
|---|---|---|---|---|---|
| 1 | 1 | 1 | \(x = y\) | move both markers | \([\,]\) |
| 2 | 2 | 3 | \(x < y\) | add 2, move List 1 marker | \([2]\) |
| 3 | 3 | 3 | \(x = y\) | move both markers | \([2]\) |
| 4 | 6 | 5 | \(x > y\) | move List 2 marker to 9 | \([2]\) |
| 5 | 6 | 9 | \(x < y\) | add 6, move List 1 marker | \([2, 6]\) |
| 6 | — | 9 | List 1 finished | stop | \([2, 6]\) |
\[ \begin{aligned} \text{List 1} \setminus \text{List 2} &= \mathbf{[2, 6]} \end{aligned} \] Check: \(2\) and \(6\) do occur in List 1, and neither occurs in \([1, 3, 5, 9]\).
The same procedure with the two roles exchanged. It is not a different method, only a
relabelling:
1. Start with an empty list called result.
2. Put a marker on the first element \(y\) of List 2 and a marker on the first element \(x\) of
List 1.
3. If the marker on List 2 has passed the last element, stop and report result.
4. While the List 1 marker has not passed the last element and \(x < y\), move the List 1
marker one step right.
5. If the List 1 marker has passed the last element, add \(y\) to result, move the
List 2 marker, and go to Step 3.
6. If \(x = y\), move both markers and go to Step 3.
7. If \(x > y\), add \(y\) to result, move only the List 2 marker, and go to Step 3.
| Step | \(y\) (List 2) | \(x\) (List 1) | Comparison | Action | result after the step |
|---|---|---|---|---|---|
| 1 | 1 | 1 | \(y = x\) | move both markers | \([\,]\) |
| 2 | 3 | 2 | \(y > x\) | move List 1 marker to 3 | \([\,]\) |
| 3 | 3 | 3 | \(y = x\) | move both markers | \([\,]\) |
| 4 | 5 | 6 | \(y < x\) | add 5, move List 2 marker | \([5]\) |
| 5 | 9 | 6 | \(y > x\) | move List 1 marker past the end | \([5]\) |
| 6 | 9 | — | List 1 finished | add 9, move List 2 marker | \([5, 9]\) |
| 7 | — | — | List 2 finished | stop | \([5, 9]\) |
\[ \begin{aligned} \text{List 2} \setminus \text{List 1} &= \mathbf{[5, 9]} \end{aligned} \] Both outputs come out in increasing order, because each list is scanned once from left to right and elements are appended in the order they are met.
The naive version — “for each \(x\) in List 1, search the whole of List 2” — makes about \(4 \times 4 = 16\) comparisons on these lists. The marker version makes 6 comparisons for part (i) and 7 for part (ii), because no element of either list is ever examined twice: \[ \begin{aligned} \text{comparisons for (i)} &= 6 \\ \text{comparisons for (ii)} &= 7 \\ \text{worst case for lists of sizes } p, q &= p + q \end{aligned} \] This is the same “do not repeat work” idea that later turns the divisor scan into Euclid’s algorithm.
• (i) Walk both lists with one marker each; when the List 1 value is smaller, it is missing — append it; when the values are equal, move both; when the List 2 value is smaller, move only List 2. Output for the example: \(\mathbf{[2, 6]}\).
• (ii) The mirror image of the same procedure: \(\mathbf{[5, 9]}\).
A common multiple of \(m\) and \(n\) is a number that appears in both the list of multiples of \(m\) and the list of multiples of \(n\). The least common multiple is therefore the first number that the two lists share. Since \(\mathrm{lcm}(m,n) \le mn\), it is enough to write down the first \(n\) multiples of \(m\) and the first \(m\) multiples of \(n\): both lists then contain \(mn\), so a common entry is certain to be found.
1. Take the two positive numbers \(m\) and \(n\).
2. Build List A \(= [m, 2m, 3m, \dots, nm]\), the first \(n\) multiples of \(m\).
3. Build List B \(= [n, 2n, 3n, \dots, mn]\), the first \(m\) multiples of \(n\).
4. Look at the entries of List A from left to right.
5. For each entry, check whether it also occurs in List B.
6. As soon as an entry occurs in both lists, report it as the lcm and stop.
The lists are increasing, so the first shared entry is the smallest shared entry. The lists always share at least \(mn\), so the search can never fail.
Here \(mn = 24\), so List A has the first 6 multiples of 4 and List B the first 4 multiples of 6: \[ \begin{aligned} \text{List A} &= [4, 8, 12, 16, 20, 24] \\ \text{List B} &= [6, 12, 18, 24] \end{aligned} \] Scanning List A:
| Entry of List A | Present in List B? | Action |
|---|---|---|
| 4 | no | move on |
| 8 | no | move on |
| 12 | yes (\(2 \times 6\)) | report 12 and stop |
\[ \begin{aligned} \mathrm{lcm}(4, 6) &= \mathbf{12} \end{aligned} \] Check with the product rule \(\mathrm{lcm}(m,n)\times \gcd(m,n) = mn\): \[ \begin{aligned} \gcd(4,6) &= 2 \\ \mathrm{lcm}(4,6) &= \frac{4 \times 6}{2} \\ &= 12 \end{aligned} \]
\[ \begin{aligned} \text{List A} &= [12, 24, 36, 48, \dots, 216] \\ \text{List B} &= [18, 36, 54, \dots, 216] \end{aligned} \] 12 is not in List B, 24 is not in List B, and 36 is the second multiple of 18, so \[ \begin{aligned} \mathrm{lcm}(12, 18) &= \mathbf{36} \end{aligned} \] Here the algorithm had to build \(18 + 12 = 30\) entries and compare 3 of them before stopping — much less work than listing all divisors.
(i) If we write out divisors in pairs, how many numbers do we have to examine between 1 and \(n\) to find all the divisors of \(n\)?
(ii) If we list out the divisors in pairs, will our gcd algorithm still work in the manner we have described?
Suppose \(j\) divides \(n\), say \(n = j \times k\). Then both \(j\) and \(k\) are divisors, and the two are called a factor pair. If both members of a pair were larger than \(\sqrt{n}\), their product would satisfy \[ \begin{aligned} j \times k &> \sqrt{n}\times \sqrt{n} \\ &= n \end{aligned} \] which is impossible, since the product is exactly \(n\). Hence at least one member of every pair is at most \(\sqrt{n}\).
It is enough to test \[ \begin{aligned} j &= 1, 2, 3, \dots, \lfloor \sqrt{n} \rfloor \end{aligned} \] because each such \(j\) that divides \(n\) delivers the whole pair \((j,\, n \div j)\). So the number of examinations is \(\lfloor \sqrt{n} \rfloor\), instead of \(n\).
Running it for \(n = 18\):
| \(j\) | Does \(j\) divide 18? | Pair produced | Divisors collected so far |
|---|---|---|---|
| 1 | yes | \((1, 18)\) | \(1, 18\) |
| 2 | yes | \((2, 9)\) | \(1, 18, 2, 9\) |
| 3 | yes | \((3, 6)\) | \(1, 18, 2, 9, 3, 6\) |
| 4 | no | — | \(1, 18, 2, 9, 3, 6\) |
Since \(4^2 = 16 \le 18 < 25 = 5^2\), we stop at \(j = 4\). Only 4 numbers were examined instead of 18, and sorting the collected values gives \[ \begin{aligned} \text{divisors}(18) &= [1, 2, 3, 6, 9, 18] \end{aligned} \]
If \(n\) is a perfect square, the pair \(( \sqrt{n}, \sqrt{n})\) has two equal members and must contribute the divisor only once. For \(n = 36\), since \(\sqrt{36} = 6\), we examine \(j = 1, \dots, 6\):
| \(j\) | Pair produced | Divisors collected so far |
|---|---|---|
| 1 | \((1, 36)\) | \(1, 36\) |
| 2 | \((2, 18)\) | \(1, 36, 2, 18\) |
| 3 | \((3, 12)\) | \(1, 36, 2, 18, 3, 12\) |
| 4 | \((4, 9)\) | \(1, 36, 2, 18, 3, 12, 4, 9\) |
| 5 | none (5 does not divide 36) | \(1, 36, 2, 18, 3, 12, 4, 9\) |
| 6 | \((6, 6)\) — record 6 once only | \(1, 36, 2, 18, 3, 12, 4, 9, 6\) |
\[ \begin{aligned} \text{divisors}(36) &= [1, 2, 3, 4, 6, 9, 12, 18, 36] \end{aligned} \] That is \(9\) divisors, not \(10\): the repeated 6 is counted once. So the number of examinations is \(6\) (that is \(\lfloor \sqrt{36}\rfloor\)) instead of \(36\).
The gcd algorithm of Section 11.2.3 reports the rightmost element of the list of common divisors. That is safe only because both divisor lists were built by checking \(1, 2, 3, \dots, n\) in order, so they are increasing. A paired list is not increasing.
\[ \begin{aligned} \text{paired list for } 12 &= [1, 12, 2, 6, 3, 4] \\ \text{paired list for } 18 &= [1, 18, 2, 9, 3, 6] \end{aligned} \] Walk through the first list and keep the entries that also occur in the second:
| Entry of the paired list of 12 | In the paired list of 18? | common-divisors so far |
|---|---|---|
| 1 | yes | \([1]\) |
| 12 | no | \([1]\) |
| 2 | yes | \([1, 2]\) |
| 6 | yes | \([1, 2, 6]\) |
| 3 | yes | \([1, 2, 6, 3]\) |
| 4 | no | \([1, 2, 6, 3]\) |
The rightmost entry is \(3\), but the greatest common divisor is \(6\): \[ \begin{aligned} \gcd(12, 18) &= 2 \times 3 \\ &= \mathbf{6} \end{aligned} \] So the answer would be reported as \(3\), which is wrong. The algorithm can be repaired in two ways: sort the paired list before using it, or scan the whole common-divisor list and report its maximum instead of its rightmost entry.
• (i) Only \(\mathbf{\lfloor \sqrt{n} \rfloor}\) numbers must be examined — \(j = 1\) to \(\lfloor \sqrt{n}\rfloor\) — because each divisor \(j\) yields its partner \(n \div j\). For \(n = 18\) that is 4 checks instead of 18; for \(n = 36\), 6 checks instead of 36 (counting the pair \((6,6)\) once).
• (ii) No. A paired list is not in increasing order, so “the rightmost common divisor” is no longer the greatest: for 12 and 18 the paired lists give \(3\), whereas \(\gcd(12,18) = \mathbf{6}\).
Exercise Set 11.3 (Textbook Page 45)
Two questions on reversing the scan direction of the gcd algorithms: how the lists, the position reported and the stopping rule all change when we count downwards.
The only change is the order of the checks: \(n, n-1, \dots, 1\) instead of \(1, 2, \dots, n\). Each new divisor is still appended at the end, so each divisor list now comes out in decreasing order, and its first entry is \(n\) itself.
\[ \begin{aligned} \text{divisors}(12) \text{ downwards} &= [12, 6, 4, 3, 2, 1] \\ \text{divisors}(18) \text{ downwards} &= [18, 9, 6, 3, 2, 1] \end{aligned} \]
1. Build divisors-of-\(m\) by testing \(j = m, m-1, \dots, 1\) and appending each divisor as it
is found.
2. Build divisors-of-\(n\) the same way, testing \(j = n, n-1, \dots, 1\).
3. Start with an empty list called common-divisors.
4. For each entry \(x\) of divisors-of-\(m\), in the order listed, check whether \(x\) occurs
in divisors-of-\(n\); if it does, append \(x\) to common-divisors.
5. Report the leftmost entry of common-divisors as \(\gcd(m,n)\).
Step 5 changes from “rightmost” to “leftmost”, because the lists are now decreasing: the largest common divisor is the first one met, not the last.
| Entry of divisors(12) downwards | Occurs in divisors(18) downwards? | common-divisors so far |
|---|---|---|
| 12 | no | \([\,]\) |
| 6 | yes | \([6]\) |
| 4 | no | \([6]\) |
| 3 | yes | \([6, 3]\) |
| 2 | yes | \([6, 3, 2]\) |
| 1 | yes | \([6, 3, 2, 1]\) |
\[ \begin{aligned} \gcd(12, 18) &= \mathbf{6} \end{aligned} \] The leftmost entry is \(6\), which is correct. Moreover the scan could have stopped the moment \(6\) was found, since a decreasing scan meets the greatest common divisor first.
| Entry of divisors(375) downwards | Occurs in divisors(825)? | Action |
|---|---|---|
| 375 | no | move on |
| 125 | no | move on |
| 75 | yes | stop and report 75 |
\[ \begin{aligned} \text{divisors}(375) &= [375, 125, 75, 25, 15, 5, 3, 1] \\ \text{divisors}(825) &= [825, 275, 165, 75, 55, 33, 25, 15, 11, 5, 3, 1] \\ \gcd(375, 825) &= \mathbf{75} \end{aligned} \] Only three membership tests were needed. The increasing-order scan meets 75 only after passing \(1\), \(3\), \(5\), \(15\) and \(25\), so it spends six tests to reach the same place — and if it insisted on the rightmost common divisor it would use all eight.
In the final refinement of Section 11.3.1 the list is dropped altogether: a single name, most-recent-common-divisor, is updated to \(k\) whenever \(k\) divides both \(m\) and \(n\), and the scan runs \(k = 2, 3, \dots, \min(m,n)\). At the end that name holds the gcd, because the scan meets larger and larger common divisors.
Start at \(k = \min(m,n)\) and decrease \(k\) by 1 each time. The first \(k\) that divides both numbers is immediately the greatest common divisor — nothing larger can divide both, because we have already tested every larger value. So the moment a common divisor is found, it can be reported and the algorithm stops; no list and no “most recent” value are needed at all.
1. Let \(k = \min(m,n)\).
2. If \(k\) divides both \(m\) and \(n\), report \(k\) as the gcd and stop.
3. Otherwise decrease \(k\) by 1 and return to Step 2.
4. The scan always terminates, because \(k = 1\) divides both \(m\) and \(n\).
| \(k\) | Divides 12? | Divides 18? | Action |
|---|---|---|---|
| 12 | yes | no | decrease to 11 |
| 11 | no | no | decrease to 10 |
| 10 | no | no | decrease to 9 |
| 9 | no | yes | decrease to 8 |
| 8 | no | no | decrease to 7 |
| 7 | no | no | decrease to 6 |
| 6 | yes | yes | report 6 and stop |
\[ \begin{aligned} \gcd(12, 18) &= \mathbf{6} \\ \text{checks used} &= 7 \end{aligned} \]
| \(k\) | Divides 60? | Divides 16? | Action |
|---|---|---|---|
| 16 | no | yes | decrease to 15 |
| 15 | yes | no | decrease to 14 |
| 14 | no | no | decrease to 13 |
| 13 | no | no | decrease to 12 |
| 12 | yes | no | decrease to 11 |
| 11 | no | no | decrease to 10 |
| 10 | yes | no | decrease to 9 |
| 9 | no | no | decrease to 8 |
| 8 | no | yes | decrease to 7 |
| 7 | no | no | decrease to 6 |
| 6 | yes | no | decrease to 5 |
| 5 | yes | no | decrease to 4 |
| 4 | yes | yes | report 4 and stop |
\[ \begin{aligned} \gcd(60, 16) &= \mathbf{4} \\ \text{checks used} &= 13 \end{aligned} \]
The upwards scan of Section 11.3.1 always uses \(\min(m,n) - 1\) steps, because it must visit every value up to \(\min(m,n)\). The downwards scan stops at the gcd, so it uses \(\min(m,n) - \gcd(m,n) + 1\) steps: \[ \begin{aligned} \gcd(12,18): \quad 12 - 6 + 1 &= 7 \text{ checks} \\ \gcd(60,16): \quad 16 - 4 + 1 &= 13 \text{ checks} \\ \gcd(375,825): \quad 375 - 75 + 1 &= 301 \text{ checks} \end{aligned} \] When the gcd is small the saving is small — for coprime numbers such as \(12\) and \(35\) the scan still runs almost all the way down to 1. So reversing the direction helps a little, but the number of steps still grows with the value of \(\min(m,n)\), not with its number of digits. That is exactly the weakness that Euclid’s subtraction algorithm, and then Āryabhaṭa’s division algorithm, are designed to remove.
End-of-Chapter Exercises (Textbook Page 50)
Five questions closing the chapter: four full runs of Āryabhaṭa’s division form of Euclid’s algorithm, the proof that \(\gcd(m,n) = \gcd(n, m \bmod n)\), and algorithms for primality, prime divisors and prime factorisation.
(i) \(\gcd(375, 825)\)
(ii) \(\gcd(51000, 81000)\)
(iii) \(\gcd(1789287, 237656)\)
(iv) \(\gcd(2587392, 157656)\)
1. If \(m < n\), interchange them so that \(m \ge n\).
2. If \(n = 0\), report \(m\) and stop.
3. Otherwise divide \(m\) by \(n\): write \(m = b \cdot q + r\) with \(0 \le r < n\), and
replace the pair \((a,b)\) by \((b,r)\); return to Step 2.
Each replacement is one division, and the last non-zero remainder is the answer. The reason the replacement is allowed is proved in Question 2: \[ \begin{aligned} \gcd(m, n) &= \gcd(n,\, m \bmod n) \end{aligned} \] Every step keeps the gcd unchanged while making the pair of numbers smaller, so the process must stop — and it stops at a remainder of 0, whose partner is the gcd.
Here \(375 < 825\), so start with the pair \((825, 375)\). \[ \begin{aligned} 825 &= 375 \times 2 + 75 \\ 375 &= 75 \times 5 + 0 \end{aligned} \]
| Step | \(a\) | \(b\) | Quotient \(q\) | Remainder \(r\) |
|---|---|---|---|---|
| 1 | 825 | 375 | 2 | 75 |
| 2 | 375 | 75 | 5 | 0 |
The last non-zero remainder is \(75\): \[ \begin{aligned} \gcd(375, 825) &= \mathbf{75} \end{aligned} \] Check by prime factorisation: \(375 = 3 \times 5^3\) and \(825 = 3 \times 5^2 \times 11\), so the common part is \(3 \times 5^2 = 75\). ✔
\[ \begin{aligned} 81000 &= 51000 \times 1 + 30000 \\ 51000 &= 30000 \times 1 + 21000 \\ 30000 &= 21000 \times 1 + 9000 \\ 21000 &= 9000 \times 2 + 3000 \\ 9000 &= 3000 \times 3 + 0 \end{aligned} \]
| Step | \(a\) | \(b\) | Quotient \(q\) | Remainder \(r\) |
|---|---|---|---|---|
| 1 | 81000 | 51000 | 1 | 30000 |
| 2 | 51000 | 30000 | 1 | 21000 |
| 3 | 30000 | 21000 | 1 | 9000 |
| 4 | 21000 | 9000 | 2 | 3000 |
| 5 | 9000 | 3000 | 3 | 0 |
\[ \begin{aligned} \gcd(51000, 81000) &= \mathbf{3000} \end{aligned} \] Check: \(51000 = 2^3 \times 3 \times 5^3 \times 17\) and \(81000 = 2^3 \times 3^4 \times 5^3\), so the common part is \(2^3 \times 3 \times 5^3 = 8 \times 3 \times 125 = 3000\). ✔
Here \(1789287 > 237656\), so no interchange is needed. \[ \begin{aligned} 1789287 &= 237656 \times 7 + 125695 \\ 237656 &= 125695 \times 1 + 111961 \\ 125695 &= 111961 \times 1 + 13734 \\ 111961 &= 13734 \times 8 + 2089 \\ 13734 &= 2089 \times 6 + 1200 \\ 2089 &= 1200 \times 1 + 889 \\ 1200 &= 889 \times 1 + 311 \\ 889 &= 311 \times 2 + 267 \\ 311 &= 267 \times 1 + 44 \\ 267 &= 44 \times 6 + 3 \\ 44 &= 3 \times 14 + 2 \\ 3 &= 2 \times 1 + 1 \\ 2 &= 1 \times 2 + 0 \end{aligned} \]
| Step | \(a\) | \(b\) | Quotient \(q\) | Remainder \(r\) |
|---|---|---|---|---|
| 1 | 1789287 | 237656 | 7 | 125695 |
| 2 | 237656 | 125695 | 1 | 111961 |
| 3 | 125695 | 111961 | 1 | 13734 |
| 4 | 111961 | 13734 | 8 | 2089 |
| 5 | 13734 | 2089 | 6 | 1200 |
| 6 | 2089 | 1200 | 1 | 889 |
| 7 | 1200 | 889 | 1 | 311 |
| 8 | 889 | 311 | 2 | 267 |
| 9 | 311 | 267 | 1 | 44 |
| 10 | 267 | 44 | 6 | 3 |
| 11 | 44 | 3 | 14 | 2 |
| 12 | 3 | 2 | 1 | 1 |
| 13 | 2 | 1 | 2 | 0 |
The last non-zero remainder is \(1\), so the two numbers are coprime: \[ \begin{aligned} \gcd(1789287, 237656) &= \mathbf{1} \end{aligned} \] This took \(13\) divisions. The subtraction form of Euclid’s algorithm would have needed \(7+1+1+8+6+1+1+2+1+6+14+1+2 = 51\) subtractions on the same pair — the sum of the quotients above — which is exactly the kind of saving the division step is designed to give.
\[ \begin{aligned} 2587392 &= 157656 \times 16 + 64896 \\ 157656 &= 64896 \times 2 + 27864 \\ 64896 &= 27864 \times 2 + 9168 \\ 27864 &= 9168 \times 3 + 360 \\ 9168 &= 360 \times 25 + 168 \\ 360 &= 168 \times 2 + 24 \\ 168 &= 24 \times 7 + 0 \end{aligned} \]
| Step | \(a\) | \(b\) | Quotient \(q\) | Remainder \(r\) |
|---|---|---|---|---|
| 1 | 2587392 | 157656 | 16 | 64896 |
| 2 | 157656 | 64896 | 2 | 27864 |
| 3 | 64896 | 27864 | 2 | 9168 |
| 4 | 27864 | 9168 | 3 | 360 |
| 5 | 9168 | 360 | 25 | 168 |
| 6 | 360 | 168 | 2 | 24 |
| 7 | 168 | 24 | 7 | 0 |
\[ \begin{aligned} \gcd(2587392, 157656) &= \mathbf{24} \end{aligned} \] Check: \(2587392 = 24 \times 107808\) and \(157656 = 24 \times 6569\), and \(6569\) has no common factor with \(107808\). ✔
| Problem | Divisions (Āryabhaṭa) | Subtractions (Euclid) | GCD |
|---|---|---|---|
| \(\gcd(375, 825)\) | 2 | \(2+5=7\) | 75 |
| \(\gcd(51000, 81000)\) | 5 | \(1+1+1+2+3=8\) | 3000 |
| \(\gcd(1789287, 237656)\) | 13 | 51 | 1 |
| \(\gcd(2587392, 157656)\) | 7 | \(16+2+2+3+25+2+7=57\) | 24 |
The chapter’s warning example is \(\gcd(99, 2)\). Subtraction has to walk down one step of 2 at a time: \[ \begin{aligned} \gcd(99,2) &\to \gcd(97,2) \to \gcd(95,2) \to \cdots \\ &\to \gcd(3,2) \to \gcd(2,1) \to \gcd(1,1) \to \gcd(1,0) \\ \text{reductions} &= 51 \end{aligned} \] Division reaches the same answer at once: \[ \begin{aligned} 99 &= 2 \times 49 + 1 \\ 2 &= 1 \times 2 + 0 \\ \text{divisions} &= 2 \end{aligned} \] Comparing the two versions on the chapter’s own examples:
| Algorithm | Steps for \(\gcd(99,2)\) | Steps for \(\gcd(375,825)\) |
|---|---|---|
| Euclid’s subtraction algorithm | 51 reductions | 7 reductions |
| Āryabhaṭa’s division algorithm | 2 divisions | 2 divisions |
In general \(\gcd(2k+1, 2)\) needs about \(k\) reductions of the subtraction algorithm — \(51\) of them when \(k = 49\) — but only two divisions of the division algorithm. So the number of subtraction steps grows with the value of the numbers, while the number of division steps grows only with the number of digits.
• (i) \(\gcd(375, 825) = \mathbf{75}\) — 2 divisions.
• (ii) \(\gcd(51000, 81000) = \mathbf{3000}\) — 5 divisions.
• (iii) \(\gcd(1789287, 237656) = \mathbf{1}\) — 13 divisions.
• (iv) \(\gcd(2587392, 157656) = \mathbf{24}\) — 7 divisions.
Here \(m \bmod n\) means the remainder left when \(m\) is divided by \(n\). This step needs \(n > 0\); when \(n = 0\) the division algorithm stops and reports \(m\), so no remainder is ever needed.
By the division lemma there are whole numbers \(q\) and \(r\) with \[ \begin{aligned} m &= qn + r, \qquad 0 \le r < n \\ \text{so } r &= m - qn \\ \text{and } r &= m \bmod n \end{aligned} \] Both assertions to be proved are about the same \(d\), an arbitrary positive whole number.
Assume \(d \mid m\) and \(d \mid n\). Then there are whole numbers \(a\) and \(b\) with \[ \begin{aligned} m &= ad \\ n &= bd \end{aligned} \] Substitute into \(r = m - qn\): \[ \begin{aligned} r &= m - qn \\ &= ad - q(bd) \\ &= (a - qb)\,d \end{aligned} \] Since \(a - qb\) is a whole number, \(d \mid r\). We already assumed \(d \mid n\), so \(d\) divides both \(n\) and \(r\). \(\square\)
Assume \(d \mid n\) and \(d \mid r\). Then there are whole numbers \(b\) and \(c\) with \[ \begin{aligned} n &= bd \\ r &= cd \end{aligned} \] Substitute into \(m = qn + r\): \[ \begin{aligned} m &= qn + r \\ &= q(bd) + cd \\ &= (qb + c)\,d \end{aligned} \] So \(d \mid m\). Together with the assumed \(d \mid n\), this says \(d\) divides both \(m\) and \(n\). \(\square\)
Steps 2 and 3 together show \[ \begin{aligned} \{d : d \mid m \text{ and } d \mid n\} &= \{d : d \mid n \text{ and } d \mid r\} \end{aligned} \] Two sets with exactly the same elements have the same largest element, hence \[ \begin{aligned} \gcd(m, n) &= \gcd(n, m \bmod n) \end{aligned} \] The right-hand pair is smaller, because \(0 \le r < n\). Repeating the replacement must therefore terminate, and when the remainder becomes 0 the pair is \((g, 0)\), whose gcd is \(g\) — the last non-zero remainder used in Question 1.
A numerical check with \(m = 375\), \(n = 825\) (after interchange \(m = 825\), \(n = 375\)): \[ \begin{aligned} 825 \bmod 375 &= 75 \\ \gcd(825, 375) &= 75 \\ \gcd(375, 75) &= 75 \end{aligned} \] The gcd is unchanged by the replacement, as claimed.
(Hint: A prime number \(p\) has exactly two distinct factors, 1 and \(p\). Can you make use of \(\text{divisors}(n)\) to write out \(\text{prime}(n)\)?)
A prime is a whole number greater than 1 whose only positive divisors are 1 and itself. So the test is a counting test: list all positive divisors of \(n\), then count them. Exactly two divisors means prime; any other count means not prime. The list \(\text{divisors}(n)\) of Section 11.2.1 already does the hard part.
1. Take the input \(n\).
2. If \(n < 2\), report “not prime” and stop.
3. Compute the list \(\text{divisors}(n)\) by testing every \(j\) from 1 to \(n\).
4. Count the number of entries in that list; call the count \(c\).
5. If \(c = 2\), report “prime”.
6. Otherwise report “not prime”.
Step 2 is essential: without it, \(n = 1\) would have the single divisor 1, and \(n = 0\) would have infinitely many divisors, so neither could be handled by a simple count.
Since a diagram cannot be drawn here, the flowchart is given as the sequence of boxes it
contains; each arrow goes from one box to the next.
Box 1 (Start): begin.
Box 2 (Input): read \(n\).
Box 3 (Decision): is \(n < 2\)?
• Yes → Box 4 (Output): “not prime” → Box 9 (Stop).
• No → Box 5 (Process): compute \(\text{divisors}(n)\).
Box 6 (Process): count the entries, store the count in \(c\).
Box 7 (Decision): is \(c = 2\)?
• Yes → Box 8a (Output): “prime” → Box 9 (Stop).
• No → Box 8b (Output): “not prime” → Box 9 (Stop).
Box 9 (Stop): end.
| \(n\) | \(\text{divisors}(n)\) | Count \(c\) | Output |
|---|---|---|---|
| 1 | \([1]\) | 1 | not prime (caught by Step 2) |
| 2 | \([1, 2]\) | 2 | prime |
| 7 | \([1, 7]\) | 2 | prime |
| 9 | \([1, 3, 9]\) | 3 | not prime |
| 15 | \([1, 3, 5, 15]\) | 4 | not prime |
| 97 | \([1, 97]\) | 2 | prime |
Check on 15: the divisors are exactly \(1, 3, 5, 15\), and there are four of them, so 15 is composite. Check on 97: no number from 2 to 9 divides 97 (\(97 = 9 \times 10 + 7\)), so its only divisors are 1 and 97, and the count is 2.
Building \(\text{divisors}(n)\) costs \(n\) checks. Using the factor-pair idea of Exercise 11.2 Question 3, Step 3 can be replaced by: test only \(j = 1, 2, \dots, \lfloor\sqrt{n}\rfloor\), and if no such \(j > 1\) divides \(n\), report “prime”. For \(n = 97\) that is 9 checks instead of 97, and the answers are identical: \[ \begin{aligned} \lfloor \sqrt{97} \rfloor &= 9 \\ \text{no } j \in \{2, 3, \dots, 9\} &\text{ divides } 97 \\ \Rightarrow 97 &\text{ is prime} \end{aligned} \]
(Hint: Compute \(\text{divisors}(n)\) and then filter out the primes in this list.)
Two ideas are combined: the divisor-listing algorithm of Section 11.2.1 produces every positive divisor, and the primality test \(\text{prime}(d)\) of Question 3 decides which of them to keep. A filter keeps the order of the list it filters, so the output is automatically increasing.
1. Compute \(\text{divisors}(n)\), the list of all positive divisors of \(n\).
2. Start with an empty list called prime-divisors.
3. Take the divisors one at a time, from left to right; call the current one \(d\).
4. Run \(\text{prime}(d)\) from Question 3.
5. If the answer is “prime”, append \(d\) to prime-divisors; if not, discard \(d\).
6. When every divisor has been tested, report prime-divisors.
Since \(180 = 2^2 \times 3^2 \times 5\), it has \((2+1)(2+1)(1+1) = 18\) divisors: \[ \begin{aligned} \text{divisors}(180) &= [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, \\ &\quad\; 20, 30, 36, 45, 60, 90, 180] \end{aligned} \]
| \(d\) | \(\text{prime}(d)\)? | prime-divisors after the test |
|---|---|---|
| 1 | not prime | \([\,]\) |
| 2 | prime | \([2]\) |
| 3 | prime | \([2, 3]\) |
| 4 | not prime | \([2, 3]\) |
| 5 | prime | \([2, 3, 5]\) |
| 6 | not prime | \([2, 3, 5]\) |
| 9 | not prime | \([2, 3, 5]\) |
| 10 | not prime | \([2, 3, 5]\) |
| 12 | not prime | \([2, 3, 5]\) |
| 15 | not prime | \([2, 3, 5]\) |
| 18 | not prime | \([2, 3, 5]\) |
| 20, 30, 36, 45, 60, 90, 180 | none is prime | \([2, 3, 5]\) |
\[ \begin{aligned} \text{primedivisors}(180) &= \mathbf{[2, 3, 5]} \end{aligned} \] Check that each entry really divides 180: \[ \begin{aligned} 180 \div 2 &= 90 \\ 180 \div 3 &= 60 \\ 180 \div 5 &= 36 \end{aligned} \]
For \(n = 1\) the divisor list is \([1]\), and 1 is not prime, so the filter keeps nothing: \[ \begin{aligned} \text{primedivisors}(1) &= [\,] \end{aligned} \] For a prime input such as \(n = 13\), the divisor list is \([1, 13]\); the 1 is rejected and the 13 is kept, so \(\text{primedivisors}(13) = [13]\). A prime divides itself, so every prime is its own prime divisor.
Note also what the output does not say: each prime appears once, with no information about how many times it divides \(n\). That extra information is exactly what Question 5 adds.
(i) The prime factorisation of 180 is \(2^2 \times 3^2 \times 5^1\). How would you represent this?
(ii) How would you compare the prime factorisations of two numbers?
Every number \(n > 1\) can be written as a product of primes, and the primes and their exponents are unique. So the factorisation is completely described by a list of pairs \((p, c)\), where \(p\) is a prime and \(c\) is the number of times it occurs. For 180: \[ \begin{aligned} 180 &= 2 \times 2 \times 3 \times 3 \times 5 \\ &= 2^2 \times 3^2 \times 5^1 \end{aligned} \]
1. Take the input \(n\) and set remaining \(= n\).
2. Compute \(\text{primedivisors}(n)\), the list of primes that divide \(n\).
3. Start with an empty list called factorisation.
4. For each prime \(p\) in that list, in increasing order:
(a) set count \(= 0\);
(b) while \(p\) divides remaining, replace remaining by
remaining \(\div p\) and increase count by 1;
(c) append the pair \((p, \text{count})\) to factorisation.
5. When every prime in the list has been used, report factorisation.
Each division removes one copy of the prime, so the final count is the exponent of \(p\). Once all the listed primes are used, remaining must equal 1, which is the check that nothing was missed.
| Prime \(p\) | Divisions of remaining | Count | Pair appended |
|---|---|---|---|
| 2 | \(180 \div 2 = 90\), then \(90 \div 2 = 45\); 45 is odd | 2 | \((2, 2)\) |
| 3 | \(45 \div 3 = 15\), then \(15 \div 3 = 5\); 5 is not a multiple of 3 | 2 | \((3, 2)\) |
| 5 | \(5 \div 5 = 1\); 1 is not a multiple of 5 | 1 | \((5, 1)\) |
\[ \begin{aligned} \text{factorisation}(180) &= [(2, 2), (3, 2), (5, 1)] \\ 2^2 \times 3^2 \times 5^1 &= 4 \times 9 \times 5 \\ &= 180 \end{aligned} \] The remaining value after the last division is \(1\), so the factorisation is complete.
Store it as the list of pairs \[ \begin{aligned} 180 &= [(2, 2), (3, 2), (5, 1)] \end{aligned} \] where the first entry of each pair is the prime and the second is its exponent. Reading the list: \[ \begin{aligned} (2,2) &\Rightarrow 2^2 = 4 \\ (3,2) &\Rightarrow 3^2 = 9 \\ (5,1) &\Rightarrow 5^1 = 5 \\ 4 \times 9 \times 5 &= 180 \end{aligned} \] A prime that does not occur is simply left out of the list, or equivalently written with exponent 0. Using a fixed set of columns \(\{2, 3, 5, 7, 11, \dots\}\) and writing the exponents \((2, 2, 1, 0, 0, \dots)\) is the same information in a different layout.
A common divisor can use only primes that appear in both factorisations, and for each
such prime it can use at most the smaller of the two exponents. So the comparison runs prime
by prime:
1. Write both numbers as lists of pairs.
2. Mark the primes that occur in both lists.
3. For each common prime take the smaller exponent.
4. Multiply those prime powers; the product is the gcd.
5. If no prime is common, the gcd is 1.
Example with \(180\) and \(252\): \[ \begin{aligned} 180 &= 2^2 \times 3^2 \times 5^1 \\ 252 &= 2^2 \times 3^2 \times 7^1 \end{aligned} \]
| Prime | Exponent in 180 | Exponent in 252 | Smaller |
|---|---|---|---|
| 2 | 2 | 2 | 2 |
| 3 | 2 | 2 | 2 |
| 5 | 1 | 0 (absent) | 0 — not used |
| 7 | 0 (absent) | 1 | 0 — not used |
\[ \begin{aligned} \gcd(180, 252) &= 2^2 \times 3^2 \\ &= 4 \times 9 \\ &= \mathbf{36} \end{aligned} \] The same comparison with the larger exponents gives the lcm: \[ \begin{aligned} \mathrm{lcm}(180, 252) &= 2^2 \times 3^2 \times 5 \times 7 \\ &= 36 \times 35 \\ &= 1260 \end{aligned} \] and indeed \(36 \times 1260 = 45360 = 180 \times 252\), as the product rule predicts.
Take \(12 = 2^2 \times 3\) and \(18 = 2 \times 3^2\). A common divisor may use at most two 2’s from 12 but only one 2 from 18, so at most one 2 is available; similarly at most one 3. Hence \[ \begin{aligned} \gcd(12, 18) &= 2^1 \times 3^1 \\ &= 6 \end{aligned} \] Using the larger exponent would produce \(2^2 \times 3^2 = 36\), which does not divide 12 at all. So the minimum is forced.
Comparison with the Euclid method: factorisation needs the primes of both numbers (which itself needs a divisor scan), while the division algorithm of Question 1 needs only a handful of divisions. That is why the chapter presents factorisation as an alternative, not as the fastest method.
• Algorithm: for each prime \(p\) dividing \(n\), divide remaining by \(p\) repeatedly, count the divisions, and record the pair \((p, \text{count})\); report the list of pairs.
• (i) \(180 = 2^2 \times 3^2 \times 5^1\) is represented as \(\mathbf{[(2,2), (3,2), (5,1)]}\) — prime and exponent in each pair (equivalently the exponent vector \((2,2,1)\)).
• (ii) Compare prime by prime: keep the primes common to both lists and, for each, take the smaller exponent; multiply. Example: \(180 = 2^2 3^2 5\) and \(252 = 2^2 3^2 7\) give \(\gcd(180,252) = \mathbf{36}\) (and the larger exponents give \(\mathrm{lcm} = 1260\)).