Lesson 103 — Introducing Algorithms: Sequences of Steps and Decisions

Strand: Space | Descriptor: AC9M7SP04 | Duration: 45 minutes

Learning Intentions

  • To understand an algorithm as a precise sequence of steps and decisions.
  • To follow, write and trace simple algorithms.

Success Criteria

I can:

  1. Define an algorithm and give everyday examples.
  2. Follow an algorithm exactly, without adding my own assumptions.
  3. Recognise a decision step and its two branches.
  4. Write a short algorithm in numbered steps.

Warmup

(6 minutes — the sandwich disaster, whole class)

The teacher follows student instructions literally to make a jam sandwich (mimed or with real props).

Ask for instructions. Follow each one exactly as stated — “put jam on bread” gets the jar placed on the loaf; “open the bread” gets the bag torn open.

  1. Why did it go wrong?
  2. What was missing from the instructions?
  3. What would it take to make the instructions foolproof?

Answers: 1. Steps assumed knowledge the follower did not have; 2. Precision, order, and unstated sub-steps; 3. Every action broken down, in order, with nothing assumed.

The definition, earned: an algorithm is a precise, ordered sequence of steps that anyone (or any machine) can follow to complete a task, with no guessing required.

Activities

Activity 1 — Explicit Instruction: Steps, Decisions and Tracing (14 min)

Two kinds of step:

TypeWhat it doesExample
InstructionDo something”Measure the longest side.”
DecisionAsk a yes/no question, then branch”Are all three sides equal? If yes → …; if no → …”

Writing conventions for this course:

  • Number every step.
  • Decisions are written as questions with both branches given.
  • The algorithm must end — every path reaches a stop.

I do — a non-geometric algorithm (getting to school):

  1. Is it raining?
    • Yes → take an umbrella. Go to step 2.
    • No → go to step 2.
  2. Is the time before 8:15?
    • Yes → walk. Stop.
    • No → catch the bus. Stop.

Trace it aloud for two cases: raining at 8:00 (umbrella, walk) and dry at 8:30 (no umbrella, bus). Tracing means following the algorithm step by step for a specific input and recording what happens.

I do — a number algorithm:

  1. Write down your number.
  2. Is it even?
    • Yes → halve it. Go to step 3.
    • No → triple it and add . Go to step 3.
  3. Is the result ?
    • Yes → stop.
    • No → go back to step 2.

Trace with input : . Stop after decisions.

Trace with input : .

(This is the Collatz algorithm. Whether it always stops is a famous unsolved problem — worth one sentence: some algorithms are easy to write and hard to understand.)

We do — trace together:

  1. Start with a number.
  2. Is it greater than ?
    • Yes → subtract . Go back to step 2.
    • No → go to step 3.
  3. Write down the result. Stop.

Trace with , and . (Answers: ; stays — careful, “greater than” is strict; stays .)

The case is the teaching moment: an algorithm does exactly what it says. “Greater than” excludes equality — a follower must not improvise.

Activity 2 — Following and Tracing (14 min)

Pairs. Follow exactly; no improvising.

Algorithm A — the sorter.

  1. Take the first number in your list.
  2. Is it odd?
    • Yes → put it in pile A. Go to step 3.
    • No → put it in pile B. Go to step 3.
  3. Are there numbers left?
    • Yes → take the next number. Go back to step 2.
    • No → stop.
  1. Trace with the list . What is in each pile?
  2. Describe in one sentence what this algorithm does.

Algorithm B — the guessing game.

  1. Think of the range to . Guess the middle number.
  2. Is the guess correct?
    • Yes → stop.
    • No → go to step 3.
  3. Is the guess too high?
    • Yes → the new range is from the bottom to just below the guess. Go to step 1.
    • No → the new range is from just above the guess to the top. Go to step 1.
  1. Trace for the secret number . List each guess.
  2. What is the greatest number of guesses this could ever need for ?

Algorithm C — spot the flaw. Each of these has a problem. Find it.

C1: 1. Add . 2. Go back to step 1.

C2: 1. Is the number big? Yes → stop. No → add , go to step 1.

C3: 1. Divide by the number of sides. 2. Stop.

Socratic scaffolding for Algorithm C:

PromptPurpose
For C1, trace it. When does it stop?Never — an infinite loop; no exit condition.
For C2, what is wrong with “big”?Undefined. One follower’s “big” is another’s “small” — decisions must be testable.
For C3, what is missing?An input. Divide what? Algorithms need their starting materials specified.
So what are the three requirements?Precision, a definite ending, and defined inputs.

