KnowledgeBoat Logo
|
OPEN IN APP

Chapter 11

The World of Algorithms

Class - 9 Ganita Manjari Part 2 Mathematics Solutions



Exercise Set 11.1

Question 1

Add two 4-digit numbers using the steps we have written down. Make sure you follow the steps precisely; do not perform any action that is not explicitly mentioned. Are you able to obtain the correct result?

Answer

In Steps 2 and 3, the algorithm tells us what to do only when the sum of a column is less than 10 or more than 10. Let us follow the steps exactly for two pairs of 4-digit numbers.

Example 1: 4738 + 2156

Step 1: Write 2156 below 4738 with the digits aligned from right to left.

Step 2: Rightmost digits : 8 + 6 = 14.

14 is more than 10, so write 4 below the two digits and set carry = 1.

Step 3: Next column : 3 + 5 + 1 = 9.

9 is less than 10, so write 9 below the two digits and set carry = 0.

Step 4: Repeat Step 3 for the next column : 7 + 1 + 0 = 8.

8 is less than 10, so write 8 below the two digits and set carry = 0.

Repeat Step 3 for the last column : 4 + 2 + 0 = 6.

6 is less than 10, so write 6 below the two digits and set carry = 0.

There are no more digits on the left.

Step 5: The value of carry is 0, so nothing more is written.

4738+ 21566894\begin{array}{r} 4738 \\ +\ 2156 \\ \hline 6894 \end{array}

Here the steps give 6894, which is the correct sum.

Example 2: 2345 + 1265

Step 1: Write 1265 below 2345 with the digits aligned from right to left.

Step 2: Rightmost digits : 5 + 5 = 10.

The sum 10 is neither less than 10 nor more than 10. Step 2 does not say what to write below the digits or what value to give the carry in this case.

The same thing happens in Step 3 whenever the two digits and the carry add up to exactly 10.

The steps become correct if the second case in Steps 2 and 3 is changed to "If the sum is equal to or more than 10".

Hence, we obtain the correct result only when no column adds up to exactly 10 (for example, 4738 + 2156 = 6894); for numbers such as 2345 + 1265, the steps fail because they do not say what to do when a column sum is exactly 10.

Question 2

What happens if you add a 5-digit number to a 3-digit number. Do our steps handle this situation correctly?

Answer

Let us add 45678 and 789 by following the steps.

Step 1: Write 789 below 45678 with the digits aligned from right to left.

Step 2: Rightmost digits : 8 + 9 = 17.

17 is more than 10, so write 7 and set carry = 1.

Step 3: Next column : 7 + 8 + 1 = 16.

16 is more than 10, so write 6 and set carry = 1.

Step 4: Repeat Step 3 : 6 + 7 + 1 = 14.

14 is more than 10, so write 4 and set carry = 1.

The next column on the left has only one digit, 5, because 789 has no thousands digit. Step 3 asks us to "add the two digits and the current value of carry", but this column does not have two digits. So, followed precisely, the steps do not tell us what to do here.

If we write zeros in the empty places, that is, write 789 as 00789, every column has two digits and the steps can go on :

Repeat Step 3 : 5 + 0 + 1 = 6.

6 is less than 10, so write 6 and set carry = 0.

Repeat Step 3 : 4 + 0 + 0 = 4.

4 is less than 10, so write 4 and set carry = 0.

Step 5: The value of carry is 0, so nothing more is written.

45678+ 0078946467\begin{array}{r} 45678 \\ +\ 00789 \\ \hline 46467 \end{array}

Hence, our steps do not handle this situation as written, because some columns have only one digit; they work correctly if we first fill the empty places with zeros, giving 45678 + 789 = 46467.

Question 3

Why is it important to align the columns from right to left?

Answer

In the Indian base-ten place-value system, the rightmost digit of every number is its units digit, the next digit to the left is its tens digit, then hundreds, and so on.

When the numbers are aligned from right to left, units come below units, tens below tens and hundreds below hundreds. So each column adds digits of the same place value, and a carry of 1 correctly moves one group of ten to the next place.

Numbers with different numbers of digits start at different positions on the left. If we aligned them from the left, digits of different place values would be added together.

Example: Consider 473 + 95.

Aligned from right to left:

473+ 95568\begin{array}{r} 473 \\ +\ 95 \\ \hline 568 \end{array}

Aligned from left to right, the 9 tens of 95 would come below the 4 hundreds of 473, and the 5 units below the 7 tens :

473+ 9501423\begin{array}{r} 473 \\ +\ 95\phantom{0} \\ \hline 1423 \end{array}

The first answer 568 is correct, while 1423 is wrong.

