The World of Algorithms: Class 9 Ganita Manjari · Chapter 11
Ganita Manjari Part 2 · Companion notes, simulations and practice · Official NCERT chapter PDF ↗
Published · Updated
What you will learn in The World of Algorithms
- Explain what an algorithm is, and recognise its basic steps, repeated steps and conditional steps.
- Write column addition as a precise algorithm with a carry, execute it, and justify it using place value.
- Compare the work of two methods by asking how it grows as the numbers get larger.
- Build gcd algorithms from the definition using lists of divisors, and improve them step by step.
- Describe a list as a data structure and use the fact that a list is sorted.
- Prove that gcd(m, n) = gcd(n, m − n) and gcd(m, n) = gcd(n, m mod n), and use them in Euclid’s algorithm.
- Find the gcd of large numbers by Āryabhaṭa’s division method and explain why it needs few steps.
Original companion text aligned to NCERT Mathematics Class 9, Chapter 11, pp. 37–50. Not affiliated with or endorsed by NCERT.
Ask a map app for the quickest way to the railway station and it suggests a route within a second. A music app picks the next song, and a shopping site guesses what you might buy. People shrug and say, “the algorithm decided”. So what is this thing called an algorithm, and where does mathematics come into it?
Think about how tea is made at home. Boil water, add tea leaves, then milk, then sugar “to taste”. That is a list of steps, but “to taste” leaves the cook to decide. A mathematical procedure leaves nothing to taste. Every step says exactly what to do, so anyone who follows it reaches the same answer. In this chapter we study two such procedures closely: adding numbers and finding a greatest common divisor. For each one we ask three questions. What are the exact steps? Why do they give the right answer? How much work do they take?
11.1 Adding Numbers
Suppose nobody had ever shown you column addition, and someone asked for 586 + 749. You could draw 586 dots, draw 749 more, and count the whole crowd: all 1335 of them. That is really how small children add 6 + 8, by counting everything. The method never fails, but for big numbers it would take ages.
You already know a far better way, and Fig. 11.1 puts it next to counting for a small sum. On the left, every dot has to be counted. On the right, the numbers are stacked by place value, and two single-digit sums finish the job. When a column reaches 10 or more, a carry is handed to the next column.
3/3The column method needs just two one-digit sums.
Left panel: 23 blue dots in rows of ten and 14 orange dots below them. Right panel: 23 written above 14 in columns. In the first step only the dots and the column sum are set out. In the second step every dot is counted, and a label says this takes 37 counts. In the third step the column method finds 3 + 4 = 7 in the units column and 2 + 1 = 3 in the tens column, giving 37 with only two small additions.
11.1.1 Adding Numbers Digit by Digit
Column addition deserves a name of its own: it is an algorithm (कलन-विधि). What makes it one is not cleverness but two other qualities. It is general: the same instructions handle 23 + 14 and 586 + 749 alike. And it is exact: no instruction needs judgement or taste.
A finite list of exact steps that, followed in order, solves every problem of a given kind.
ExampleColumn addition is an algorithm for adding any two whole numbers.
Let us write the instructions down so carefully that someone who has never met column addition, or a machine, could follow them. We call the result ADD. The word carry names a number that we keep track of while we work; you will soon see why it is always 0 or 1.
When we follow ADD's steps for two particular numbers, we are said to execute (or run) it. Fig. 11.2 executes ADD on 586 + 749. Step 2 handles one column at a time, and Step 3 keeps sending us one column to the left until none are left.
5/5No columns left and carry is 1, so write 1 at the far left: 1335.
The numbers 586 and 749 are written in columns with a shaded box marking the current column. Step 1: the numbers are lined up and carry is 0. Step 2: in the units column 6 + 9 + 0 = 15, so 5 is written and a carry of 1 appears above the tens column. Step 3: in the tens column 8 + 4 + 1 = 13, so 3 is written and a carry of 1 appears above the hundreds. Step 4: in the hundreds column 5 + 7 + 1 = 13, so 3 is written with carry 1. Step 5: no columns are left, so the carry 1 is written at the far left, giving 1335.
Executing ADD, column by column
Execute ADD on 7058 + 2967. For each column, show the total , the digit written and the new value of carry.
Try it yourself first. Then reveal the solution one step at a time.
ADD never says how to work out 8 + 7. It simply trusts you to know single-digit sums by heart, the way a recipe trusts you to know how to boil water. Actions that an algorithm uses without spelling them out are its basic steps. For ADD, one column is a basic step: add two digits and a carry, write a digit, update the carry. Any algorithm has to stop explaining somewhere, and its basic steps mark that point.
Practice 11.1: questions with answers
Act as a machine that knows nothing except Algorithm ADD. Add 6847 and 5976, writing down and carry for each column. Then check your answer by subtraction.
Show solution
Answer12823
- Units: , write 3, carry 1.
- Tens: , write 2, carry 1.
- Hundreds: , write 8, carry 1.
- Thousands: , write 2, carry 1.
- No columns left and carry is 1: write 1 at the far left.
The result 12823 is correct; you can check it as .
Now try ADD on 40382 + 976, where the numbers have different lengths.
In the thousands column there is a 0 on top and no digit at all below it. Taken word for word, can Step 2 be carried out there?
Show solution
AnswerNo: Step 2 needs two digits.
Step 2 says “add the two digits in the current column”. Here there is only one digit, so a reader who follows the words exactly gets stuck. The algorithm, as written, only handles numbers of equal length.
Change Step 1 so that ADD handles numbers of different lengths. Then find the sum.
Show solution
Answer41358
Add to Step 1: “If one number has fewer digits, write 0s in its empty places on the left.” So 976 becomes 00976, which has the same value.
- Units: , carry 0.
- Tens: , write 5, carry 1.
- Hundreds: , write 3, carry 1.
- Thousands: , carry 0.
- Ten-thousands: . The sum is 41358.
Riya writes 41 under 352 with the first digits lined up, 4 under 3 and 1 under 5, and then runs ADD. What answer does she get, and what went wrong?
Show solution
Answer762; the true sum is 393.
Each column must hold digits of the same place value, so that ones are added to ones and tens to tens. Lined up on the left, the 4 of 41 sits in the hundreds column and the 1 in the tens column. ADD then really adds 410, giving instead of .
Could ADD ever need a carry of 2? Find the largest total any column can have, and use it to decide.
Show solution
AnswerEach total is at most 19.
In the units column carry is 0, so and the new carry is at most 1.
If carry is at most 1 coming into a column, then . That is less than 20, so is a single digit and the new carry is again at most 1. Column after column, the carry stays 0 or 1.
A printing error deletes Step 4 from ADD.
Give a sum for which ADD still gives the right answer.
Show solution
AnswerFor example 314 + 452.
In 314 + 452 the hundreds column gives with carry 0, so nothing is lost: the answer 766 is right.
Give a sum for which it fails, and the wrong answer it gives.
Show solution
AnswerFor example 814 + 452 gives 266.
In 814 + 452 the hundreds column gives : write 2, carry 1. Without Step 4 that 1 is never written, so the result reads 266 instead of 1266.
Exactly when is Step 4 needed?
Show solution
AnswerWhen the leftmost column totals 10 or more.
Step 4 writes something only when carry is 1 after the leftmost column, that is, when that column's total is 10 or more.
In ADD, a column has total . What happens?
Show answer
Answer (A) write 0 and set carry to 1
10 is “10 or more”, so we write and carry 1. A rule that said “more than 10” would leave this case out, which is why exact wording matters.How many times is Step 2 of ADD carried out when it adds two 6-digit numbers?
Show answer
Answer 6
Step 2 runs once per column. Step 4 may add a seventh digit, but it does not run Step 2 again.
Why the column method is so much faster
Both methods give the same answer, so the real question is how much work each one costs. Add one digit to each number and the numbers become roughly ten times larger, and so does the pile of dots. ADD just gains a single extra column.
Let us put rough times on this. Suppose you can count 2 dots a second, and you spend a careful 10 seconds on each column of ADD. Table 11.1 compares the two methods as the numbers grow.
| Sum | Dots to count | Time by counting | Columns | Time by ADD |
|---|---|---|---|---|
| 48 + 36 | 84 | 42 s | 2 | 20 s |
| 486 + 365 | 851 | about 7 min | 3 | 30 s |
| 4862 + 3657 | 8519 | about 71 min | 4 | 40 s |
| 48620 + 36571 | 85191 | about 12 hours | 5 | 50 s |
Read the table down its columns. The counting times explode, from under a minute to half a day, while the times for ADD creep up by 10 seconds a row. This is the whole point of a good algorithm: its work stays modest even when the numbers get large. From now on we judge every algorithm the same way, by asking how its work grows as its input grows.
Why the column method gives the right answer
Speed is worthless if the answer is wrong, so we also owe a reason why ADD always agrees with counting. Picture 47 as 4 rods of ten cubes plus 7 loose cubes, and 38 as 3 rods plus 8 loose cubes, as in Fig. 11.3. Tipping everything into one heap and counting it gives the true total; that is what 47 + 38 means. ADD reaches the same total by counting loose cubes first and rods afterwards.
4/4 tens and 5 ones: the sum is 85.
Rods stand for tens and small cubes for ones. In the first step 47 is shown as 4 rods and 7 cubes, and 38 as 3 rods and 8 cubes. In the second step the ones are counted together: 7 + 8 = 15 cubes. In the third step ten of the cubes are bundled into one new rod, the carry, leaving 5 cubes. In the fourth step the rods are counted, 4 + 3 + 1 = 8 tens, so the total is 8 tens and 5 ones, which is 85.
Seven loose cubes and eight loose cubes make fifteen: enough to snap ten of them into a new rod, with five left loose. Snapping that rod together is exactly what the carry records. A hundred is a bundle of ten rods, so the same snapping-together can happen one column further left. Grouping cubes never adds or removes a cube, so the rods and loose cubes we end with count exactly the cubes we started with.
Show answer
Answer (D) exact steps that solve every problem of one kind
Show answer
Answer (1) 7, (2) 1
Show answer
- (A) Counting dots takes about 1000 times as long.
- (B) ADD needs 3 more columns.
Show answer
Answer True
11.2 Greatest Common Divisor
Now a second familiar problem. A tailor has two ribbons, 84 cm and 126 cm long. She wants to cut both into pieces of one equal length with nothing left over. What is the longest piece she can cut? The length must divide 84 and divide 126, and we want the largest such number. It is the greatest common divisor of 84 and 126, written . You may know it as the highest common factor, or HCF (महत्तम समापवर्तक).
The largest whole number that divides each of two given numbers exactly. It is also called the highest common factor (HCF).
Example: 6 divides both numbers, and no larger number does.
You could find this with prime factorisation, as in earlier classes. Instead, let us pretend we know nothing except what the words greatest common divisor say, and build an algorithm straight from them. Read the name from the back. Divisor: find the numbers that divide. Common: keep the ones shared by both numbers. Greatest: choose the largest. For two numbers and that gives a three-part plan.
- Find all the divisors of and write them down.
- Find all the divisors of and write them down.
- Compare the two collections and pick out the largest number that appears in both.
Each part is a smaller job than the whole problem, and smaller jobs are easier to turn into exact steps. We take them one at a time.
11.2.1 Computing the Divisors of a Number
A divisor (भाजक) of is a whole number that divides with remainder 0. Finding all of them is like taking a roll call. Call out 1, 2, 3, and so on up to , and every number that divides exactly answers “present”. Nobody beyond can answer, because a divisor of is never larger than .
The answers must be written down somewhere. We use a list: an ordered row of values inside square brackets, such as . Before the roll call starts, nobody has answered, so the list begins empty, written . Each new answer is appended, that is, written at the end.
Try reading Step 2 aloud without the letter : “for each number from 1 to , if that number divides , append that number…”. It soon gets muddled. Short labels such as and found keep the steps crisp. A label for a value that changes while an algorithm runs is called a variable. Step 2 also shows two new features: a step can be repeated (once for each ), and it can be conditional (append only if it divides ).
5/5Only 20 itself succeeds. The list is complete.
The numbers 1 to 20 sit in two rows of boxes, with a pointer marking the current value of j, and the list found is written below. Step 1: found is the empty list. Step 2: j = 1 and j = 2 both divide 20, so found becomes [1, 2]. Step 3: j = 3 fails while 4 and 5 divide 20, giving [1, 2, 4, 5]. Step 4: of 6 to 10 only 10 divides 20. Step 5: of 11 to 20 only 20 divides 20, so the final list is [1, 2, 4, 5, 10, 20].
The finished list comes out in increasing order. That is no accident: the roll call runs upwards, so each divisor joins the list after every smaller one. Keep this fact in mind, because we use it soon. Here the basic step is a divisibility test, which we treat as something we can always do, for instance by long division.
Divisors of 63
Execute DIVISORS(63). How many tests does it make, and how many divisors does it find?
Try it yourself first. Then reveal the solution one step at a time.
11.2.2 Finding the Greatest Common Divisor
Two lists are on the table. Which number is in both, and biggest? For 36 and 60 the answer, 12, almost jumps out, as Fig. 11.5 shows. For 1680 and 2520, which have 40 and 48 divisors, nothing jumps out, and a tired eye easily skips a match. (Their gcd is 840; a much faster method later in the chapter finds it in two divisions.)
So we hand the comparison to one more small algorithm. Picture a shopkeeper checking a delivery: she reads down her order list and ticks each item that also appears on the delivery note. In the same way, we read the first divisor list from left to right and copy every entry that also appears in the second list into a new list, common. Since 1 divides everything, common always gets at least one entry.
Now a pleasant surprise. Both divisor lists were sorted, and we read the first one from left to right, so common is sorted too. Its biggest entry sits at the right-hand end. The gcd is therefore simply the last entry of common, and no further search is needed.
4/4The last entry of common is the largest: .
Two rows of boxes show the divisors of 36 (nine boxes) and of 60 (twelve boxes). Step 1 shows only the two lists. Step 2 highlights the entries of the first list that also appear in the second and joins each to its match: 1, 2, 3, 4, 6 and 12. Step 3 copies them, in order, into a third row called common. Step 4 rings the last box of common, 12, and labels it as the gcd of 36 and 60.
11.2.3 First Algorithm for gcd
Snap the three parts together and we have a complete gcd algorithm. Notice that it does not repeat the instructions of DIVISORS. It simply calls on DIVISORS twice, the way a biryani recipe might say “cook the rice” without explaining how. Reusing a trusted algorithm inside a bigger one is one of the most useful habits in mathematics and computing.
The tailor’s ribbons
Use GCD-LIST to find the longest equal pieces that can be cut from ribbons of 84 cm and 126 cm with nothing left over.
Try it yourself first. Then reveal the solution one step at a time.
Show answer
Answer (B) it tests the candidates from smallest to largest
Show answer
Answer 250
Show answer
Answer (1) 4, (2) 15
Show answer
Answer False
11.3 Data Structures
Think of the ticket checker on a long-distance train. Her reservation chart is printed coach by coach and berth by berth, so she can find any passenger in seconds. If the names were printed in the order the tickets were booked, the same information would cost her far more effort. How information is arranged decides how quickly we can use it.
Algorithms face the same issue. GCD-LIST keeps working values that are not the answer itself: the lists A, B and common. Each is a whole collection of numbers, held in increasing order. Any such arrangement of working values is called a data structure, and the sorted list is one of the simplest. Its sortedness paid off at the end, when the gcd could be read straight from the right-hand end of common.
Giving each working value a short name, as we did with A, B and common, keeps the steps readable. Your phone's contact list is another data structure you use daily: sorted by name, it lets you find a friend without reading every entry. Higher classes study many more.
A way of organising the values an algorithm works with, such as a list kept in increasing order, so that its steps can use them easily.
ExampleBecause common is sorted, GCD-LIST reads the gcd off as its last entry.
Practice 11.2: questions with answers
A class teacher keeps two lists of roll numbers, each in increasing order: P for students who handed in homework on Monday, and Q for Tuesday.
Write an algorithm that produces the roll numbers that appear in P but are missing from Q.
Show solution
- Make an empty list onlyP.
- For each value in P, from left to right: if is not in Q, append to onlyP.
- Report onlyP.
Execute it for P = [2, 5, 8, 11, 14] and Q = [1, 5, 9, 14, 20].
Show solution
Answer[2, 8, 11]
2 is not in Q (append), 5 is (skip), 8 is not (append), 11 is not (append), 14 is (skip). Result: [2, 8, 11].
Who handed in on Tuesday only? Adapt your algorithm and run it on the same lists.
Show solution
AnswerSwap the roles of P and Q: [1, 9, 20].
Walk along Q instead and test membership in P. 1, 9 and 20 are not in P, while 5 and 14 are, so the result is [1, 9, 20].
Both lists are sorted. How could you avoid searching all of Q for every ?
Show solution
Keep a pointer in each list and compare the two values pointed at. If the P value is smaller, it cannot appear later in Q, so append it and move along P. If the two are equal, move both pointers. If the Q value is smaller, move along Q. When Q runs out, append the rest of P. Each list is read only once.
Two buses leave a depot together at 6 a.m., one every 12 minutes and one every 18 minutes. They next leave together after minutes, the smallest whole number that both 12 and 18 divide.
Write an algorithm that finds for any two whole numbers.
Show solution
- Let be the larger of and , and the smaller.
- For in turn: if divides , report and stop.
It must stop, at the latest when , because divides .
Execute it for the buses. When do they next leave together?
Show solution
AnswerAfter 36 minutes, at 6:36 a.m.
and . For : 12 does not divide 18. For : 12 divides 36. Report 36, so the buses next leave together after 36 minutes, at 6:36 a.m.
When 36 is written as a product of two whole numbers, its divisors turn up two at a time: , , , and .
Suppose DIVISORS appends both numbers of a pair whenever it finds the smaller one. Up to which must it test for ? For a general ?
Show solution
AnswerUp to 6; in general, while .
If both numbers of a pair were larger than 6, their product would be larger than 36. So the smaller number of each pair is at most 6, and testing to 6 finds every pair. In general we test only the with : about values instead of .
This gives the list [1, 36, 2, 18, 3, 12, 4, 9, 6] for 36. Would GCD-LIST still work with lists in this order? What would you change?
Show solution
AnswerNot always; report the largest entry.
The lists are no longer sorted, so the last entry of common need not be the largest. For and : A = [1, 36, 2, 18, 3, 12, 4, 9, 6] and B = [1, 18, 2, 9, 3, 6], so common = [1, 2, 18, 3, 9, 6]. Its last entry is 6, but .
Fix: change Step 3 to report the largest entry of common (or sort the lists first).
11.3.1 Improving the Algorithm
GCD-LIST is correct, but wasteful. A good habit is to look at an algorithm and ask: which work could never change the answer? Each time we spot such waste we can remove it. Table 11.2 records four rounds of trimming for and . It scores each version by the candidates it looks at and the values it keeps in memory.
| Version | What changes | Looked at | Kept |
|---|---|---|---|
| 1. GCD-LIST | Two separate scans, then the lists are compared | 96 | 27 |
| 2. One scan | Each up to the larger number is tested against both and | 60 | 27 |
| 3. Common list only | goes straight into common; A and B are never built | 60 | 6 |
| 4. Stop at min | The scan stops at the smaller number, since nothing larger can divide it | 36 | 6 |
| 5. GCD-SCAN | Only the latest common divisor, best, is kept | 36 | 1 |
The last change deserves a closer look. The scan climbs upwards, so every common divisor it meets is bigger than all the ones before, and the older ones are dead weight. One variable, best, is enough. It begins at 1, which divides every number, and is overwritten whenever a bigger common divisor turns up. Fig. 11.6 shows the shrinking work as bars.
5/5Still 36 candidates, and only one number, best, is stored.
Five labelled bars, one per version of the algorithm for m = 36 and n = 60, appear one at a time. Step 1: GCD-LIST with two separate scans looks at 96 candidates. Step 2: one scan up to the larger number looks at 60. Step 3: building the common list directly still looks at 60 but stores fewer numbers. Step 4: stopping at the smaller number cuts this to 36. Step 5: keeping only the best divisor so far still looks at 36 candidates but stores just one number.
Practice 11.3: questions with answers
A friend rewrites DIVISORS so that counts down: , , …, 1.
In what order would it list the divisors of 20?
Show solution
Answer[20, 10, 5, 4, 2, 1]
The largest divisor is found first, so the list is in decreasing order: [20, 10, 5, 4, 2, 1].
Which step of GCD-LIST must change, and how?
Show solution
AnswerStep 3: report the first entry.
Now common is in decreasing order, so its largest entry is the first one. Step 3 must report the leftmost entry of common instead of the rightmost.
Run the scan of GCD-SCAN backwards: let go from down to 1.
Explain why you can stop at the first that divides both numbers.
Show solution
Every above it has already been tested and failed. So the first common divisor met on the way down is the largest one, which is the gcd. Nothing below it can beat it.
How many values of does the backward scan look at for , ? How many does the forward scan look at?
Show solution
AnswerBackward 33, forward 47.
. Backwards, runs 48, 47, …, 16 and stops: values. Forwards, runs from 2 to 48: 47 values.
For which pairs does the backward scan save nothing?
Show solution
AnswerPairs with gcd 1.
If , no above 1 works, so the backward scan goes all the way down to 1, looking at every value from down.
11.3.2 Analysing these Algorithms
We now have five versions of one algorithm. Are they all right, and are any of them fast? These are the two questions to ask about every algorithm.
Correctness. Think of the versions as links in a chain. The first link, GCD-LIST, can be trusted because it is the definition of the gcd turned into steps. Each later link differs from the one before only by throwing away work that could not affect the result: candidates too large to divide both numbers, or lists that were never read again. A chain of such links does not break, so GCD-SCAN at the end is as correct as GCD-LIST at the start.
Executing GCD-SCAN
Execute GCD-SCAN for and . Show the value of best after each .
Try it yourself first. Then reveal the solution one step at a time.
Efficiency. Speed is another matter. GCD-SCAN still visits every candidate up to the smaller number. Hand it a pair of 9-digit numbers and it may look at hundreds of millions of candidates; a computer copes, but a person with pencil and paper never would. In the language of Table 11.1, GCD-SCAN behaves like counting dots, not like ADD. That raises a challenge: is there a gcd algorithm whose work follows the length of the numbers rather than their size?
Show answer
Answer 83
Show answer
Answer (C) a common divisor cannot exceed either number
Show answer
Answer False
Show answer
Answer (D) it grows about 100 times
11.3.3 Euclid’s Subtraction Algorithm
The answer is more than 2300 years old. Around 300 BCE, Euclid of Alexandria gathered the mathematics of his time into the Elements, a work studied for over two thousand years. This method appears near the start of its Book VII. It was probably not his own invention, since earlier Greek mathematicians seem to have used it, but his account is the one that has come down to us.
Making the problem smaller
Try a small experiment first. The common divisors of 30 and 18 are 1, 2, 3 and 6. Now replace 30 by : the common divisors of 18 and 12 are again 1, 2, 3 and 6. Nothing was lost and nothing was gained, yet the numbers got smaller. Euclid's idea is that this always happens.
If , then .
Fig. 11.7 shows the idea on a number line. A hop length that lands exactly on and on also covers the stretch between them, of length , in whole hops. Euclid himself reasoned with lengths in much this way.
3/3The stretch is exactly two hops: 6 divides 12.
A number line runs from 0 to 30 with marks every 6 units. Step 1: three hops of length d = 6 from 0 land exactly on n = 18. Step 2: two more hops land exactly on m = 30. Step 3: the stretch from 18 to 30, of length m − n = 12, is highlighted and is covered by exactly two of the hops, so 6 divides m − n as well.
Each subtraction makes the total smaller, so the process cannot go on for ever: sooner or later the smaller number hits 0. What is ? Every whole number divides 0, because . So the common divisors of and 0 are just the divisors of , and the greatest of these is itself. Hence : when a 0 appears, the other number is the answer. For example, becomes , and the answer is 9.
Euclid by subtraction
Use EUCLID-SUB to find . How many subtractions does it make?
Try it yourself first. Then reveal the solution one step at a time.
There is a neat picture of EUCLID-SUB. Draw an by rectangle. Each subtraction cuts an by square off the end, leaving a smaller rectangle whose sides are the new pair. When a cut leaves nothing over, the side of the last square is the gcd. Fig. 11.8 does this for a 42 by 24 rectangle.
4/4Three subtractions use up the rest exactly. 5 cuts in all: the gcd is 6.
A rectangle 42 units wide and 24 units tall. Step 1 shows the whole rectangle and the pair (42, 24). Step 2 cuts off a 24 by 24 square on the left, leaving an 18 by 24 strip: the pair (24, 18). Step 3 cuts an 18 by 18 square off the strip, leaving an 18 by 6 piece: the pair (18, 6). Step 4 cuts the piece into three 6 by 6 squares with nothing left over, so the gcd is 6.
EUCLID-SUB is a big step forward, but give it one large number and one small one and it crawls. For it chips away 3 at a time: , , , … all the way down to , and then needs three more steps. That is 336 subtractions. A 7-digit number paired with 3 would need over a million. The work still follows the size of the numbers, not their length.
Show answer
Show answer
Answer (A) the two pairs have exactly the same common divisors
Show answer
Answer 14
Show answer
Answer 12
11.3.4 Āryabhaṭa’s Division Algorithm
Look again at . Taking 3 away from 1000 again and again, 333 times, leaves 1. One division does exactly that in a single stroke: . The leftover 1 is the remainder.
Indian mathematicians worked in exactly this way. In the Āryabhaṭīya, completed in 499 CE, Āryabhaṭa describes a method, now known as kuṭṭaka (“the pulveriser”), for solving certain equations in whole numbers. Its first stage divides two numbers into each other again and again, each time keeping only the remainder. That repeated division is precisely what finds a gcd, and with Indian place-value numerals it became a compact long-division method.
is the remainder left over after dividing by . It is always less than .
Example, since ; and .
That suggests a one-line upgrade to EUCLID-SUB. Wherever it would subtract from over and over, let it divide once and keep the remainder, so that the pair jumps straight to . The theorem below shows that the jump keeps the gcd the same.
If , then .
Step 1 now matters only at the very start, because a remainder is always smaller than the divisor. Run the slow case again: becomes , then , and the answer 1 arrives after 2 divisions instead of 336 subtractions. In fact never needs more than 3 divisions, however large is.
The long-division layout
By hand, EUCLID-DIV is a chain of divisions. Each divisor becomes the next number to be divided, and each remainder becomes the next divisor. The chain stops at remainder 0, and the last divisor, which is the last non-zero remainder, is the gcd. Indian texts set this out compactly, writing each new division just below and to the right of the one before. Fig. 11.9 follows the chain for .
5/5The last non-zero remainder is the gcd: .
A table with columns divide, by, quotient and remainder. Step 1: 1155 divided by 546 gives quotient 2 and remainder 63. Step 2: arrows carry 546 to the divide column and 63 to the by column of the next row: 546 divided by 63 gives 8, remainder 42. Step 3: 63 divided by 42 gives 1, remainder 21. Step 4: 42 divided by 21 gives 2, remainder 0. Step 5: the last non-zero remainder, 21, is circled as the gcd.
The same pair takes 13 subtractions with EUCLID-SUB but only 4 divisions here.
Euclid with division
Find with EUCLID-DIV, and compare the work with EUCLID-SUB.
Try it yourself first. Then reveal the solution one step at a time.
Why is EUCLID-DIV so quick in general? A remainder is always smaller than its divisor. With a little more care one can show something stronger: after any two divisions, the larger number of the pair has dropped below half its old value. Each digit of a number can survive only about halvings, so the count of divisions grows with the number of digits. A complete proof waits for a later class; for now, test the claim in the stepper.
Show answer
Answer 5
Show answer
Show answer
Answer 3
Show answer
Answer (C) is replaced by
Algorithms through time
- c. 300 BCE
Euclid’s Elements
The gcd found by repeated subtraction.
- 499 CE
Āryabhaṭa’s Āryabhaṭīya
Repeated division with remainders, in the kuṭṭaka method.
- c. 820 CE
al-Khwārizmī, Baghdad
A book on Indian numerals and how to compute with them.
- 12th century
Latin translation
Indian arithmetic, called algorismus, spreads through Europe.
- 1844
Gabriel Lamé
Proves that Euclid’s division algorithm needs few steps.
The World of Algorithms: key points and summary
- An algorithm is a finite list of exact steps that solves every problem of a given kind. It is built from basic steps that we can already do (Section 11.1.1).
- ADD adds two numbers column by column from the right, with a carry that is always 0 or 1 (Algorithm ADD).
- ADD is correct because it adds ones, tens and hundreds separately; a carry is a bundle of ten (why ADD works).
- Counting dots takes about ten times longer for each extra digit; ADD needs just one more column (why ADD is fast).
- Steps may repeat (ADD runs Step 2 once per column) and may be conditional (GCD-SCAN changes best only when divides both numbers) (Section 11.2.1).
- DIVISORS() tests every candidate from 1 to and returns the divisors in increasing order (Section 11.2.1).
- GCD-LIST keeps the divisors common to both lists; the last entry of that sorted list is the gcd (Section 11.2.3).
- A data structure, such as a sorted list, organises the values an algorithm works with (Section 11.3).
- Scanning once, stopping at and keeping only best make the work smaller, but it still grows with the size of the numbers (Section 11.3.1, Section 11.3.2).
- Euclid: for , and (Section 11.3.3).
- Āryabhaṭa’s division method uses ; its number of steps grows only with the number of digits (Section 11.3.4).
- For every algorithm we ask two questions: is it correct, and how does its work grow with the size of its input (Section 11.3.2)?
- The word algorithm comes from al-Khwārizmī, whose book carried Indian arithmetic to the wider world (history).
Practice questions with answers (NCERT “End-of-chapter exercises”)
Use EUCLID-DIV to find each gcd. Show every division.
Show solution
Answer91
Each row divides by ; the next row divides by the remainder.
remainder quotient 1365 1001 364 1 1001 364 273 2 364 273 91 1 273 91 0 3 The last non-zero remainder is 91, so the gcd is 91.
Show solution
Answer10800
Step 1 swaps the numbers, since .
Each row divides by ; the next row divides by the remainder.
remainder quotient 75600 43200 32400 1 43200 32400 10800 1 32400 10800 0 3 The last non-zero remainder is 10800, so the gcd is 10800.
Show solution
Answer1287
Each row divides by ; the next row divides by the remainder.
remainder quotient 5162157 956241 380952 5 956241 380952 194337 2 380952 194337 186615 1 194337 186615 7722 1 186615 7722 1287 24 7722 1287 0 6 The last non-zero remainder is 1287, so the gcd is 1287. Six divisions were enough for 7-digit numbers.
Show solution
Answer3003
Each row divides by ; the next row divides by the remainder.
remainder quotient 2114112 945945 222222 2 945945 222222 57057 4 222222 57057 51051 3 57057 51051 6006 1 51051 6006 3003 8 6006 3003 0 2 The last non-zero remainder is 3003, so the gcd is 3003.
Let , and write , where . Prove that the common divisors of and are exactly the same as the common divisors of and .
Show solution
Every common divisor of and is a common divisor of and . If divides and , write and . Then , so divides (and it divides already).
Every common divisor of and is a common divisor of and . If divides and , write and . Then , so divides (and ).
Both directions hold, so the two pairs have the same common divisors. This is why EUCLID-DIV may replace by .
Is 221 prime? Is 223? Checking by hand is tedious, so let an algorithm do it.
Write an algorithm PRIME() that reports “yes” for a prime and “no” otherwise. (A prime has no divisors except 1 and itself.)
Show solution
- Let D be the list DIVISORS().
- If D has exactly 2 entries, report “yes”; otherwise report “no”.
This handles correctly: DIVISORS(1) = [1] has one entry, and 1 is not prime. For 221, D = [1, 13, 17, 221], so the answer is “no”; for 223, D = [1, 223], so it is “yes”.
How many tests does your algorithm make for ? Suggest a faster version.
Show solution
Answer223 tests; stop once .
DIVISORS(223) tests every from 1 to 223: 223 tests.
Faster: test while , and report “no” at the first that divides . If a number has a divisor other than 1 and itself, it has one with (Practice 11.2, Q3). For 223 that means to 14, only 13 tests, and none succeeds, so 223 is prime.
The primes that divide 84 are 2, 3 and 7. Write an algorithm PRIME-DIVISORS() that produces such a list for any , using DIVISORS and PRIME as building blocks. Then execute it for .
Show solution
Answer[2, 3, 5]
- Let D be the list DIVISORS(), and make an empty list P.
- For each in D: if PRIME() reports “yes”, append to P.
- Report P.
For 90, D = [1, 2, 3, 5, 6, 9, 10, 15, 18, 30, 45, 90]. Only 2, 3 and 5 are prime, so the result is [2, 3, 5].
Prime factors offer yet another road to the gcd.
. Suggest a way to store a prime factorisation as a list.
Show solution
Answer[(2, 3), (3, 2), (5, 1)]
Use a list of pairs (prime, power), with the primes in increasing order: [(2, 3), (3, 2), (5, 1)].
Describe an algorithm FACTORISE() that builds this list.
Show solution
- Make an empty list F, and set to 2.
- While : count how many times divides , dividing by each time. If the count is more than 0, append to F. Then increase by 1.
- Report F.
A non-prime never divides what is left, because its prime factors have already been divided out.
Use the lists for 360 and to find their gcd. State your rule.
Show solution
Answer36
Rule: for each prime in both lists, take the smaller power; leave out primes in only one list. From [(2, 3), (3, 2), (5, 1)] and [(2, 2), (3, 3), (7, 1)] we get . Check with EUCLID-DIV: and .
A shop has 1428 red beads and 1092 blue beads. It wants to make identical gift packets, each with the same numbers of red and blue beads, using every bead. What is the largest number of packets it can make, and what is in each one?
Show solution
Answer84 packets of 17 red and 13 blue.
remainder quotient 1428 1092 336 1 1092 336 84 3 336 84 0 4 So packets. Each packet holds red and blue beads.
Which pair makes EUCLID-SUB do the most subtractions?
Show answer
Answer (D)
The counts are 2, 4, 6 and 36. From 100, subtracting 3 alone takes 33 steps just to get down to 1.Which statement about an algorithm is false?
Show answer
Answer (B) it may give different answers to the same input
Exact steps followed in order always lead to the same result for the same input.For every whole number , .
Show answer
Answer m
Every number divides 0, so the common divisors are the divisors of , and the largest is .If , the backward scan of Practice 11.3, Q2 looks at every value from down to 1.
Show answer
Answer True
No value above 1 divides both numbers, so the scan only stops when it reaches 1.
Find your NCERT exercise question
Our practice questions are written for ncert.ai and follow the NCERT exercises. Find the one that matches the question in your textbook; its answer is with it.
| In your NCERT textbook | Practice question here |
|---|---|
| NCERT Exercise Set 11.1, Q1 | Practice 11.1 · Q1 |
| NCERT Exercise Set 11.1, Q2 | Practice 11.1 · Q2 |
| NCERT Exercise Set 11.1, Q3 | Practice 11.1 · Q3 |
| NCERT Exercise Set 11.1, Q4 | Practice 11.1 · Q4 |
| NCERT Exercise Set 11.1, Q5 | Practice 11.1 · Q5 |
| NCERT Exercise Set 11.2, Q1 | Practice 11.2 · Q1 |
| NCERT Exercise Set 11.2, Q2 | Practice 11.2 · Q2 |
| NCERT Exercise Set 11.2, Q3 | Practice 11.2 · Q3 |
| NCERT Exercise Set 11.3, Q1 | Practice 11.3 · Q1 |
| NCERT Exercise Set 11.3, Q2 | Practice 11.3 · Q2 |
| NCERT End-of-chapter Exercises Q1 | End-of-chapter exercises · Q1 |
| NCERT End-of-chapter Exercises Q2 | End-of-chapter exercises · Q2 |
| NCERT End-of-chapter Exercises Q3 | End-of-chapter exercises · Q3 |
| NCERT End-of-chapter Exercises Q4 | End-of-chapter exercises · Q4 |
| NCERT End-of-chapter Exercises Q5 | End-of-chapter exercises · Q5 |
Quick check: MCQs on The World of Algorithms
- Why the column method gives the right answer4 questions
- 11.2.3 First Algorithm for gcd4 questions
- 11.3.2 Analysing these Algorithms4 questions
- Making the problem smaller4 questions
- The long-division layout4 questions
Key terms (English–Hindi)
- algorithmकलन-विधिkalan-vidhi
- A finite list of exact steps that, followed in order, solves every problem of a given kind.First seen in the chapter →
- basic stepमूल चरणmool charan
- An action an algorithm uses without explaining it, because we can already do it, such as adding two digits.First seen in the chapter →
- data structureडेटा संरचनाdata sanrachna
- A way of organising the values an algorithm works with, such as a sorted list.First seen in the chapter →
- divisorभाजकbhajak
- A whole number that divides a given number with remainder 0.First seen in the chapter →
- efficiencyदक्षताdakshata
- How much work an algorithm needs, judged by how its number of basic steps grows with the size of the input.First seen in the chapter →
- executeनिष्पादित करनाnishpadit karna
- To carry out the steps of an algorithm one by one for a particular input.First seen in the chapter →
- greatest common divisorमहत्तम समापवर्तकmahattam samapavartak
- The largest whole number that divides each of two given numbers exactly.First seen in the chapter →
- listसूचीsoochi
- An ordered row of values in square brackets, such as ; new values are appended at the end.First seen in the chapter →
- modशेषफलsheshphal
- is what is left over after taking as many whole lots of out of as possible.
- variableचरchar
- A name for a value that an algorithm keeps track of and may change, such as carry or best.First seen in the chapter →