11.1 Adding Numbers
Column addition shows how a clearly described set of steps can solve a problem much faster than counting objects.
Add any two 4-digit numbers by following the textbook algorithm exactly. Does it always produce the correct answer?
Worked example: Add 3487 and 2756.
For this pair, the algorithm works. But it does not always work exactly as written. The textbook gives branches for a sum less than 10 and a sum more than 10; it leaves out a sum equal to 10. For instance, in 1234 + 5676, the units total is 4 + 6 = 10, and neither instruction applies.
Fix: Replace “more than 10” with “10 or more” (or “≥ 10”) wherever a carry is determined. Then 1234 + 5676 = 6910.
What happens when a 5-digit number is added to a 3-digit number? Do the original steps handle this case correctly?
Not fully. The original instructions say to align the rightmost digits but do not explicitly explain what to do when one number has fewer digits.
For example, write 12,345 + 678 as:
12345 +00678 ------ 13023
We treat the absent left-hand digits of 678 as zeros. We must also correct the rule for column sums equal to 10. After both clarifications, 12,345 + 678 = 13,023.
Why should the columns be aligned from right to left?
Because digits have different place values. The rightmost positions are units, then tens, hundreds, and so on. Aligning from the right means we add quantities of the same place value: units with units, tens with tens, etc.
If we line up the leftmost digits of numbers with different lengths, a hundreds digit could be incorrectly added to a tens digit.
Why can the carry in Step 3 never be greater than 1?
The largest single digit is 9, and the incoming carry is at most 1. Therefore the greatest possible total for a column is:
Since 19 contains only one ten, at most 1 needs to be carried to the next column. No column can generate a carry of 2 when adding only two numbers.
What happens if the fifth step (writing the final carry at the left) is omitted? Show a case where it works and one where it fails.
Case where it works: 1234 + 4321 = 5555. There is no final carry after the thousands column, so skipping Step 5 causes no problem.
Case where it fails: 8888 + 8888 = 17,776. The column operations leave a final carry of 1. Without Step 5, we would write just 7776, which is wrong.
Conclusion: The final step is necessary whenever the leftmost column produces a carry.
Explain why grouping numbers into units, tens, hundreds, etc., proves the usual addition algorithm is correct.
Every number can be broken into place-value groups. For example, 248 = 200 + 40 + 8 and 176 = 100 + 70 + 6.
At each place, we preserve the total value by exchanging 10 units for 1 ten, or 10 tens for 1 hundred. This is why carrying does not change the sum, and the column method is correct.
How should the algorithm be modified to add two decimal fractions?
- Align the decimal points, so equal place values line up.
- Add trailing zeros where helpful (for example, 2.5 = 2.50).
- Add each column from the right, carrying whenever a column total is 10 or more.
- Put the decimal point in the answer directly below the aligned decimal points.
11.2 Greatest Common Divisor (GCD / HCF)
Understand the original divisor-list algorithm before looking at the faster methods.
Execute the divisor-finding algorithm for 15, 135, and 775. How does the amount of work grow as the numbers increase?
| Number | Complete list of divisors | Count |
|---|---|---|
| 15 | 1, 3, 5, 15 | 4 |
| 135 | 1, 3, 5, 9, 15, 27, 45, 135 | 8 |
| 775 | 1, 5, 25, 31, 155, 775 | 6 |
The textbook's basic method tests every integer from 1 through n. Therefore it makes 15, 135, and 775 divisibility checks, respectively. Although 775 has fewer actual divisors than 135, the basic algorithm still makes many more checks because it examines all candidate numbers.
How the first GCD algorithm works
- Find all divisors of m in increasing order.
- Find all divisors of n in increasing order.
- Keep only the divisors present in both lists.
- Because the common-divisor list is increasing, its last element is the GCD.
For 54,000 and 81,000, the chapter finds GCD = 27,000. Later sections improve this method so that we do not have to test thousands of candidates.
11.3 Data Structures and Exercise Set 11.2
Learn how to compare ordered lists, compute LCM, and use factor pairs to speed up divisor searches.
Given two lists of numbers in increasing order, write an algorithm to find values that occur in List 1 but do not occur in List 2.
Start with an empty result list. For every number x in List 1, check if x occurs in List 2. If it does not, append x to the result.
only_in_1 = []
for each x in List1:
if x is not in List2:
append x to only_in_1
report only_in_1
Example: List 1 = [1, 3, 5, 7] and List 2 = [3, 4, 7]. Answer = [1, 5].
Write an algorithm to find values that occur in List 2 but do not occur in List 1.
This is the same procedure with the roles reversed:
only_in_2 = []
for each y in List2:
if y is not in List1:
append y to only_in_2
report only_in_2
Example: For the same two lists, the answer is [4].
Efficiency tip: Because both lists are sorted, a two-pointer scan can compare the smallest remaining elements and move forward without repeatedly searching the entire other list. This is an optional improvement, not required to understand the original algorithm.
Describe an algorithm to calculate the least common multiple (LCM) of two positive integers.
The LCM is the smallest positive number divisible by both inputs. One direct algorithm checks multiples in ascending order:
step = max(m, n)
candidate = step
while candidate is not divisible by min(m, n):
candidate = candidate + step
report candidate
Example: For 12 and 18, test multiples of 18: 18 is not divisible by 12, but 36 is. The first positive integer divisible by both is 36, so LCM(12, 18) = 36.
Faster method: Once we know the GCD, use LCM(m, n) = (m ÷ GCD(m, n)) × n. Here, GCD(12, 18) = 6 and (12 ÷ 6) × 18 = 36.
Divisors come in pairs, such as (1,18), (2,9), and (3,6) for 18. How many candidate values do we need to examine to find every divisor of n?
We only need to check 1, 2, …, ⌊√n⌋ (all positive integers up to the square root of n).
If j divides n, then n ÷ j is also a divisor. At least one number in each factor pair is ≤ √n.
Example: For 36, check j = 1, 2, 3, 4, 5, 6 (just 6 checks instead of 36). The pairs found are (1,36), (2,18), (3,12), (4,9), and (6,6). When both elements of a pair are equal, include the divisor only once.
If divisors are generated in pairs, will the original GCD algorithm still work exactly as described?
Not necessarily. The original algorithm assumes the divisor lists are in increasing order, then takes the rightmost common divisor as the largest. Pair-generation does not automatically preserve that order.
Counterexample: Pair-order divisors of 36 can be [1, 36, 2, 18, 3, 12, 4, 9, 6]. Comparing with divisors of 24 gives common values in this order: [1, 2, 3, 12, 4, 6]. The final value is 6, but the true GCD(36, 24) is 12.
Fix: Sort the divisors into ascending order before using the original algorithm, or track the maximum common divisor rather than taking the last one.
11.3.1 Improving the Algorithm and Exercise Set 11.3
Changes in ordering affect whether we must use the first, last, or greatest element.
Improving the GCD search
- The original algorithm tests m + n candidate divisors to build two lists.
- Combining the scans reduces the range to max(m, n).
- Checking only possible common divisors reduces it to min(m, n).
- Storing only the latest (and therefore largest so far) common divisor removes the need for a list.
latest = 1
for k from 2 to min(m, n):
if k divides m and k divides n:
latest = k
report latestIf the divisor-finding algorithm checks n, n−1, …, 1 instead of 1, 2, …, n, how must the original GCD algorithm be changed?
Each divisor list will now be in decreasing order, not increasing order. Therefore the common-divisor list will also be decreasing.
We must report its first (leftmost) element, rather than its last element.
Example: For 12 and 18, the common-divisor list in descending order is [6, 3, 2, 1]. The first element gives the correct GCD, 6; the last would incorrectly give 1.
What changes if the improved algorithm that keeps no list scans possible common divisors backward, from min(m, n) down to 1?
We can stop at the first number that divides both inputs. Since we are searching from largest to smallest, the first common divisor encountered must be the GCD.
for k from min(m, n) down to 1:
if k divides m and k divides n:
report k
stop
Example: For 12 and 18, test 12, 11, 10, 9, 8, 7, then 6. The first common divisor is 6, so the algorithm stops.
11.3.2–11.3.4 Euclid and Āryabhaṭa: The Fast GCD Methods
The faster method keeps the same mathematical answer while replacing many subtractions with a single division.
Euclid’s subtraction algorithm
For m ≥ n, replace (m, n) with (n, m−n), reordering if necessary. If one entry reaches zero, the other is the GCD.
gcd_subtract(m, n):
while m != 0 and n != 0:
if m > n: m = m - n
else: n = n - m
return m if n == 0 else n
Āryabhaṭa’s division algorithm
Replace repeated subtraction with the remainder operation. This is the much faster algorithm used in the chapter's final exercise.
gcd_divide(m, n):
while n != 0:
remainder = m mod n
m = n
n = remainder
return mWhy does GCD(m, n) = GCD(n, m−n) when m ≥ n?
If d divides both m and n, then d also divides their difference m−n.
Conversely, if d divides both n and m−n, it must divide their sum n + (m−n) = m.
Thus the two number-pairs have exactly the same common divisors and consequently the same GCD.
Show why every odd number 2k+1 has GCD 1 with 2 using the division algorithm.
Every odd number can be written as 2k + 1. Dividing it by 2 leaves remainder 1.
two remainder reductions suffice when the odd number is at least 3. For the odd number 1, starting with the larger input gives 2 mod 1 = 0 in one step.
End-of-Chapter Exercises
Complete step-by-step solutions to the textbook’s closing questions.
These are the chapter's final textbook questions, including all four large GCD calculations.
Compute GCD(375, 825) using the improved (division/remainder) algorithm.
Use larger number = smaller number × quotient + remainder, then repeat with the smaller number and the remainder:
The last nonzero remainder is 75, so GCD(375, 825) = 75.
Compute GCD(51,000, 81,000) using the improved (division/remainder) algorithm.
Use larger number = smaller number × quotient + remainder, then repeat with the smaller number and the remainder:
The last nonzero remainder is 3,000, so GCD(51,000, 81,000) = 3,000.
Compute GCD(1,789,287, 237,656) using the improved (division/remainder) algorithm.
Use larger number = smaller number × quotient + remainder, then repeat with the smaller number and the remainder:
The last nonzero remainder is 1, so GCD(1,789,287, 237,656) = 1.
Compute GCD(2,587,392, 157,656) using the improved (division/remainder) algorithm.
Use larger number = smaller number × quotient + remainder, then repeat with the smaller number and the remainder:
The last nonzero remainder is 24, so GCD(2,587,392, 157,656) = 24.
Assuming m ≥ n > 0 and d is a positive integer, prove that d divides both m and n if and only if d divides both n and m mod n.
By the division algorithm, we can write m = qn + r, where r = m mod n and 0 ≤ r < n.
- Forward direction: If d divides m and n, then it divides m − qn = r. So d divides n and r.
- Reverse direction: If d divides n and r, then it divides qn + r = m. So d divides m and n.
Therefore the common divisors of (m, n) and (n, m mod n) are identical. GCD(m, n) = GCD(n, m mod n).
Write an algorithm prime(n) to check whether n is prime. Use the earlier divisors(n) algorithm.
A prime number has exactly two distinct positive divisors, 1 and itself. The number 1 is not prime.
prime(n):
if n <= 1:
return False
D = divisors(n)
if length(D) == 2:
return True
else:
return False
Example: divisors(13) = [1, 13], so prime(13) is True. But divisors(12) = [1, 2, 3, 4, 6, 12], so prime(12) is False.
Optional speed improvement: Check possible divisors only from 2 up to ⌊√n⌋. If any divides n, the number is not prime; otherwise a number greater than 1 is prime.
Write an algorithm primedivisors(n) that returns the divisors of n that are prime.
First find all divisors, then keep only those for which prime(d) returns True.
primedivisors(n):
result = []
D = divisors(n)
for each d in D:
if prime(d):
append d to result
return result
Example: Divisors of 180 are [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]. The prime divisors are [2, 3, 5].
Note that 1 is excluded, because it is not prime.
Write an algorithm to find the prime factorisation of a natural number n.
Start with the smallest possible prime factor, 2. Divide by that factor repeatedly and record the number of times it divides. Then move to the next possible factor.
prime_factorisation(n):
factors = []
p = 2
while p * p <= n:
exponent = 0
while n mod p == 0:
n = n / p
exponent = exponent + 1
if exponent > 0:
append (p, exponent) to factors
p = p + 1
if n > 1:
append (n, 1) to factors
return factors
Why this works: We repeatedly divide out the smallest remaining factor. If a number p greater than 2 is composite, its smaller factors would already have been removed; therefore a successful new factor is prime. If the leftover n > 1 after testing up to its square root, it is itself prime.
How can the prime factorisation 180 = 2² × 3² × 5¹ be represented?
Store a list of (prime, exponent) pairs:
[(2, 2), (3, 2), (5, 1)]
This means that 2 is multiplied twice, 3 is multiplied twice, and 5 is multiplied once: 4 × 9 × 5 = 180.
How should we compare the prime factorisations of two numbers?
Match the prime bases. For the GCD, choose the smaller exponent of each prime shared by both numbers. For the LCM, choose the larger exponent of each prime appearing in either factorisation (using exponent 0 if a prime is absent).
With the pair-list representation, compare the matching prime entries and select the exponent according to the operation.
Chapter Summary
Six short definitions that help connect the main ideas in the PDF.
- Algorithm: A definite sequence of steps for solving a problem. Its correctness should be justified.
- Condition: Run an action only if a test holds, such as “if k divides both numbers”.
- Loop: Repeat an action over a specified range or until a stopping condition holds.
- Data structure: An organised way to store information, such as an ordered list.
- Efficiency: Compare how many steps different correct algorithms need.
- Remainder / mod: The number left after division, such as 17 mod 5 = 2.
- In the addition steps on pages 37–38, “more than 10” should include exactly 10.
- The correct remainder identity on page 48 is GCD(m, n) = GCD(n, m mod n); retain n alongside the remainder.