Hence, the columns must be aligned from right to left so that digits of the same place value (units with units, tens with tens, and so on) are added together.

Question 4

In Step 3, why cannot the value of carry be more than 1?

Answer

Every digit is at most 9.

In Step 2, the largest possible sum of the two rightmost digits is:

⇒ 9 + 9 = 18

Since 18 is less than 20, the sum has at most one ten. So the carry after Step 2 is 0 or 1.

In Step 3, we add two digits and the carry, which is at most 1. The largest possible sum is:

⇒ 9 + 9 + 1 = 19

Since 19 is less than 20, the sum again has at most one ten, so the new carry is again 0 or 1.

Each time Step 3 is repeated, the carry coming in is at most 1, so the same argument holds for every column.

Example: In 999 + 999, every column has the largest digits:

Units: 9 + 9 = 18, write 8, carry = 1.

Tens: 9 + 9 + 1 = 19, write 9, carry = 1.

Hundreds: 9 + 9 + 1 = 19, write 9, carry = 1.

Step 5 writes 1 on the left, giving 1998. Even here, the carry never goes above 1.

Hence, the value of carry cannot be more than 1, because the sum in any column is at most 9 + 9 + 1 = 19, which is less than 20.

Question 5

What happens if we do not include the fifth step in the algorithm? Give examples where the algorithm will work correctly and where it will fail to work.

Answer

The fifth step writes the final carry of 1 to the left of the bottom row. Without it, the carry produced by the leftmost column is lost.

Example where the algorithm works : 473 + 215

Units: 3 + 5 = 8, write 8, carry = 0.

Tens: 7 + 1 + 0 = 8, write 8, carry = 0.

Hundreds: 4 + 2 + 0 = 6, write 6, carry = 0.

473+ 215688\begin{array}{r} 473 \\ +\ 215 \\ \hline 688 \end{array}

The final carry is 0, so the fifth step is not needed and the answer 688 is correct.

Example where the algorithm fails : 473 + 695

Units: 3 + 5 = 8, write 8, carry = 0.

Tens: 7 + 9 + 0 = 16, write 6, carry = 1.

Hundreds: 4 + 6 + 1 = 11, write 1, carry = 1.

Without the fifth step, the final carry 1 is never written, and the bottom row reads 168.

But 473 + 695 = 1168. So the answer is wrong.

Hence, without the fifth step, the algorithm works when the leftmost column gives no carry (for example, 473 + 215 = 688), but fails when the leftmost column gives a carry of 1 (for example, 473 + 695 gives 168 instead of 1168).

Think and Reflect 1

Question 1

Why does adding individual digits from right to left result in the sum of the two numbers? The key idea is that we are grouping each number into units, tens, hundreds, … and adding each group separately. Whenever a smaller group, say units, generates a large group of ten, we carry over the newly formed ten to the tens group.

See if you can complete the argument about grouping by units, tens, hundreds, … to justify why the addition algorithm works.

Answer

Adding two numbers means putting together their dots and counting all of them. Grouping the dots into units, tens, hundreds, … does not change how many dots there are; it only makes them easier to count.

Consider 473 + 695.

Grouping each number :

⇒ 473 = 4 hundreds + 7 tens + 3 units

⇒ 695 = 6 hundreds + 9 tens + 5 units

Adding each group separately, starting from the units (the rightmost column):

Units: 3 + 5 = 8 units.

Tens: 7 + 9 = 16 tens.

16 tens = 10 tens + 6 tens = 1 hundred + 6 tens.

So we keep 6 tens and carry the newly formed 1 hundred to the hundreds group.

Hundreds: 4 + 6 + 1 = 11 hundreds.

11 hundreds = 10 hundreds + 1 hundred = 1 thousand + 1 hundred.

So we keep 1 hundred and carry the newly formed 1 thousand to the thousands group.

Total = 1 thousand + 1 hundred + 6 tens + 8 units = 1168.

This is exactly what the column algorithm does :

  • Each column adds the groups of one place value.

  • When a column has 10 or more, ten of its groups form one group of the next place value. This new group is the carry of 1 added to the column on the left, and the units digit of the column sum is what remains.

  • A column sum is at most 9 + 9 + 1 = 19, so at most one new group is formed each time.

  • When the last column gives a new group, Step 5 writes it as the new leftmost digit.

Since only the grouping changes and no dot is added or lost, the digits written in the bottom row give the total number of dots.

Hence, adding the digits from right to left and carrying each newly formed group of ten to the next place gives the sum of the two numbers.

Question 2

How would you modify the algorithm to add two decimal fractions?

