Overview
An algorithm is a precise list of steps that solves every problem of a given kind. You have used one since primary school: the column method of addition. This chapter starts there, writes the method out step by step, explains why it gives the right answer using place value, and shows why it is far faster than counting. You then design algorithms for a familiar problem, finding the greatest common divisor of two numbers. Starting from the definition, you list divisors, compare lists and take the largest common entry. You learn to trace an algorithm, to use named values and lists, and to justify that an algorithm is correct. Next you refine the method to do less work, and see its limit: the work still grows with the size of the numbers. Euclid’s subtraction algorithm brings a new idea, replacing gcd(m, n) by gcd(n, m − n), and the Indian long-division method of Āryabhaṭa makes it fast by using remainders. The chapter ends with a checklist for designing algorithms, prime-testing examples and the story of al-Khwārizmī, whose name gave us the word algorithm.
हिंदी में पढ़ें
यह अध्याय स्तंभ जोड़ से शुरू करके बताता है कि कलनविधि क्या है, और महत्तम समापवर्तक (gcd) के लिए कलनविधियाँ बनाता, उनका औचित्य देता और उन्हें सुधारता है, जिसका अंत यूक्लिड की कलनविधि और आर्यभट की भाग विधि से होता है।
Learning Objectives
- Explain what an algorithm is and write one precisely
- Trace an algorithm step by step on a given input
- Justify the column method of addition using place value
- Write and trace an algorithm that lists the divisors of a number
- Compute the gcd from the definition using lists, and explain why it is correct
- Refine an algorithm to reduce work while keeping its answer
- Explain and use Euclid’s rule gcd(m, n) = gcd(n, m − n)
- Use the division (remainder) form of the Euclidean algorithm and compare its efficiency
- Design simple algorithms such as a prime test, and describe the origin of the word algorithm
Topics in this chapter
8 topics · tap a topic title to jump straight to it.
What is an algorithm? The column method of addition
1Think first
2The idea, in plain words
A small child adds 5 and 7 by drawing 5 dots, then 7 more dots, and counting all of them: 12. That works, but try it for 5834 + 4729. You would have to draw and count more than ten thousand dots! Long ago people found a much better way, and you use it every day without thinking about it.
The column method. Write the numbers one below the other so that the units are lined up, then the tens, then the hundreds, and so on. Add each column from right to left, and whenever a column total reaches 10 or more, write its units digit and carry 1 to the next column.
An algorithm is a step-by-step procedure that solves every problem of a certain kind, with steps so precise that anyone (or any machine) following them exactly gets the right answer. Here is the column method written as an algorithm.
- Step 1. Write the two numbers one below the other, lining up the digits from the right.
- Step 2. Set carry = 0. Start with the rightmost column.
- Step 3. Add the two digits of the column (a missing digit counts as 0) and the carry. If the total is less than 10, write it below and set carry = 0. If it is 10 or more, write its units digit below and set carry = 1.
- Step 4. If there is a column further to the left, move to it and repeat Step 3.
- Step 5. When no columns are left, if carry = 1, write 1 at the far left of the answer.
Basic steps. Every algorithm is built on a few actions we assume we can already do. Here the basic step is adding two single digits and a carry, a fact we know by heart. The algorithm then tells us in what order to use the basic step.
Every step matters. Suppose we forget Step 5. Then 58 + 47 gives: units 8 + 7 = 15, write 5, carry 1; tens 5 + 4 + 1 = 10, write 0, carry 1; stop. The answer written is 05, instead of 105. The algorithm works for 23 + 14 but fails whenever the leftmost column produces a carry. A good algorithm must handle every case, including numbers with different numbers of digits (that is why a missing digit counts as 0).
Why is it so much faster? Suppose we count one dot each second, and add one column (with a carry) in 15 seconds.
| Sum | Dots to count | Time by counting | Columns | Time by columns |
| 58 + 47 | 105 | about 1 min 45 s | 2 | 30 s |
| 583 + 472 | 1055 | about 17.5 min | 3 | 45 s |
| 5834 + 4729 | 10563 | nearly 3 hours | 4 | 1 min |
Each extra digit multiplies the counting work by about 10, but adds only one more column to the algorithm. The work of the column method grows with the number of digits, not with the size of the numbers. This idea, comparing how the work grows, will guide the whole chapter.
- Algorithm = precise steps + basic operations + order of steps
- Column method: work grows with the number of digits
3Worked example
- 58 + 47 = 105 needs 2 columns and a final carry.
- 5834 + 4729 = 10563 needs 4 columns and a final carry.
- Forgetting the last step turns 58 + 47 into the wrong answer 05.
4Your turn
In the column method, why can the carry never be more than 1?
5Remember this
Why an algorithm works: place value and carrying
1Think first
2The idea, in plain words
Giving a list of steps is only half the job. We must also justify that the steps always give the right answer. Addition is defined by joining the two collections and counting everything. Why should adding digits column by column give the same result?
Grouping by place value. In our number system, 583 means 5 hundreds, 8 tens and 3 units, and 472 means 4 hundreds, 7 tens and 2 units. Joining the two collections, we can join the units with the units, the tens with the tens, and the hundreds with the hundreds, because the order in which we put things together does not change the total. So
| Hundreds | Tens | Units | |
| 583 | 5 | 8 | 3 |
| 472 | 4 | 7 | 2 |
| Joined | 9 | 15 | 5 |
We get 9 hundreds, 15 tens and 5 units. But 15 tens is not a single digit. Ten tens make one hundred, so we exchange 10 of the tens for 1 hundred. Now we have 10 hundreds, 5 tens and 5 units. Ten hundreds make one thousand, so we exchange again: 1 thousand, 0 hundreds, 5 tens, 5 units, which is 1055. Each exchange is exactly what the algorithm calls a carry.
The justification in one sentence. The algorithm adds each place-value group separately and, whenever a group reaches ten, turns those ten into one item of the next larger group; since nothing is lost or created, the result is the true total.
Why line up from the right? The rightmost digit of every whole number is its units digit. Lining up from the right puts units under units and tens under tens, so we only ever join groups of the same size. Lining up from the left would add, for example, the hundreds of one number to the tens of another.
Extending the algorithm. Once you see why it works, you can change it for new situations with confidence. To add decimals such as 3.75 + 12.6, line up the decimal points (so that tenths are under tenths), fill a missing place with 0 (12.60), and add from the right exactly as before: 3.75 + 12.60 = 16.35. The reason is the same: groups of ten hundredths make a tenth, ten tenths make a unit.
Correctness and efficiency. From now on we will ask two questions about every algorithm:
- Correctness: does it always give the right answer? We answer with a justification.
- Efficiency: how much work does it take, and how does the work grow as the inputs grow?
A method that is fast but sometimes wrong is useless. A method that is correct but takes years for large numbers is not practical. Good algorithms are both correct and efficient.
- 10 units = 1 ten, 10 tens = 1 hundred, 10 hundreds = 1 thousand
- A carry = exchanging ten of one place for one of the next place
3Worked example
- 583 + 472: 9 hundreds, 15 tens, 5 units, regroup to 1055.
- 3.75 + 12.60 = 16.35 after lining up the decimal points.
- Lining up from the left would wrongly add hundreds to tens.
4Your turn
To add 7.4 and 15.86 with the column method, we should:
5Remember this
An algorithm for the divisors of a number
1Think first
2The idea, in plain words
To find the greatest common divisor (gcd), also called the highest common factor (HCF), of two numbers, we can break the problem into smaller pieces. First find all divisors of each number, then compare the two collections. Breaking a problem into simpler sub-problems is the first habit of algorithm design.
Sub-problem: list the divisors of n. Every divisor of n lies between 1 and n. The most direct method is to test each of these numbers in turn. We keep the divisors we find in a list, written in square brackets, such as [1, 2, 5]. At the start we know none, so the list is empty, [ ].
- Step 1. Start with an empty list, called list-of-divisors.
- Step 2. For each number j = 1, 2, 3, …, n: if j divides n exactly, add j at the end of list-of-divisors.
- Step 3. When j has reached n, report list-of-divisors.
Names for changing values. The names j and list-of-divisors let us talk about values that change as the algorithm runs. Without them, the steps would be clumsy to write.
Executing the algorithm. Running the steps by hand is called executing or tracing the algorithm. Here is a trace for n = 20.
| j | Does j divide 20? | list-of-divisors |
| 1 | yes | [1] |
| 2 | yes | [1, 2] |
| 3 | no | [1, 2] |
| 4 | yes | [1, 2, 4] |
| 5 | yes | [1, 2, 4, 5] |
| 6 to 9 | no | [1, 2, 4, 5] |
| 10 | yes | [1, 2, 4, 5, 10] |
| 11 to 19 | no | [1, 2, 4, 5, 10] |
| 20 | yes | [1, 2, 4, 5, 10, 20] |
Because we tested j in increasing order, the list comes out in increasing order. This will be useful in the next step. The basic step assumed is: we can decide whether one number divides another.
How much work? For n = 20 we made 20 tests; for n = 375 we would make 375; for n = 81000, eighty-one thousand. The work grows with the size of n itself, just like counting dots.
A first improvement: pairs. Divisors come in pairs whose product is n: for 20 the pairs are (1, 20), (2, 10), (4, 5). In each pair the smaller number is at most the square root of n, because if both were larger, their product would be larger than n. So it is enough to test j from 1 up to √n and record each divisor with its partner n ÷ j. For n = 20 that is only j = 1, 2, 3, 4 (since 4 × 4 = 16 is not more than 20 but 5 × 5 = 25 is). For n = 10000 it is 100 tests instead of 10000. The list then needs sorting if we want it in order.
- Divisors of n lie between 1 and n
- Divisors pair up as (j, n ÷ j); the smaller one is at most √n
3Worked example
- divisors(20) = [1, 2, 4, 5, 10, 20].
- divisors(84) = [1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84].
- divisors(120) has 16 entries, from 1 to 120.
4Your turn
Using divisor pairs, up to which number must we test to find all divisors of 100?
5Remember this
The first gcd algorithm: lists of common divisors
1Think first
2The idea, in plain words
Now we can put the pieces together. The greatest common divisor of two numbers m and n, written gcd(m, n), is the largest number that divides both. To compute it straight from this definition:
- Step 1. Let divisors-of-m = divisors(m).
- Step 2. Let divisors-of-n = divisors(n).
- Step 3. Start with an empty list, common-divisors.
- Step 4. For each number x in divisors-of-m: if x also appears in divisors-of-n, add x to common-divisors.
- Step 5. Report the last (rightmost) entry of common-divisors as gcd(m, n).
Example: gcd(84, 120).
| List | Entries |
| divisors(84) | 1, 2, 3, 4, 6, 7, 12, 14, 21, 28, 42, 84 |
| divisors(120) | 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120 |
| common-divisors | 1, 2, 3, 4, 6, 12 |
Going through the divisors of 84 one by one: 1, 2, 3, 4, 6 are in the second list; 7 is not; 12 is; 14, 21, 28, 42 and 84 are not. The common list is [1, 2, 3, 4, 6, 12], and its last entry gives gcd(84, 120) = 12.
Why is the last entry the greatest? Step 4 goes through divisors-of-m in increasing order, so common-divisors is built in increasing order too. Its last entry is therefore its largest. And common-divisors is never empty, because 1 divides every number.
Why is this algorithm correct? It follows the definition word for word: list every divisor of each number, keep those common to both, take the largest. Nothing is skipped, so the answer must be the gcd.
Data structures. While the algorithm runs, it keeps intermediate results: three lists of numbers. A way of organising such information is called a data structure. A list is the simplest one. Here it matters that the lists are sorted: because they are in increasing order, we know the answer is at the right end, and we could also stop early when comparing. Choosing a good data structure often makes an algorithm faster; you will meet many more in higher classes.
Related problems. With small changes the same plan solves other problems.
- Elements of List 1 that are not in List 2: go through List 1 and keep each x that does not appear in List 2.
- Least common multiple: lcm(m, n) is the smallest number that both divide. One method: check m, 2m, 3m, … and stop at the first one that n divides. For 84 and 120: 84, 168, …, 840; the first multiple of 84 divisible by 120 is 840 = 10 × 84 = 7 × 120.
- Shortcut: lcm(m, n) × gcd(m, n) = m × n, so lcm(84, 120) = 84 × 120 ÷ 12 = 840.
The cost. For gcd(84, 120) we tested 84 + 120 = 204 candidate divisors and then compared lists. For 54000 and 81000 each list has 80 entries and we would test 135000 candidates. The method is correct but slow. In the next topics we make it faster.
- gcd(m, n) = largest number dividing both m and n
- lcm(m, n) × gcd(m, n) = m × n
3Worked example
- gcd(84, 120) = 12 from the common list [1, 2, 3, 4, 6, 12].
- gcd(375, 825) = 75.
- lcm(84, 120) = 840 = 84 × 120 ÷ 12.
4Your turn
Why does the list common-divisors always have at least one entry?
5Remember this
Improving an algorithm step by step
1Think first
2The idea, in plain words
A correct algorithm is a good start. The next question is: can it do less work and still be correct? Let us refine the gcd algorithm in small steps, checking each time that the answer does not change.
Refinement 1: one scan instead of two. The first algorithm tests 1 to m for divisors of m and then starts again, testing 1 to n for divisors of n: m + n tests. Instead, run j once from 1 to the larger of the two numbers, written max(m, n). For each j, if j divides m, add it to the first list; if j divides n, add it to the second. Now there are only max(m, n) tests.
Refinement 2: collect common divisors directly. In the same scan, simply ask: does j divide both m and n? If yes, add j to common-divisors. There is no need for separate lists.
Refinement 3: stop at the smaller number. A common divisor cannot be larger than the smaller of the two numbers, min(m, n). So the scan need only run from 1 to min(m, n).
Refinement 4: drop the list. Each time we find a larger common divisor, all the earlier ones become useless. So keep only one number, called most-recent-common-divisor. Start it at 1 (since 1 always divides both). For each k from 2 to min(m, n): if k divides both m and n, set most-recent-common-divisor = k. When the scan ends, it holds the gcd.
| k | divides 18? | divides 24? | most-recent-common-divisor |
| start | 1 | ||
| 2 | yes | yes | 2 |
| 3 | yes | yes | 3 |
| 4, 5 | no | 3 | |
| 6 | yes | yes | 6 |
| 7 to 18 | only 9 and 18 | neither 9 nor 18 | 6 |
So gcd(18, 24) = 6. The table is a trace of the refined algorithm.
Refinement 5: scan backwards. If we test k = min(m, n), min(m, n) − 1, …, down to 1, the first common divisor we meet is the greatest, and we can stop at once. For gcd(84, 120) we test 84, 83, …, 12 and stop: 73 tests, compared with 204 for the first algorithm. But when the gcd is 1 we still test every number down to 1.
Comparing the versions for gcd(84, 120).
| Version | Numbers tested |
| Two separate scans | 84 + 120 = 204 |
| One scan to max(m, n) | 120 |
| One scan to min(m, n) | 84 |
| Backward scan from min(m, n) | 73 (stops at 12) |
Correctness is kept. Each refinement changes how the work is organised, not what is computed, so every version still returns the gcd.
The limit of this approach. All these versions may still need to test every number up to min(m, n). If the smaller number grows from 3 digits to 5 digits, the work grows about 100 times. To do better we need a new idea, not just tidier bookkeeping. That idea is Euclid’s.
- Common divisors of m and n are at most min(m, n)
- Backward scan: first common divisor found = gcd
3Worked example
- One scan to max(84, 120) = 120 tests instead of 204.
- Scanning 1 to 18 for gcd(18, 24) updates the answer at 2, 3 and 6.
- Backward scan for gcd(84, 120) stops at 12 after 73 tests.
4Your turn
Why may the scan for common divisors stop at min(m, n)?
5Remember this
Euclid’s subtraction algorithm
1Think first
2The idea, in plain words
More than 2300 years ago Euclid wrote down, in his Elements, a completely different way to find the gcd. Instead of searching through divisors, it keeps replacing the problem with a smaller problem that has the same answer. (Historians think earlier Greek mathematicians already knew it; Euclid is the first to record it.)
The key fact. If m ≥ n, then gcd(m, n) = gcd(n, m − n).
Why? We show that m, n and n, m − n have exactly the same common divisors.
- If d divides m and n, write m = ad and n = bd. Then m − n = ad − bd = (a − b)d, so d also divides m − n.
- If d divides n and m − n, write n = xd and m − n = yd. Then m = n + (m − n) = (x + y)d, so d also divides m.
The two collections of common divisors are identical, so their largest members are equal. Think of it with tiles: if strips of length m and n can both be covered exactly by tiles of length d, then so can the leftover piece m − n, and the other way round.
When to stop. gcd(m, m) = m, and the next reduction gives gcd(m, 0). Every number divides 0, so gcd(m, 0) = m. That is the stopping rule.
Euclid’s algorithm for gcd(m, n)
- Step 1. If m is smaller than n, swap them and compute gcd(n, m).
- Step 2. If n = 0, report m as the answer.
- Step 3. Otherwise replace the problem by gcd(n, m − n) and go back to Step 1.
Trace for gcd(84, 36).
| Problem | Action |
| gcd(84, 36) | 84 − 36 = 48: becomes gcd(36, 48) |
| gcd(36, 48) | swap: gcd(48, 36) |
| gcd(48, 36) | 48 − 36 = 12: gcd(36, 12) |
| gcd(36, 12) | 36 − 12 = 24: gcd(12, 24), swap: gcd(24, 12) |
| gcd(24, 12) | 24 − 12 = 12: gcd(12, 12) |
| gcd(12, 12) | 12 − 12 = 0: gcd(12, 0) |
| gcd(12, 0) | n = 0, answer 12 |
Five subtractions found the answer, with no list of divisors at all.
Does it always stop? Each subtraction makes the larger number smaller, while all numbers stay whole and not negative. A whole number cannot keep decreasing for ever, so eventually one number becomes 0.
The weak spot. Try gcd(101, 2). The algorithm subtracts 2 again and again: gcd(99, 2), gcd(97, 2), …, gcd(3, 2), gcd(2, 1), gcd(1, 1), gcd(1, 0). That is 52 reductions. In general gcd(2k + 1, 2) needs about k steps, so the work still grows with the size of the number. Repeated subtraction of the same number is really a slow way of dividing, and that suggests the improvement in the next topic.
- If m ≥ n: gcd(m, n) = gcd(n, m − n)
- gcd(m, 0) = m
3Worked example
- gcd(84, 36): five subtractions give 12.
- gcd(375, 825): seven reductions give 75.
- gcd(101, 2) takes 52 reductions to reach 1.
4Your turn
Which equation is the basis of Euclid’s subtraction algorithm (m ≥ n)?
5Remember this
Āryabhaṭa’s division method and the efficient Euclidean algorithm
1Think first
2The idea, in plain words
When 101 has 2 subtracted from it again and again until less than 2 remains, what remains is 1, which is exactly the remainder when 101 is divided by 2. One division does the work of fifty subtractions. Indian mathematicians, at least from the time of Āryabhaṭa (Āryabhaṭīya, 499 CE), used this ‘long division’ procedure with place-value numerals to find the greatest common divisor.
Notation. m mod n means the remainder when m is divided by n. For example, 17 mod 5 = 2, 33 mod 7 = 5 and 1071 mod 462 = 147 (since 1071 = 2 × 462 + 147).
The improved rule. If m ≥ n and n is not 0, then gcd(m, n) = gcd(n, m mod n). The reason is the same as before. Write m = qn + r with r = m mod n. Any d that divides m and n divides m − qn = r; any d that divides n and r divides qn + r = m. So the common divisors do not change.
- Step 1. If m is smaller than n, swap them.
- Step 2. If n = 0, report m as the answer.
- Step 3. Otherwise replace the problem by gcd(n, m mod n) and go back to Step 1.
Only Step 3 has changed. After the first step, the remainder is always smaller than the divisor, so Step 1 never needs to swap again.
Example: gcd(1071, 462).
| Division | Quotient | Remainder | New problem |
| 1071 ÷ 462 | 2 | 147 | gcd(462, 147) |
| 462 ÷ 147 | 3 | 21 | gcd(147, 21) |
| 147 ÷ 21 | 7 | 0 | gcd(21, 0) |
So gcd(1071, 462) = 21 after three divisions. The subtraction version would need 12 reductions. For gcd(101, 2): 101 mod 2 = 1, giving gcd(2, 1); 2 mod 1 = 0, giving gcd(1, 0) = 1. Two steps instead of 52.
The long-division layout. Each remainder becomes the next divisor, and the previous divisor becomes the next dividend. Written as a ladder of divisions, the last non-zero remainder (or the last divisor, when the remainder becomes 0) is the gcd. This is the method taught in Indian texts and still used in schools today.
How efficient is it? It can be shown that the number of division steps grows only with the number of digits of the numbers, as in column addition, and not with their size. (The proof is for higher classes.) Even numbers with many digits, such as gcd(2587392, 157656) = 24, take only 7 divisions.
| Pair | gcd | Subtraction steps | Division steps |
| 84, 36 | 12 | 5 | 2 |
| 1071, 462 | 21 | 12 | 3 |
| 101, 2 | 1 | 52 | 2 |
| 2587392, 157656 | 24 | 57 | 7 |
- m = q × n + r, with r = m mod n smaller than n
- gcd(m, n) = gcd(n, m mod n)
- gcd(m, 0) = m
3Worked example
- 17 mod 5 = 2, 33 mod 7 = 5.
- gcd(1071, 462): remainders 147, 21, 0, so the gcd is 21.
- gcd(252, 198): remainders 54, 36, 18, 0, so the gcd is 18.
- gcd(1789287, 237656) = 1 after 13 divisions.
4Your turn
What is gcd(91, 35) by the division method?
5Remember this
Designing algorithms, and where the word comes from
1Think first
2The idea, in plain words
Looking back over the chapter, every algorithm we wrote was made from a few kinds of instruction:
- Basic steps that we assume we can do directly: adding two digits, testing whether j divides n.
- Repetition: doing the same step for each item, as in ‘for each j = 1, 2, …, n’, or ‘go back to Step 1’.
- Conditions: doing a step only when something is true, as in ‘if j divides n, add j to the list’.
- Named values that change as we go, such as carry, j and most-recent-common-divisor, and data structures such as lists.
A design checklist. When you write an algorithm for a new problem:
- Step 1. Understand exactly what is given and what must be produced.
- Step 2. Break the problem into smaller problems you already know how to solve.
- Step 3. Write precise steps, using names for values that change.
- Step 4. Trace the steps on small examples, including awkward cases (0, 1, equal numbers, different lengths).
- Step 5. Justify why the steps are always correct.
- Step 6. Count the work and ask how it grows; then look for a better idea.
Example: is n prime? A prime has exactly two divisors, 1 and itself. So: compute divisors(n); if the list has exactly 2 entries, report ‘prime’, otherwise ‘not prime’. This is correct (and handles n = 1, whose list [1] has one entry, so 1 is not prime). A faster version tests only j = 2, 3, … up to √n and stops at the first divisor found. For 221 it finds 13 (221 = 13 × 17) after 12 tests.
Example: prime divisors of n. Compute divisors(n), then keep only those entries d for which ‘is d prime?’ says yes. For 60: divisors [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60] give prime divisors [2, 3, 5]. One algorithm is used as a basic step inside another.
A pinch of history. Around 820 CE the Persian scholar Muḥammad ibn Mūsā al-Khwārizmī, working at the House of Wisdom in Baghdad, wrote a book explaining the Indian place-value numerals and the Indian methods for addition, subtraction, multiplication and division. Its Arabic original is lost, but a Latin translation from the 12th century, known as Algoritmi de numero Indorum (‘al-Khwārizmī on the Indian numbers’), spread these methods across Europe. ‘Algoritmi’, the Latin form of his name, gave the word algorism for these Indian ways of calculating, and later algorithm, which now means any precise step-by-step procedure.
Algorithms today. A computer program is an algorithm written in a language a machine can follow. Maps that find the shortest route, apps that sort your photos and the methods that keep online payments secure (which use gcd calculations with very large numbers) all depend on algorithms that are correct and efficient, exactly the two questions of this chapter.
- Algorithm = basic steps + repetition + conditions + named values
- To test n for primality, divisors up to √n are enough
3Worked example
- prime(n): n is prime exactly when divisors(n) has 2 entries.
- Prime divisors of 60 are 2, 3 and 5.
- 221 = 13 × 17 is found after testing j = 2 to 13.
4Your turn
The word ‘algorithm’ comes from:
5Remember this
Key Concepts
- Algorithm
- A precise, step-by-step procedure that solves every problem of a given kind.
- Basic step
- An action an algorithm assumes can be done directly, such as adding two digits.
- Executing (tracing)
- Carrying out the steps of an algorithm by hand on a particular input, recording values as they change.
- Carry
- In column addition, the 1 passed to the next column when a column total is 10 or more; it stands for ten of the smaller place.
- Divisor
- A whole number that divides another exactly; also called a factor.
- List
- An ordered collection of values written in square brackets, such as [1, 2, 4]; the simplest data structure.
- Data structure
- A way of organising the information an algorithm keeps while it runs.
- gcd (HCF)
- The greatest common divisor of two numbers: the largest number dividing both.
- lcm
- The least common multiple: the smallest positive number that both numbers divide; lcm(m, n) × gcd(m, n) = m × n.
- Correctness
- The property that an algorithm always gives the right answer; shown by a justification.
- Efficiency
- How much work an algorithm needs and how that work grows with the size of the input.
- Refinement
- A change that makes an algorithm do less work without changing what it computes.
- Euclid’s subtraction rule
- For m ≥ n, gcd(m, n) = gcd(n, m − n).
- m mod n
- The remainder when m is divided by n; for example, 33 mod 7 = 5.
- Euclidean (division) algorithm
- Repeatedly replace gcd(m, n) by gcd(n, m mod n) until the second number is 0; the first is then the gcd.
- Stopping rule
- The condition that ends an algorithm; for gcd it is reaching gcd(m, 0) = m.
- Al-Khwārizmī
- Scholar of Baghdad (about 780–850 CE) whose book on Indian arithmetic gave us the word algorithm.
End-of-Chapter Trial Paper & Test Questions
Topic-wise questions to test your understanding of every concept in this chapter.
-
What is an algorithm? Give an example from arithmetic. / कलनविधि (एल्गोरिद्म) क्या है? अंकगणित से एक उदाहरण दीजिए।
Show answer
An algorithm is a precise step-by-step procedure that solves every problem of a certain kind. The column method of addition, which adds digits from right to left with carries, is an example. / कलनविधि एक सटीक चरणबद्ध प्रक्रिया है जो किसी प्रकार की हर समस्या हल करती है। दाएँ से बाएँ अंकों को हासिल के साथ जोड़ने वाली स्तंभ विधि इसका उदाहरण है।
-
Why can the carry in column addition never be more than 1? / स्तंभ जोड़ में हासिल कभी 1 से अधिक क्यों नहीं होता?
Show answer
The largest column total is 9 + 9 + 1 = 19, which contains only one ten, so at most 1 is carried. / किसी स्तंभ का सबसे बड़ा योग 9 + 9 + 1 = 19 है, जिसमें केवल एक दहाई है, इसलिए अधिकतम 1 ही हासिल जाता है।
-
List the divisors of 28 using the divisor algorithm. / भाजक कलनविधि से 28 के भाजकों की सूची बनाइए।
Show answer
Testing j = 1 to 28 in order, the numbers that divide 28 are 1, 2, 4, 7, 14 and 28, so divisors(28) = [1, 2, 4, 7, 14, 28]. / j = 1 से 28 तक क्रम से जाँचने पर 28 को विभाजित करने वाली संख्याएँ 1, 2, 4, 7, 14 और 28 हैं, अतः divisors(28) = [1, 2, 4, 7, 14, 28]।
-
Find gcd(84, 120) using lists of divisors. / भाजकों की सूचियों से gcd(84, 120) ज्ञात कीजिए।
Show answer
The common divisors of 84 and 120 are [1, 2, 3, 4, 6, 12]. The list is in increasing order, so the last entry, 12, is the gcd. / 84 और 120 के उभयनिष्ठ भाजक [1, 2, 3, 4, 6, 12] हैं। सूची बढ़ते क्रम में है, इसलिए अंतिम संख्या 12 ही gcd है।
-
Prove that if d divides m and n, then d divides m − n. / सिद्ध कीजिए कि यदि d, m और n दोनों को विभाजित करता है, तो d, m − n को भी विभाजित करता है।
Show answer
Write m = ad and n = bd for whole numbers a and b. Then m − n = ad − bd = (a − b)d, which is a multiple of d. / m = ad और n = bd लिखें, जहाँ a और b पूर्ण संख्याएँ हैं। तब m − n = ad − bd = (a − b)d, जो d का गुणज है।
-
Use Euclid’s subtraction algorithm to find gcd(60, 16). / यूक्लिड की व्यवकलन कलनविधि से gcd(60, 16) ज्ञात कीजिए।
Show answer
Subtracting 16 three times gives gcd(16, 12), then gcd(12, 4), then subtracting 4 gives gcd(4, 0). So gcd(60, 16) = 4. / 16 को तीन बार घटाने पर gcd(16, 12), फिर gcd(12, 4) मिलता है, और 4 घटाते रहने पर gcd(4, 0) मिलता है। अतः gcd(60, 16) = 4।
-
Find gcd(1071, 462) by the division method. / भाग विधि से gcd(1071, 462) ज्ञात कीजिए।
Show answer
1071 = 2 × 462 + 147, 462 = 3 × 147 + 21 and 147 = 7 × 21 + 0. The last divisor is 21, so gcd(1071, 462) = 21. / 1071 = 2 × 462 + 147, 462 = 3 × 147 + 21 और 147 = 7 × 21 + 0। अंतिम भाजक 21 है, अतः gcd(1071, 462) = 21।
-
Why is the division method faster than the subtraction method for gcd(101, 2)? / gcd(101, 2) के लिए भाग विधि व्यवकलन विधि से तेज़ क्यों है?
Show answer
Subtraction removes 2 one step at a time and needs 52 reductions, but one division gives the remainder 1 at once, so only 2 steps are needed. / व्यवकलन में 2 एक-एक बार घटाया जाता है और 52 चरण लगते हैं, जबकि एक भाग से शेषफल 1 तुरंत मिल जाता है, इसलिए केवल 2 चरण लगते हैं।
-
Find lcm(84, 120) using the gcd. / gcd की सहायता से lcm(84, 120) ज्ञात कीजिए।
Show answer
Since lcm × gcd = m × n and gcd(84, 120) = 12, lcm(84, 120) = 84 × 120 ÷ 12 = 840. / चूँकि lcm × gcd = m × n और gcd(84, 120) = 12 है, अतः lcm(84, 120) = 84 × 120 ÷ 12 = 840।
-
Write an algorithm to test whether a number n is prime. / किसी संख्या n के अभाज्य होने की जाँच के लिए कलनविधि लिखिए।
Show answer
Compute divisors(n). If the list has exactly two entries, 1 and n, report that n is prime; otherwise report that it is not prime. Testing up to √n is enough for a faster version. / divisors(n) निकालिए। यदि सूची में ठीक दो संख्याएँ, 1 और n, हों तो n अभाज्य है, अन्यथा नहीं। तेज़ रूप के लिए √n तक जाँचना पर्याप्त है।
-
Where does the word ‘algorithm’ come from? / ‘एल्गोरिद्म’ शब्द कहाँ से आया है?
Show answer
It comes from Algoritmi, the Latin form of the name of al-Khwārizmī, whose book on Indian numerals and methods of calculation was translated into Latin and spread in Europe. / यह अल-ख़्वारिज़्मी के नाम के लैटिन रूप ‘अल्गोरित्मी’ से आया है, जिनकी भारतीय अंकों और गणना विधियों पर पुस्तक का लैटिन अनुवाद यूरोप में फैला।
Related Laws & Principles
Explore allFoundational laws & principles behind this chapter. Each one opens a full page — what it says, why it matters, five practice questions and the mistakes to avoid.