What an algorithm is
The method for finding the greatest common divisor in Book VII of Euclid's Elements, written about 2,300 years ago, still runs in computers today.
A method that always works
Suppose you need the largest number that divides both 1071 and 462. You could try every number from 462 down to 1 and stop at the first one that divides both. That works, but it may take hundreds of tries. There is a shorter way. Divide 1071 by 462 and keep the remainder, 147. Now divide 462 by 147 and keep the remainder, 21. Divide 147 by 21: the remainder is 0, so the answer is 21. Three divisions instead of several hundred.
That shorter way is an algorithm: a finite list of precise steps that takes some input and produces the right output. This one is Euclid's algorithm for the greatest common divisor, and it appears in Book VII of the Elements, written around 300 BC. Euclid described it with repeated subtraction of lengths rather than division, but the idea is the same, and the version with remainders is what programs use today.
function gcd(a, b) {
while (b !== 0) {
const r = a % b; // remainder
a = b;
b = r;
}
return a;
}
console.log(gcd(1071, 462)); // 21What makes a list of steps an algorithm
Textbooks such as the one by Cormen, Leiserson, Rivest and Stein describe an algorithm as a well-defined procedure that turns an input into an output. Three things are packed into that description, and each one matters when you write code.
- Precise. Each step means one thing. "Divide a by b and keep the remainder" is precise; "make the numbers smaller somehow" is not.
- Finite. It must stop. In Euclid's algorithm the remainder is always smaller than the number you divided by, so it shrinks every round and must reach 0.
- Correct. For every allowed input, the output is the right answer, not only for the examples you tried.
A cooking recipe is often given as an everyday example, and it is a fair one: ingredients are the input, the dish is the output, and the steps are in order. The difference is how much the reader fills in. A cook decides what "a pinch" means. A computer decides nothing, which is why algorithms for computers are written with every step spelled out.
Same problem, different algorithms
One problem usually has many correct algorithms, and they can differ enormously in how much work they do. For the greatest common divisor of 1071 and 462, trying every number from 462 downward needs 442 tries before it reaches 21, while Euclid's method needs 3 divisions. Both are correct. Only one is quick. Much of this course is about that second question: once a method is correct, how many steps does it take, and how does that number grow when the input gets bigger?
To answer it, computer scientists count basic steps (a comparison, a swap, a division) rather than seconds. Seconds depend on the machine. A count of steps depends only on the algorithm and the input, so it lets you compare two methods fairly. The lab below counts comparisons for you while it sorts a row of bars. Try the same starting order with two different methods and watch the counters.
Drag to turn. Scroll or pinch to zoom.
Inputs, outputs and the steps between
Before writing an algorithm, write down exactly what goes in and what must come out. For the greatest common divisor: the input is two positive whole numbers, and the output is one whole number that divides both, with no larger number doing the same. That description tells you how to test the result, and it often hints at the steps. It also reveals the edge cases: what if one number is 0? (The loop above returns the other number, which is the correct answer.) What if the two numbers are equal? Thinking about edge cases before running the code catches many mistakes.
- About 300 BC: Euclid's Elements, Book VII: the greatest common divisor method.
- 9th century: Al-Khwarizmi in Baghdad writes on Hindu numerals; his Latin name gives the word algorithm.
- 1843: Ada Lovelace's Note G: steps for the Analytical Engine to compute Bernoulli numbers.
- 1945: John von Neumann proposes merge sort.
- 1959: Tony Hoare devises quicksort in Moscow (published 1961).
| algorithm | A finite list of precise steps that turns an input into the right output |
| input | The data an algorithm starts from |
| output | The result the algorithm must produce |
| correct | Gives the right output for every allowed input |
| finite | Always stops after some number of steps |
| greatest common divisor | The largest whole number that divides two numbers exactly |
| edge case | An unusual input such as 0, an empty list or equal values |
| step count | How many basic operations an algorithm performs |
- Using Euclid's algorithm, what is the greatest common divisor of 48 and 18?
- How many divisions does Euclid's algorithm need for 1071 and 462?
- Which of these is not a property every algorithm must have?
- It stops after a finite number of steps
- Each step is precise
- It uses a computer
- It gives the right output for every allowed input
- Why does Euclid's algorithm always stop?
- The remainder is always smaller than the divisor, so it shrinks to 0
- It stops after 10 rounds
- Computers stop loops automatically
- The numbers grow until they match
- The word algorithm comes from the name of
- Euclid
- Al-Khwarizmi
- Ada Lovelace
- Alan Turing
- Why do computer scientists count steps instead of timing in seconds?
- Seconds depend on the machine; step counts depend only on the algorithm and input
- Clocks are not accurate
- Steps are always faster
- Seconds cannot be measured in code
- A method gives the right answer for the five examples you tried. Is it correct?
- Yes, five examples prove it
- Not necessarily: it must be right for every allowed input
- Only if it is fast
- Only if it uses a loop
- What does gcd(35, 0) return with the loop in this unit?
- In which year did Ada Lovelace publish her notes, including Note G?
- 1801
- 1843
- 1945
- 1961
- Put the steps of Euclid's algorithm in order.
- An algorithm is a finite list of precise steps from input to correct output.
- Euclid's algorithm finds a greatest common divisor by repeated remainders.
- One problem can have many correct algorithms with very different step counts.
- Write down the input, the output and the edge cases before the steps.
- The word comes from al-Khwarizmi's Latin name.