Answer

Step 1 needs to change and one step is added at the end. The carrying rule in Steps 2 and 3 must also be corrected, because the original steps do not say what to do when a column sum is exactly 10 (for example, 5 + 5 in the tenths column of 0.5 + 0.5).

Modified algorithm to add two decimal fractions:

Step 1: Write the numbers one below the other so that their decimal points are in one line. If one number has fewer digits after the decimal point, write zeros at its right end; if it has fewer digits before the decimal point, write zeros at its left end. Now both numbers have the same number of digits in every column.

Step 2: Add the rightmost digits, ignoring the decimal point.

– If the sum is less than 10, write the sum directly below the two digits. Set the value of carry to 0.

– If the sum is equal to or more than 10, write the units digit of the sum directly below the two digits. Set the value of carry to 1.

Step 3: Move to the next column of digits on the left, ignoring the decimal point. Add the two digits and the current value of carry.

– If the sum is less than 10, write the sum directly below the two digits. Set the value of carry to 0.

– If the sum is equal to or more than 10, write the units digit of the sum directly below the two digits. Set the value of carry to 1.

Step 4: Repeat Step 3 until there are no more digits on the left.

Step 5: If the value of carry is 1, write 1 to the left of the bottom row.

Step 6: Place the decimal point in the answer directly below the decimal points of the two numbers.

Aligning the decimal points puts tenths below tenths and hundredths below hundredths. Ten hundredths make one tenth, and ten tenths make one unit, so a carry works in exactly the same way as before.

Example: Add 3.75 and 12.6.

Step 1: Write 3.75 as 03.75 and 12.6 as 12.60.

Step 2: Hundredths : 5 + 0 = 5, write 5, carry = 0.

Step 3: Tenths : 7 + 6 + 0 = 13, write 3, carry = 1.

Units: 3 + 2 + 1 = 6, write 6, carry = 0.

Tens: 0 + 1 + 0 = 1, write 1, carry = 0.

Step 5: The value of carry is 0, so nothing more is written.

Step 6: Place the decimal point below the decimal points.

03.75+ 12.6016.35\begin{array}{r} 03.75 \\ +\ 12.60 \\ \hline 16.35 \end{array}

Hence, to add two decimal fractions, we align the decimal points (filling empty places with zeros), add as before with a carry of 1 whenever a column sum is 10 or more, and put the decimal point of the answer directly below them; for example, 3.75 + 12.6 = 16.35.

Think and Reflect 2

Question 1

Try to execute the algorithm to compute the divisors of 15, 135, and 775. How does the amount of work increase as the numbers grow?

Answer

Algorithm to find the divisors of n

1. Start with an empty list-of-divisors.

2. For each number j in the sequence 1,2,3,...,n

– if j divides n, add j to the list-of-divisors.

Divisors of 15

Initially list-of-divisors is empty [ ].

1 divides 15, so we update list-of-divisors to [1].

2 does not divide 15, so list-of-divisors stays [1].

3 divides 15, so we update list-of-divisors to [1, 3].

4 does not divide 15, so list-of-divisors stays [1, 3].

5 divides 15, so we update list-of-divisors to [1, 3, 5].

6, 7, 8, … , 14 do not divide 15, so list-of-divisors stays [1, 3, 5].

15 divides 15, so we update list-of-divisors to [1, 3, 5, 15].

So, divisors(15) = [1, 3, 5, 15].

Divisors of 135

Initially list-of-divisors is empty [ ].

1 divides 135, so we update list-of-divisors to [1].

2 does not divide 135.

3 divides 135, so we update list-of-divisors to [1, 3].

4 does not divide 135.

5 divides 135, so we update list-of-divisors to [1, 3, 5].

6, 7 and 8 do not divide 135.

9 divides 135, so we update list-of-divisors to [1, 3, 5, 9].

10, 11, … , 14 do not divide 135.

15 divides 135, so we update list-of-divisors to [1, 3, 5, 9, 15].

16, 17, … , 26 do not divide 135.

27 divides 135, so we update list-of-divisors to [1, 3, 5, 9, 15, 27].

28, 29, … , 44 do not divide 135.

45 divides 135, so we update list-of-divisors to [1, 3, 5, 9, 15, 27, 45].

46, 47, … , 134 do not divide 135.

135 divides 135, so we update list-of-divisors to [1, 3, 5, 9, 15, 27, 45, 135].

So, divisors(135) = [1, 3, 5, 9, 15, 27, 45, 135].

Divisors of 775

Initially list-of-divisors is empty [ ].

1 divides 775, so we update list-of-divisors to [1].

