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.

AmbiguousUnambiguous
Add a pinch of saltAdd 1 g of salt
Bake until the cake is golden brownBake for 40 minutes at 175 °C
Stir for a whileStir 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:

  1. Take the number: 10
  2. Triple it: 10 · 3 = 30
  3. 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.

  1. Take the number: 10
  2. Add 4: 10 + 4 = 14
  3. Triple the result: 14 · 3 = 42
  4. 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.

  1. Ones: 7 + 8 = 15, write 5, carry 1
  2. Tens: 4 + 3 + 1 = 8, write 8
  3. 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

An algorithm is a finite sequence of unambiguous steps. Anyone who follows them exactly gets the same result for the same input.

Tasks

Three tasks complete the lesson. The order does not matter, the text above stays where it is.

Task 1 · Choice

A recipe is supposed to become an algorithm. Which step is ambiguous?

One answer is right.

Task 2 · Input

Follow the instructions exactly. What is the result for the input 9?

Enter the result.

Result =

Task 3 · Sorting

Algorithm or not? Sort each set of instructions.

Drag every card into its group, or tap the group.

6 cards left

Pour in 300 ml of water and heat it to 60 °C

Go deeper

Three more tasks, optional. They do not count for completing the lesson.

Task 4 · Order · optional

You add 29 + 44 in columns. Put the steps in the order in which you carry them out.

Drag the rows into the right order.

  1. 1Write 3, carry 1
  2. 2Read off the result: 73
  3. 3Add the ones: 9 + 4 = 13
  4. 4Write 7 to the left of the 3
  5. 5Add the tens: 2 + 4 + 1 = 7

Task 5 · Several right · optional

What holds for every algorithm?

Several answers are right. Pick all of them.

Task 6 · Input · optional

Kim follows an algorithm exactly: 5 gives 23, and 8 gives 35. Noah follows it exactly with the input 5. What does he get?

Enter the result.

Result =

Back to the chapter