Lesson 1 of 5 · Open
What an algorithm is
Instructions that leave nothing open.
Two people bake from the same recipe, and still their cakes taste different. The reason is in the recipe: “a pinch of salt,” “bake until golden brown.” Instructions like these are ambiguous: they leave room for interpretation, and each person fills it differently.
An algorithm is a set of instructions that leaves no such room. Every step is unambiguous: it can be understood in only one way. Whoever carries it out does not have to decide or guess anything.
| Ambiguous | Unambiguous |
|---|---|
| Add a pinch of salt | Add 1 g of salt |
| Bake until the cake is golden brown | Bake for 40 minutes at 175 °C |
| Stir for a while | Stir for 3 minutes |
From the input to the result
An algorithm usually starts with an input: what you begin with, for example a number. At the end there is the result, also called the output. In between are the steps, one after the other.
New terms
- algorithm
- a set of instructions made of unambiguous steps that leads to a result
- unambiguous
- can be understood in only one way, nobody has to guess
- ambiguous
- leaves room, and each person fills it differently
- input
- what an algorithm starts with, for example a number
- result
- what comes out at the end, also called the output
Example
Instructions with three steps
Take a number. Triple it. Add 4. With the input 10 it goes like this:
- Take the number: 10
- Triple it: 10 · 3 = 30
- Add 4: 30 + 4 = 34
Whoever follows these steps exactly gets 34 from the input 10: you, your neighbor, or a computer. A different input can give a different result. The same input gives the same result, no matter how often and no matter who does it.
The order is part of it
Example
Same steps, different order
Swap the two calculation steps of the instructions: Take a number. Add 4. Triple the result. The input is 10 again.
- Take the number: 10
- Add 4: 10 + 4 = 14
- Triple the result: 14 · 3 = 42
- The same input now gives 42 instead of 34. So an algorithm also fixes the order in which the steps come.
An algorithm comes to an end
An algorithm is also finite: it is done after a limited number of steps. “Start at 1 and keep doubling the number, without end” is therefore not an algorithm, because that never gives a result. If it says “double until the number is greater than 5” instead, it stops at 8 after three doublings.
Quick check
“Count on from 1 until you are tired.” Is that an algorithm?
Example
No computer: 47 + 38 in columns
Adding two numbers in columns is an algorithm for pencil and paper. You work place by place from the right. If a sum is 10 or more, you write only its last digit and carry 1 to the next place.
- Ones: 7 + 8 = 15, write 5, carry 1
- Tens: 4 + 3 + 1 = 8, write 8
- Result: 85
Every step is unambiguous, and after the last place you are done. A computer carries out such steps in just the same way, only much faster. In a program, algorithms are written down so that a computer can carry them out.
Typical mistake
Thinking that algorithms exist only inside computers. That cannot be right: people did written arithmetic long before the first computer. The word itself is much older too: it goes back to the scholar al-Khwarizmi, who described methods of calculation in the 9th century.
Remember
Tasks
Task 1 · Choice
A recipe is supposed to become an algorithm. Which step is ambiguous?
Task 2 · Input
Follow the instructions exactly. What is the result for the input 9?
Task 3 · Sorting
Algorithm or not? Sort each set of instructions.
6 cards left
Go deeper
Task 4 · Order · optional
You add 29 + 44 in columns. Put the steps in the order in which you carry them out.
- 1Write 3, carry 1
- 2Read off the result: 73
- 3Add the ones: 9 + 4 = 13
- 4Write 7 to the left of the 3
- 5Add the tens: 2 + 4 + 1 = 7
Task 5 · Several right · optional
What holds for every algorithm?
Task 6 · Input · optional