2, 3 and 4 do not divide 775.

5 divides 775, so we update list-of-divisors to [1, 5].

6, 7, … , 24 do not divide 775.

25 divides 775, so we update list-of-divisors to [1, 5, 25].

26, 27, … , 30 do not divide 775.

31 divides 775, so we update list-of-divisors to [1, 5, 25, 31].

32, 33, … , 154 do not divide 775.

155 divides 775, so we update list-of-divisors to [1, 5, 25, 31, 155].

156, 157, … , 774 do not divide 775.

775 divides 775, so we update list-of-divisors to [1, 5, 25, 31, 155, 775].

So, divisors(775) = [1, 5, 25, 31, 155, 775].

Amount of work

The algorithm checks every number j from 1 to n, so it makes n checks.

Checks for 15 = 15

Checks for 135 = 135

Checks for 775 = 775

Comparing with 15:

⇒ 13515\dfrac{135}{15} = 9, so 135 needs 9 times as many checks.

⇒ 77515\dfrac{775}{15} ≈ 51.67, so 775 needs about 52 times as many checks.

Hence, divisors(15) = [1, 3, 5, 15], divisors(135) = [1, 3, 5, 9, 15, 27, 45, 135] and divisors(775) = [1, 5, 25, 31, 155, 775]; the amount of work grows in proportion to the number itself, since a number n needs n checks.

Exercise Set 11.2

Question 1(i)

Suppose List 1 and List 2 are two lists of numbers in increasing order.

Write an algorithm to find elements in List 1 that are not present in List 2.

Answer

We use the name not-in-list-2 for the list of answers.

Algorithm to find elements of List 1 not present in List 2

Step 1: Start with an empty list not-in-list-2.

Step 2: For each number x in List 1

– Go through List 2 from left to right. If we find x, do nothing. If we reach a number larger than x, or the end of List 2, without finding x, add x to not-in-list-2.

Step 3: Report not-in-list-2.

Since List 2 is in increasing order, once we reach a number larger than x, every number after it is also larger than x. So x cannot appear later in List 2, and we can stop looking.

Let us execute this algorithm with :

List 1 = [1, 3, 5, 15, 25, 75, 125, 375] (the divisors of 375)

List 2 = [1, 3, 5, 11, 15, 25, 33, 55, 75, 165, 275, 825] (the divisors of 825)

Initially not-in-list-2 is empty [ ].

1, 3, 5, 15, 25 and 75 are found in List 2, so not-in-list-2 stays [ ].

For 125, we reach 165, which is larger than 125, without finding 125. So we update not-in-list-2 to [125].

For 375, we reach 825, which is larger than 375, without finding 375. So we update not-in-list-2 to [125, 375].

Hence, the above algorithm gives the elements of List 1 that are not in List 2; for the lists above, these are [125, 375].

Question 1(ii)

Suppose List 1 and List 2 are two lists of numbers in increasing order.

Write an algorithm to find elements in List 2 that are not present in List 1.

Answer

We use the name not-in-list-1 for the list of answers.

Algorithm to find elements of List 2 not present in List 1

Step 1: Start with an empty list not-in-list-1.

Step 2: For each number x in List 2

– Go through List 1 from left to right. If we find x, do nothing. If we reach a number larger than x, or the end of List 1, without finding x, add x to not-in-list-1.

Step 3: Report not-in-list-1.

As List 1 is in increasing order, once we pass a number larger than x, x cannot appear later in List 1.

Let us execute this algorithm with:

List 1 = [1, 3, 5, 15, 25, 75, 125, 375] (the divisors of 375)

List 2 = [1, 3, 5, 11, 15, 25, 33, 55, 75, 165, 275, 825] (the divisors of 825)

Initially not-in-list-1 is empty [ ].

1, 3 and 5 are found in List 1, so not-in-list-1 stays [ ].

For 11, we reach 15 without finding 11. So we update not-in-list-1 to [11].

15 and 25 are found in List 1.

For 33, we reach 75 without finding 33. So we update not-in-list-1 to [11, 33].

For 55, we reach 75 without finding 55. So we update not-in-list-1 to [11, 33, 55].

75 is found in List 1.

For 165, we reach 375 without finding 165. So we update not-in-list-1 to [11, 33, 55, 165].

For 275, we reach 375 without finding 275. So we update not-in-list-1 to [11, 33, 55, 165, 275].

For 825, we reach the end of List 1 without finding 825. So we update not-in-list-1 to [11, 33, 55, 165, 275, 825].

Hence, the above algorithm gives the elements of List 2 that are not in List 1; for the lists above, these are [11, 33, 55, 165, 275, 825].