(Answers: 1. Pile A: ; Pile B: ; 2. it separates odd from even numbers; 3. guesses (too low), (too high), (too low), (too low), (too low), ✓ — six guesses; 4. seven, since halving seven times passes (e.g. the secret number needs all seven).)

Activity 3 — Inquiry: Write Your Own (9 min)

Pairs, then swap and trace.

Write an algorithm, in numbered steps with at least one decision, for one of:

  1. Deciding whether a number is a multiple of .
  2. Finding the largest of three numbers.
  3. Deciding whether a whole number between and is prime (hard — use Lesson 4’s divisibility ideas).

Then swap with another pair and trace their algorithm on three inputs of your choosing, including a deliberately awkward one.

Sample solution for Q2 (teacher reference):

  1. Call the numbers , , .
  2. Is ? Yes → let . No → let .
  3. Is ? Yes → the largest is . No → the largest is .
  4. Stop.

Awkward inputs to encourage: equal numbers, zero, negatives. A good algorithm survives them or explicitly excludes them.

Checks for Understanding

(5 minutes — exit ticket)

  1. Define “algorithm” in one sentence.
  2. What is the difference between an instruction step and a decision step?
  3. Trace: start with ; while the number is odd, add ; stop when even. What is the result?
  4. Name two things an algorithm must have to be usable.
  5. Reasoning. Why is “if the number is large, stop” a badly written decision?

Answers: 1. A precise ordered sequence of steps for completing a task, requiring no guessing; 2. An instruction does something; a decision asks a testable question and branches; 3. , which is even — stop, result ; 4. Any two of: precision, a definite ending, defined inputs, testable decisions; 5. “Large” is not testable — different followers would decide differently, so the algorithm is not reproducible.

Common Misconceptions

MisconceptionHow to pre-empt it
Filling gaps with common sense when following.The sandwich disaster; “follow exactly” is enforced all lesson.
Vague decision questions.Algorithm C2 and exit Q5 make testability a requirement.
Algorithms that never stop.C1’s infinite loop; every algorithm must reach a stop.
Treating “greater than” as “greater than or equal”.The trace in Activity 1.
Believing an algorithm is just a list of instructions.Decisions and branching are what make it powerful.
Skipping the trace and assuming it works.Every written algorithm is traced by another pair.

Enrichment — Competition-Style Problems

E1 (Kangaroo style). Trace: start at ; double it; is it over ? If not, double again. How many doublings until you stop?

Answer

: five doublings, stopping when .

E2 (AMC Junior style). An algorithm: “Take a two-digit number; add its digits; if the result is two digits, repeat.” Trace , and find which starting values stop after exactly one step.

Answer

. Stops after one step when the digit sum is a single digit — i.e. digit sum , e.g. .

E3 (Challenge). Using the halving guessing algorithm on , what is the greatest number of guesses needed?

Answer

Each guess halves the range: — ten guesses.

E4 (Challenge). Write an algorithm to find the highest common factor of two numbers using repeated subtraction, then trace it on and .

Answer
  1. Are the numbers equal? Yes → that is the HCF, stop. No → go to 2.
  2. Subtract the smaller from the larger, replacing the larger. Go to 1.

Trace: → HCF (Euclid’s algorithm — over two thousand years old.)

Homework

  1. Define “algorithm” and give two everyday examples that are not from mathematics.

  2. Trace this algorithm for inputs , and :

    1. Is the number odd? Yes → add . No → halve it.
    2. Is the result less than ? Yes → stop. No → go to step 1.
  3. What is wrong with each: (a) “1. Keep going until you’re done.” (b) “1. If the shape looks nice, keep it.” (c) “1. Multiply. 2. Stop.”

  4. Write an algorithm with at least one decision for: deciding whether a number is a multiple of .

  5. Write an algorithm for finding the smallest of four numbers.

  6. Trace Algorithm B (the guessing game) for the secret number in the range . List the guesses.

  7. Reasoning. Explain why an algorithm must always end.

  8. Reasoning. Why must decision questions be answerable with a definite yes or no?

  9. Challenge. Write an algorithm to decide whether a whole number between and is prime, using divisibility tests only up to its square root (Lesson 4). Trace it on and on .

Answers: Q2 — , stop (result ); , stop; , stop. Q3 — (a) no testable stopping condition (b) “looks nice” is not testable (c) multiply what by what? Inputs undefined. Q6 — (too high), (too low), (too high), (too high), ✓ — five guesses. Q7 — an algorithm that never ends produces no answer, so it cannot complete its task. Q8 — otherwise different followers branch differently and the algorithm is not reproducible. Q9 — test divisibility by (since , also test ); : none divide → prime; : divisible by → not prime.