Interactive Lessons

Read, follow and write algorithms

Lecture notes

Definition

Algorithm

An algorithm is a list of steps that solves a problem. If you do the steps in the right order, you always get the answer.
n×22n+32n+3
Example
Make tea: 1 boil water, 2 add the tea bag, 3 wait 3 minutes. Right order, same cup every time:3 steps → 1 cup of tea
123 min3
Definition

Input and output

Input is what you put in and output is what comes out. A step “Input n” asks for a number and keeps it in the letter n.
nn×2+1n
Example
Input . The algorithm doubles it and adds 3. Output:
n6n×2+315n15
Definition

Variable

A variable is a letter that keeps a number. A step like n = n × 2 + 1 works out the right side with the old n and keeps the answer in n.
n=4n×2+1n=9
Example
Start with and do twice. The old is used each time:, then
n=5n+3n=8n+3n=11
Definition

Flowchart

A flowchart is a picture of an algorithm: boxes joined by arrows. You follow the arrows from Start to Stop.
nn×2n
Example
Flowchart: Start → Input → → Output → Stop. For the arrows lead to:
n3n×412n12
Definition

Step list

A step list is the same algorithm written line by line. The steps are numbered 1, 2, 3 … and a step can say “go to step 5”.
n1n=n−22n>5?3n45✘✔
Example
1 Input . 2 . 3 If , go to step 5. 4 Output . 5 Stop.For : , → step 5
n1n=n−22n>5?3n45✘✔
Rule

The four shapes

Oval: start and stop. Parallelogram: input and output. Rectangle: a calculation. Diamond: a question with Yes and No.
nn×3n>10?
Example
Which shape is the box “”? A calculation is a rectangle. The question “?” is a diamond:, → Yes
n×3n>10?
Rule

Follow the steps in order

Start at step 1. Do a step and go on to the next one, unless the step says “go to”.
…1…2…3
Example
1 Input . 2 . 3 . 4 Output . For :,
n=2n+1n=3n×3n=9
Rule

A question (decision)

A step “If n > 5, go to step 8” asks a question. Yes: jump to step 8. No: go on to the next step. For n = 7 the answer is Yes.
n>5?……8✘✔
Example
A step “If , go to step 6”. For the answer is No, so go on to step 4: is false
n>10?4…4…6✘✔
Rule

mod: the remainder

n mod 2 is the remainder when you divide n by 2. If n mod 2 = 0, the number n is even. 7 mod 2 = 1, so 7 is odd.
1
Example
Is 14 even or odd? Divide by 2 and look at the remainder: → even
0
Rule

A loop

A step “Go to step 4” that jumps back makes a loop: the same steps are done again and again until a question says stop.
s=0,n=11s=s+n2n=n+13n<4?4s✔✘
Example
1 , . 2 . 3 . 4 If , go to step 2. The loop adds:
s=0,n=11s=s+n2n=n+13n<4?4s✔✘
Rule

Trace table

A trace table has a column for every letter and a new row every time the question of the loop is asked. It shows what changes in every round.
n123s136
Example
Trace table of the loop that adds 1 to 3. After each round is 1, 3, 6: →
n123s136
Rule

The number of rounds

A loop that asks its question n + 1 times goes round n times: the last question says stop. For n = 5 the question is asked 6 times.
012345
Example
Add 1 to 3 in a loop. The question is asked 4 times, the loop goes round 3 times:
1234
Rule

Write your own algorithm

Write the steps in order: Input first, then the calculations and the questions, then Output and Stop. Test it with small numbers.
n…?…✘✔
Example
Write an algorithm for the price of stickers at €2 each plus €1 for the bag. Input , then calculate, then output. For : → €11
n5n×2+111n11
Rule

Guess in the middle

If you always guess the middle number, every guess throws away half of the numbers. 100 numbers need at most 7 guesses.
0255075100
Example
Guess a number from 1 to 100. The first guess is 50. If the number is higher, only 50 numbers are left; the next guess is 75:, then
05075100
Rule

The limits of an algorithm