Question 2

Describe an algorithm to compute the least common multiple (lcm) of two numbers.

Answer

The lcm of two numbers m and n is the smallest number that is a multiple of both m and n.

Algorithm to find lcm(m, n)

Step 1: If m < n, reverse the numbers and compute lcm(n, m). (So m is now the larger number.)

Step 2: For each number k in the sequence 1, 2, 3, … , n

– compute k × m. If n divides k × m, report k × m as lcm(m, n) and stop.

Why this works:

  • The numbers 1 × m, 2 × m, 3 × m, … are the multiples of m in increasing order.

  • We check each of them, in order, to see whether it is also a multiple of n. The first one that is a multiple of n is the smallest common multiple.

  • The scan always stops by k = n at the latest, because n × m is a multiple of both m and n.

Let us execute this algorithm for lcm(12, 18).

Since 12 < 18, Step 1 asks us to reverse the numbers and compute lcm(18, 12).

k = 1 : 1 × 18 = 18. 12 does not divide 18.

k = 2 : 2 × 18 = 36. 12 divides 36, since 36 = 12 × 3.

So, the algorithm reports 36 and stops.

(Note: Many other algorithms are also possible, for example, listing the multiples of both numbers and finding the smallest number common to both lists.)

Hence, lcm(m, n) is the first multiple of the larger number that is divisible by the smaller number; for example, lcm(12, 18) = 36.

Question 3(i)

Divisors occur in pairs. For instance, the divisors of 18 are (1, 18), (2, 9) and (3, 6).

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?

Answer

If j divides n, then n ÷ j also divides n, and j × (n ÷ j) = n. So the divisors come in pairs (j, n ÷ j).

In each pair, the smaller number j satisfies j × j ≤ n. (If both numbers of a pair were bigger than such a j, their product would be bigger than n.)

So it is enough to examine j = 1, 2, 3, … only as long as j × j ≤ n. Each time j divides n, we write the pair (j, n ÷ j).

For n = 18:

4 × 4 = 16, which is not more than 18, and 5 × 5 = 25, which is more than 18. So we examine only 1, 2, 3 and 4.

1 divides 18 and 18 ÷ 1 = 18, giving the pair (1, 18).

2 divides 18 and 18 ÷ 2 = 9, giving the pair (2, 9).

3 divides 18 and 18 ÷ 3 = 6, giving the pair (3, 6).

4 does not divide 18.

So we examine only 4 numbers instead of 18.

When n is a perfect square, one pair has the same number twice, for example (6, 6) for n = 36; this divisor is written only once.

Hence, we have to examine only the numbers j from 1 up to the largest j with j × j ≤ n, that is, the numbers from 1 to n\mathbf{\sqrt{n}}.

Question 3(ii)

Divisors occur in pairs. For instance, the divisors of 18 are (1, 18), (2, 9) and (3, 6).

If we list out the divisors in pairs, will our gcd algorithm still work in the manner we have described?

Answer

Our gcd algorithm reports the rightmost element of the list common-divisors as gcd(m, n). This works only because the lists of divisors are in increasing order, so the rightmost common divisor is the largest.

If the divisors are listed in pairs, the lists are not in increasing order:

divisors(12) = [1, 12, 2, 6, 3, 4]

divisors(18) = [1, 18, 2, 9, 3, 6]

Executing the gcd algorithm for gcd(12, 18) :

Initially common-divisors is empty [ ].

1 appears in divisors(18), so we update common-divisors to [1].

12 does not appear in divisors(18).

2 appears in divisors(18), so we update common-divisors to [1, 2].

6 appears in divisors(18), so we update common-divisors to [1, 2, 6].

3 appears in divisors(18), so we update common-divisors to [1, 2, 6, 3].

4 does not appear in divisors(18).

The rightmost element is 3, but gcd(12, 18) = 6. So the algorithm gives a wrong answer.

The algorithm works again if we first arrange each list of divisors in increasing order, or if Step 5 reports the largest element of common-divisors instead of the rightmost one.

Hence, no, the gcd algorithm does not work in the manner described if the divisors are listed in pairs, because the lists are no longer in increasing order.

Exercise Set 11.3

Question 1

How would our original algorithm change if we computed the divisors of n by examining the numbers from 1 to n in reverse order, from n down to 1?

Answer

In the original algorithm, divisors(m) and divisors(n) are found by checking j = 1, 2, 3, …, so both lists are in increasing order. The gcd is then the rightmost element of common-divisors.

If divisors are found by checking from n down to 1, each list of divisors is in decreasing order.

Step 1: Let divisors-of-m be the list obtained by computing divisors(m)

divisors(375) = [375, 125, 75, 25, 15, 5, 3, 1]

Step 2: Let divisors-of-n be the list obtained by computing divisors(n)

divisors(825) = [825, 275, 165, 75, 55, 33, 25, 15, 11, 5, 3, 1]

Step 3: Start with an empty list common-divisors

common-divisors = [ ]

Step 4: Executing Step 4 for gcd(375, 825), we check each element of divisors(375) in divisors(825) :

375 and 125 are not in divisors(825).

75, 25, 15, 5, 3 and 1 are in divisors(825).

So, common-divisors = [75, 25, 15, 5, 3, 1].

This list is in decreasing order, so the largest common divisor is now the leftmost element. Step 5 must therefore be changed to:

Step 5: Report the leftmost element of the list common-divisors as gcd(m,n)

Also, the first common divisor found is the gcd, so we could stop as soon as we find it.

The order of common-divisors follows the order of divisors-of-m. So if only the divisors of the second number n were taken in reverse order, common-divisors would still be in increasing order and the algorithm would not change.

Hence, the lists of divisors become decreasing, and the gcd must be reported as the leftmost (first) element of common-divisors instead of the rightmost; for example, gcd(375, 825) = 75.

Question 2

Algorithm:
1. Starts with most-recent-common-divisor set to 1.
2. Scans every k from 2 to min(m, n), and whenever k divides both m and n, updates most-recent-common-divisor to k.
3. Reports most-recent-common-divisor at the end of the scan as the gcd.

What about the last algorithm described above? What happens when we look at common divisors starting from min(m, n) and work backwards to 1?

Answer

In the last algorithm, we scan k from 2 to min(m, n) and keep updating most-recent-common-divisor, so the last common divisor found is the gcd.

If we scan from min(m, n) down to 1, the first number found that divides both m and n is the greatest common divisor, because all larger numbers have already been checked. So we can stop as soon as we find it, and we no longer need most-recent-common-divisor.

Modified algorithm :

Step 1: For each number k in the sequence min(m, n), min(m, n) - 1, … , 2, 1

– if k divides both m and n, report k as gcd(m, n) and stop.

The algorithm always stops, since k = 1 divides every number.

We must stop at the first common divisor. If we went on updating most-recent-common-divisor down to 1, the last update would be at k = 1, and the answer would wrongly be 1.

Example 1: gcd(6, 12)

min(6, 12) = 6

k = 6 divides both 6 and 12, so gcd(6, 12) = 6.

We needed only 1 check, while scanning from 2 to 6 needs 5 checks.

Example 2: gcd(375, 825)

min(375, 825) = 375

k = 375, 374, … , 76 do not divide both 375 and 825.

k = 75 divides both 375 and 825, so gcd(375, 825) = 75.

Checks made = 375 - 75 + 1 = 301

Checks made by the forward scan from 2 to 375 = 375 - 2 + 1 = 374

When the gcd is 1, as in gcd(99, 2), the backward scan still has to check every number down to 1, so in the worst case the work is the same.

Hence, when we scan backwards from min(m, n), the first common divisor found is the gcd, so we can stop there; this saves work when the gcd is large, but in the worst case (gcd = 1) we still check all the numbers.

End-of-Chapter Exercises

Question 1(i)

Compute the following using the improved version of Euclid’s algorithm.

gcd(375, 825)

Answer

We use the improved version of Euclid’s algorithm :

1. If m < n, reverse the numbers and compute gcd(n,m)

2. If n = 0, report the answer as m

3. Otherwise, reduce the problem to computing gcd(n, m mod n)

Given: gcd(375, 825)

Since 375 < 825, interchange the numbers.

gcd(375, 825) = gcd(825, 375)

825 = 375 × 2 + 75

375 = 75 × 5 + 0

The last non-zero remainder is 75.

Hence, gcd(375, 825) = 75.

Question 1(ii)

Compute the following using the improved version of Euclid’s algorithm.

gcd(51000, 81000)

Answer

We use the improved version of Euclid’s algorithm :

1. If m < n, reverse the numbers and compute gcd(n,m)

2. If n = 0, report the answer as m

3. Otherwise, reduce the problem to computing gcd(n, m mod n)

Given: gcd(51000, 81000)

Since 51000 < 81000, interchange the numbers.

gcd(51000, 81000) = gcd(81000, 51000)

81000 = 51000 × 1 + 30000

51000 = 30000 × 1 + 21000

30000 = 21000 × 1 + 9000

21000 = 9000 × 2 + 3000

9000 = 3000 × 3 + 0

The last non-zero remainder is 3000.