An algorithm needs exact steps. “Choose the nicest picture” or “pour in a little water” cannot be followed by a machine. A loop that never reaches its stop value runs for ever, a way of working that needs far too many steps is no use, and an algorithm is only as good as the answers it receives. The tab Limits shows all four.
Example
Start at 4 and add 2 until the number is 10: 4, 6, 8, 10, and it stops. Start at 5: 5, 7, 9, 11, … the number jumps over 10 and the loop never ends. Sorting 10 billion names by comparing every pair takes about steps: about 1 584 years at a billion steps a second. Sorting in halves takes about steps: about 6 minutes.
Rule

Improve an algorithm

An algorithm can be made better: the same answer in fewer steps. Look for a step that does nothing useful, change it and count the steps before and after in a trace table. A change that makes the answer wrong is no improvement. The tab Improve it does this for the largest of three numbers.
Example
For the list 4, 9, 7 the given algorithm needs 11 steps. Start with m = a and delete the check for a: 9 steps and the same answer, 9. That saves 2 steps. The change “stop after the second number” says 5 for the list 3, 5, 8, but the largest is 8: faster, but wrong.
Tip

Test with small numbers

Try your algorithm with easy numbers first, like 0, 1 and 10, and work out the answer yourself.
n01102n+11321
Example
Test “double and add 1” with 0, 1 and 10 and check by hand:, ,
n01102n+11321
Tip

Write down the letters

After every step, write down the number of every letter. A trace table does this for you.
n×2n×2−3n×2−3
Example
Follow and then from . Write after each step:
5×210−37
Tip

Algorithms are everywhere

A recipe, the way to school and the rules of a game are algorithms too: steps in the right order.
…1…2…3
Example
Your walk to school: 1 leave the house, 2 turn left at the shop, 3 walk 200 m. Steps in the right order:3 steps, 200 m
12200 m3
Watch out!

The order matters

The same steps in another order give another answer: 4 × 2 + 1 = 9, but (4 + 1) × 2 = 10.
4×28+194+15×210
Example
Start with : add 3 and then double, or double and then add 3?, but
5+38×2165×210+313
Watch out!

Count the rounds carefully

A loop that adds 1 + 2 + 3 + 4 + 5 goes round 5 times, but asks its question 6 times.
123456
Example
A loop adds 1 + 2 + 3 + 4 to get 10. How many rounds?✘ 5 rounds ✔ 4 rounds (the question is asked 5 times)
12345✔
Watch out!

Yes and No

Do not mix up the two arrows: Yes jumps to the step that is written, No goes on to the next step.
n>5?……8✘✔
Example
“If , go to step 8”, with :✘ Yes, jump to step 8 ✔ is No, go on to the next step
n>5?3……8✘✔
Special case

A loop that never stops

If the question never says stop, the algorithm never ends. Check that something in the loop changes every round.
n=n×12n<10?3✔
Example
1 . 2 . 3 If , go to step 2. The value stays 1 for ever:, every time
n=n×121n<10?31✔
Special case

Dividing by 0

An algorithm cannot divide by 0: it stops with a mistake. A letter that has no number yet cannot be used either.
55÷0✘
Example
Share 12 sweets among friends: . For it works; for the algorithm stops with a mistake:
412÷43✔012÷0?✘
Rule

A procedure for a system: elimination

Elimination is a procedure: numbered steps with two decisions. 1 Write both equations as ax + by = c. 2 Decision: are the left sides multiples? 3 Choose the letter to eliminate. 4 Multiply so that the letter has the same size. 5 Decision: same signs, subtract; opposite, add. 6 Solve what is left. 7 Put the value in an equation. 8 Check in both original equations. If both letters disappear, 0 = 5 means no solution and 0 = 0 means infinitely many. In A system you type your own system.
Example
x + y = 10 and x − y = 2: the y terms have opposite signs, so add: 2x = 12 and x = 6. Then y = 10 − 6 = 4. Check: 6 + 4 = 10 and 6 − 4 = 2.
246810−2246810xy(6,4)
Rule