Hence, gcd(51000, 81000) = 3000.

Question 1(iii)

Compute the following using the improved version of Euclid’s algorithm.

gcd(1789287, 237656)

Answer

We use the improved version of Euclid’s algorithm :

1. If m < n, reverse the numbers and compute gcd(n,m)

2. If n = 0, report the answer as m

3. Otherwise, reduce the problem to computing gcd(n, m mod n)

Given: gcd(1789287, 237656)

Since 1789287 > 237656, no interchange is needed.

1789287 = 237656 × 7 + 125695

237656 = 125695 × 1 + 111961

125695 = 111961 × 1 + 13734

111961 = 13734 × 8 + 2089

13734 = 2089 × 6 + 1200

2089 = 1200 × 1 + 889

1200 = 889 × 1 + 311

889 = 311 × 2 + 267

311 = 267 × 1 + 44

267 = 44 × 6 + 3

44 = 3 × 14 + 2

3 = 2 × 1 + 1

2 = 1 × 2 + 0

The last non-zero remainder is 1.

Hence, gcd(1789287, 237656) = 1.

Question 1(iv)

Compute the following using the improved version of Euclid’s algorithm.

gcd(2587392, 157656)

Answer

We use the improved version of Euclid’s algorithm :

1. If m < n, reverse the numbers and compute gcd(n,m)

2. If n = 0, report the answer as m

3. Otherwise, reduce the problem to computing gcd(n, m mod n)

Given: gcd(2587392, 157656)

Since 2587392 > 157656, no interchange is needed.

2587392 = 157656 × 16 + 64896

157656 = 64896 × 2 + 27864

64896 = 27864 × 2 + 9168

27864 = 9168 × 3 + 360

9168 = 360 × 25 + 168

360 = 168 × 2 + 24

168 = 24 × 7 + 0

The last non-zero remainder is 24.

Hence, gcd(2587392, 157656) = 24.

Question 2

Assume that m ≥ n. Verify that d divides m and n if and only if d divides both n and m mod n.

Answer

Given: m ≥ n, and r = m mod n.

When m is divided by n, let the quotient be q and the remainder be r. Then :

⇒ m = q × n + r, where q and r are whole numbers and r is less than n \quad...(1)

To prove: d divides m and n if and only if d divides n and r.

Proof:

Part 1: If d divides m and n, then d divides n and r.

Since d divides m and n, we can write m = ad and n = bd, where a and b are natural numbers.

From (1),

⇒ r = m - q × n

⇒ r = ad - q × bd

⇒ r = (a - qb)d

Since r is a whole number, a - qb is also a whole number. So d divides r. (When r = 0, this is still true, because d divides 0.)

So, d divides both n and m mod n.

Part 2 : If d divides n and r, then d divides m and n.

Since d divides n and r, we can write n = xd and r = yd, where x is a natural number and y is a whole number.

From (1),

⇒ m = q × n + r

⇒ m = q × xd + yd

⇒ m = (qx + y)d

So, d divides m. Hence d divides both m and n.

Example: Take m = 825, n = 375 and d = 25.

375 × 2 = 750

825 mod 375 = 825 - 750 = 75

25 divides 825 (825 = 25 × 33), 375 (375 = 25 × 15) and 75 (75 = 25 × 3).

Thus, the common divisors of m and n are exactly the common divisors of n and m mod n, which is why gcd(m, n) = gcd(n, m mod n).

Hence proved.

Note: Here n must be positive, since m mod n is undefined for n = 0. The gcd algorithm treats n = 0 separately.

Question 3

Write an algorithm prime(n) to check if n is prime.
(Hint: A prime number p has exactly two distinct factors, 1 and p. Can you make use of divisors(n) to write out prime(n)?)

Answer

Algorithm prime(n)

Step 1: Let divisors-of-n be the list obtained by computing divisors(n).

Step 2: If divisors-of-n has exactly two elements, report "n is prime".

Step 3: Otherwise, report "n is not prime".

Why this works:

Every number n greater than 1 has at least the two divisors 1 and n. It is prime exactly when it has no other divisor, that is, when divisors(n) has exactly two elements.

For n = 1, divisors(1) = [1] has only one element, so 1 is correctly reported as not prime.

Let us execute this algorithm.

prime(13): divisors(13) = [1, 13]. It has exactly two elements, so 13 is prime.

prime(15): divisors(15) = [1, 3, 5, 15]. It has four elements, so 15 is not prime.

Hence, prime(n) computes divisors(n) and reports n as prime exactly when this list has two elements; for example, 13 is prime and 15 is not.

Question 4

Write an algorithm primedivisors(n) to compute the list of divisors of n that are prime numbers.
(Hint: Compute divisors(n) and then filter out the primes in this list.)

Answer

Algorithm primedivisors(n)

Step 1: Let divisors-of-n be the list obtained by computing divisors(n).

Step 2: Start with an empty list prime-divisors.

Step 3: For each number x in divisors-of-n

– if prime(x) reports that x is prime, add x to the list prime-divisors.

Step 4: Report prime-divisors as primedivisors(n).

Here prime(x) is the algorithm of Question 3.

Let us execute this algorithm for n = 18.

divisors(18) = [1, 2, 3, 6, 9, 18]

Initially prime-divisors is empty [ ].

divisors(1) = [1] has one element, so 1 is not prime; prime-divisors stays [ ].

divisors(2) = [1, 2] has two elements, so 2 is prime; we update prime-divisors to [2].

divisors(3) = [1, 3] has two elements, so 3 is prime; we update prime-divisors to [2, 3].

divisors(6) = [1, 2, 3, 6] has four elements, so 6 is not prime.

divisors(9) = [1, 3, 9] has three elements, so 9 is not prime.

divisors(18) = [1, 2, 3, 6, 9, 18] has six elements, so 18 is not prime.

So, primedivisors(18) = [2, 3].

Hence, primedivisors(n) keeps only those elements of divisors(n) for which prime reports "prime"; for example, primedivisors(18) = [2, 3].

Question 5

We can also find the gcd of two numbers by computing prime factorisation of both the numbers. Try to write an algorithm to compute the prime factorisation of a number.

(i) The prime factorisation of 180 is 22 × 32 × 51. How would you represent this?

(ii) How would you compare the prime factorisations of two numbers?

Answer

We record the prime factorisation as a list of pairs (p, power), where p is a prime divisor of the number and power is the number of times p divides it.

Algorithm factorisation(n)

Step 1: Let primes-of-n be the list obtained by computing primedivisors(n).

Step 2: Start with an empty list factorisation.

Step 3: For each prime p in primes-of-n

– Set number to n and count to 0.

– If p divides number, replace number by number ÷ p and increase count by 1. Repeat this until p does not divide number.

– Add the pair (p, count) to factorisation.

Step 4: Report factorisation.

Let us execute this algorithm for n = 180.

primedivisors(180) = [2, 3, 5]

For p = 2:

180 ÷ 2 = 90, count = 1

90 ÷ 2 = 45, count = 2

2 does not divide 45, so we add (2, 2).

For p = 3:

180 ÷ 3 = 60, count = 1

60 ÷ 3 = 20, count = 2

3 does not divide 20, so we add (3, 2).

For p = 5:

180 ÷ 5 = 36, count = 1

5 does not divide 36, so we add (5, 1).

So, factorisation(180) = [(2, 2), (3, 2), (5, 1)].

(i) The prime factorisation 22 × 32 × 51 can be represented as a list of pairs, written in increasing order of the primes. In each pair, the first number is the prime and the second number is its power.

Hence, 180 = 22 × 32 × 51 is represented as the list [(2, 2), (3, 2), (5, 1)].

(ii) To compare the prime factorisations of two numbers, we go through the two lists of pairs together, prime by prime :

  • Two numbers are equal exactly when their lists are the same.

  • To find their gcd, for each prime that appears in both lists, take the smaller of its two powers. Primes that appear in only one list are left out. Multiply these primes raised to the chosen powers.

Example: Compare 180 and 84.

For n = 84, primedivisors(84) = [2, 3, 7].

84 ÷ 2 = 42, 42 ÷ 2 = 21, and 2 does not divide 21, giving (2, 2).

84 ÷ 3 = 28, and 3 does not divide 28, giving (3, 1).

84 ÷ 7 = 12, and 7 does not divide 12, giving (7, 1).

So, factorisation(84) = [(2, 2), (3, 1), (7, 1)].

Comparing with factorisation(180) = [(2, 2), (3, 2), (5, 1)] :

Prime 2 : powers 2 and 2, smaller power = 2.

Prime 3 : powers 2 and 1, smaller power = 1.

Primes 5 and 7 appear in only one list, so they are left out.

gcd(180, 84) = 22 × 31

= 4 × 3

= 12

Check by Euclid’s algorithm : 180 = 84 × 2 + 12 and 84 = 12 × 7 + 0, so gcd(180, 84) = 12.

Hence, we compare prime factorisations prime by prime; the gcd is the product of the common primes, each raised to the smaller of its two powers, for example gcd(180, 84) = 22 × 31 = 12.

PrevNext