A procedure for a quadratic: factorise and use the zero product rule

1 Bring everything to one side: ax² + bx + c = 0. 2 Decision: is there a common factor? Take it out. 3 Decision: is c = 0? Then take out x. 4 Find two numbers with product ac and sum b. 5 Decision: is there such a pair? If not, use the formula with D = b² − 4ac. 6 Split the middle term and factorise. 7 Zero product rule: one bracket is 0. 8 Check both solutions. In A quadratic you type your own quadratic.
Example
: ac = 12 and b = 7, the pair is 3 and 4 (3 × 4 = 12 and 3 + 4 = 7). Then gives and , so x = −2/3 or x = −1/2.

Exercises

Follow the algorithm with : Input ; ; Output . Easy
4×2?+1?
Show solution
Input :
4×28+1?
Output : the output is
4×28+19
Answer →
Check: and .
Follow “Even or odd?” with . Medium
n7n mod 2=0?2k2k+1✔✘
Show solution
Is ? , so No
No: go on to the next step
n7n mod 2=0?2k2k+1✔✘
Output “odd”
Answer → odd
Check: , not .
Add up 1 to with . Make the trace table. Medium
i12345s0????
Show solution
Row 1: and
Row 2: and ; row 3: and ; row 4: and
i12345s0136?
Row 5: and : , so stop
i12345s013610
Answer →
Check: .
You have 20 euros and save 8 euros every week. The goal is 100 euros. How many weeks? Hard
2060100
Show solution
Start: and
Every week and
After 10 weeks : the question “?” says Yes, stop
202836…100+8+8
Answer → 10 weeks
Check: .
How many guesses are needed at most for the numbers 1 to 100, when you always guess the middle? Hard
050100
Show solution
After every guess at most half of the numbers is left:
100502512…1÷2÷2÷2
The 7th guess finds the number
Answer → 7 guesses
Check: is at least 100, but is not.
Are you a teacher?

Start live lessons with these applications for your whole class. Free to try for one month.

Try it free

Fits these school years

IB (MYP, Diploma): PYP, MYP 1, MYP 2, MYP 3, MYP 4, MYP 5

Cambridge International: Primary, stage 5, Primary, stage 6, Lower Secondary, stage 7, Lower Secondary, stage 8, Lower Secondary, stage 9, IGCSE

Netherlands: Groep 7, Groep 8, Year 2, Year 1, Year 3 (havo, vwo), Havo · Mathematics A, Havo · Mathematics B, Vwo · Mathematics A, Vwo · Mathematics B, Vwo · Mathematics C

Türkiye (MEB): Year 5 (MEB), Year 6 (MEB), Year 7 (MEB), Year 8 (MEB), Year 9 (MEB), Year 10 (MEB)

England (GCSE, A Level): Year 5, Year 6, Year 7, Year 8, Year 9, Year 10 (GCSE), Year 11 (GCSE)

USA (Common Core): Grade 5, Grade 6, Grade 7, Grade 8, Algebra 1

What the applications of this topic teach 6 applications

Follow an algorithm

  • Following an algorithm step by step
  • Reading a flowchart and a step list
  • Knowing what a Yes or No question does in an algorithm

Loops and trace tables

  • Following a loop round by round
  • Filling in a trace table
  • Saying how many times a loop goes round

Write your own algorithm

  • Writing an algorithm as a list of steps
  • Drawing the steps as a flowchart
  • Running and testing an own algorithm

Linear function problems as algorithms

  • Explaining the steps of a linear function problem: input x, output y
  • Putting the steps in the right order to make one algorithm
  • Showing one algorithm as words, step list and flowchart

Algorithms in everyday life

  • Seeing algorithms in a cash machine, a game and a shop
  • Following an algorithm with a decision and a loop
  • Knowing why guessing in the middle is fast
  • Improving an algorithm and counting the steps before and after

Solving procedures: a system and a quadratic step by step

  • Following a procedure for a system of two equations
  • Following the steps for a quadratic equation
  • Saying what 0 = 5 and 0 = 0 tell about a system