- Learnt28 min · floor, mod, and the coin ladder
- Basic24 min · count the change out loud
- Challenge 1–327 min · a 5c coin, words, exact change only
- Extrabonus · the fewest coins
Review — where we got to 5 min
((twenties) * (20))turns a count of coins into money.- A slot can hold another operator block. That is nesting.
((money) - (price))is the change.
Quick-fire
You put in two 50c coins and buy a 30c apple. What is your change?
Reveal the answer
70c. And here is today’s problem in one line: there is no 70c coin. The machine has to work out which coins add up to 70.
Today’s Topic 3 min
- Why
(() / ())is not enough on its own ([floor v] of ())— throw away everything after the dot(() mod ())— the remainder, the bit left over- The coin ladder: biggest coin first, all the way down
Learning outcome
By the end of this lesson you will be able to:
- Use floor to turn a decimal into a whole number of coins.
- Use mod to find what is left after the big coins are taken out.
- Chain the pair to break any amount into real coins.
Learnt — which coins, and how many 28 min
1 · “130c” is not change
Your machine says Change 130c and stops. A real machine has to drop coins into the tray, and it only has 50c, 20c and 10c coins to drop.
So it needs to answer two questions, over and over:
- How many of this coin fit?
- What is left when I take those out?
2 · Divide, and the problem with divide
“How many 50c coins fit in 130c?” looks like a divide. Try it — Scratch says 2.6.
say ((change) / (50))
The answer we want is 2, and we want it whatever the decimal is — 2.6, 2.1 and 2.99 all mean “two coins fit”.
3 · Floor always goes down
Operators has one block with a dropdown full of maths. Pick floor from it:
([floor v] of (2.6))
- Drag
([abs v] of ())out of Operators. - Click its dropdown and choose floor.
- Drop
((change) / (50))into its slot. - Put the lot into
set [50s v] to ().
4 · Mod is what is left over
Two 50c coins is 100c. The machine still owes 30c. You could work that out with a multiply and a subtract — but there is one block that does it:
((130) mod (50))
You have met remainders before, in division at school. 13 mod 5 is 3, because 5 goes into 13 twice with 3 left over.
How to use this: Click the green flag, then click the stage once. Type 3, then 2, and read the three boxes.
5 · The ladder
Now do it again for the 20c coins, using the leftover instead of the change. Then again for the 10c coins. Same two blocks, three times.
set [left v] to (change)
set [50s v] to ([floor v] of ((left) / (50)))
set [left v] to ((left) mod (50))
set [20s v] to ([floor v] of ((left) / (20)))
set [left v] to ((left) mod (20))
set [10s v] to ([floor v] of ((left) / (10)))
How to use this: Click the green flag, then click the stage once. Type 3, then 2, and watch the three coin boxes fill in turn.
Basic — count it out loud 24 min
Open your machine from last lesson. You are adding the ladder to it.
Step 1 — the boxes
- Make four variables:
left,50s,20sand10s. - Tick the last three so they show on the Stage.
Step 2 — the first rung
- After the change is worked out, add
set [left v] to (change). - Add
set [50s v] to ([floor v] of ((left) / (50))). - Add
set [left v] to ((left) mod (50)). - Press the flag and check the 50s box by hand.
Step 3 — the other two rungs
- Right-click the two blocks you just made and duplicate them.
- Change both 50s to 20s in the copy.
- Duplicate again for the 10c coins.
Step 4 — say each one
- After each rung, add
say (join (50s) [ x 50c]) for (1.5) seconds. - Do the same for the 20c and 10c rungs.
Step 5 — count the coins
- Make a variable called
coinsand set it to 0 at the top. - After each rung, add
change [coins v] by ()with that rung’s count in it. - At the end, say how many coins came out.
set [left v] to (change)
set [50s v] to ([floor v] of ((left) / (50)))
say (join (50s) [ x 50c]) for (1.5) seconds
change [coins v] by (50s)
set [left v] to ((left) mod (50))
set [20s v] to ([floor v] of ((left) / (20)))
say (join (20s) [ x 20c]) for (1.5) seconds
change [coins v] by (20s)
set [left v] to ((left) mod (20))
set [10s v] to ([floor v] of ((left) / (10)))
say (join (10s) [ x 10c]) for (1.5) seconds
change [coins v] by (10s)
set [left v] to ((left) mod (10))
How to use this: Click the green flag, then click the stage once. Type 3, then 2, and listen to it count.
Every box shows a long decimal.
The floor block is missing, or its dropdown still says abs. Click the dropdown and choose floor.
The machine hands back far too many coins.
A rung is dividing change instead of left. Every rung after the first must work on what the rung above it left behind.
The 10c box is always 0 and there is money left over.
A set [left v] to ((left) mod ()) is missing between two rungs, so the leftover never shrank.
The coins box keeps growing every time I press the flag.
set [coins v] to (0) is missing at the top. A change block adds to whatever was already there.
The 5c coin
Buy the 45c muffin with 160c and the change is 115c. Run your ladder on it and 5c gets stuck at the bottom. Add a rung.
- A fourth rung for 5c coins, below the 10c one.
- After it, the leftover is 0.
- The other three rungs are not changed.
How to use this: Click the green flag, then click the stage once and type 3, then 2.
Teacher note
Let them find the stranded 5c themselves before mentioning the new rung — the leftover box showing 5 at the end is the whole lesson in one number. Pupils who add the 5c rung above the 10c one will hand back 23 coins; that is worth letting happen once.
Say it in one sentence
Four boxes on the Stage is not how a shopkeeper counts change. Say it instead.
- One bubble, listing every coin size and how many.
- The numbers come from the variables, not typed in.
- It is right for any amount of change.
How to use this: Click the green flag, then click the stage once and type 3, then 2.
Teacher reveal — joins inside joins
say (join (50s) (join [ x 50c, ] (join (20s) (join [ x 20c and ] (join (10s) [ x 10c.]))))) for (4) seconds
A join takes exactly two things, so six pieces means five joins nested inside each other — the same nesting as Lesson 3-1, on words instead of numbers. Build it left to right and drag each new join into the last empty slot.
Exact change only
Price the donut at 43c. Now the change is 117c — and this machine has no 1c coin, so it cannot pay you properly. It should say so before it takes your money.
- Before serving, the machine checks whether it can pay the change exactly.
- If it cannot, it says how much it is unable to pay and returns the coins.
- If it can, it serves as normal.
How to use this: Click the green flag, then click the stage once and type 3, then 2. Try a price of 45 in the code to see it serve instead.
Teacher reveal — one mod tells you
set [stuck v] to ((change) mod (5))
if <(stuck) > (0)> then
say (join [I cannot pay ] (join (stuck) [c. Coins returned.])) for (3) seconds
else
glide (0.6) secs to x: (-60) y: (-128)
end
The smallest coin is 5c, so any change that is not a multiple of 5 cannot be paid. ((change) mod (5)) is 0 exactly when it can. This is mod used as a test rather than as a step in a sum, and it is the same trick that answers “is this number even?” — ((n) mod (2)).
The fewest coins
Your ladder starts at the biggest coin. Prove that this matters: pay the same change a second way, all in 10c coins, and count both.
- A
biggest firstbox counts the coins your ladder hands out. - A
tens onlybox counts the same change paid entirely in 10c coins. - The machine says both numbers side by side.
How to use this: Click the green flag, then click the stage once and type 3, then 2.
Teacher reveal
set [biggest first v] to ([floor v] of ((left) / (50)))
set [left v] to ((left) mod (50))
change [biggest first v] by ([floor v] of ((left) / (20)))
set [left v] to ((left) mod (20))
change [biggest first v] by ([floor v] of ((left) / (10)))
set [tens only v] to ([floor v] of ((change) / (10)))
The comparison is the point, not the code. Ask the class why a shop cares: fewer coins is less to count, less to carry and less to run out of. Real change machines are built on exactly this rule, and it has a name — a greedy algorithm, which takes the biggest bite it can at every step.
Summary 5 min
(() / ())gives a decimal, and coins do not come in decimals.([floor v] of ())throws away everything after the dot — it always goes down.(() mod ())reports the remainder: what is left after the whole ones are taken out.- The pair together break any amount into coins, biggest first.
- Mod is also a test:
((change) mod (5))is 0 exactly when the change can be paid in 5c coins.
- Floor
- The whole number below. Floor of 2.9 is 2.
- Mod
- The remainder after dividing. 13 mod 5 is 3.
- Remainder
- What is left over when one number will not divide into another exactly.
- Greedy
- Taking the biggest piece you can at every step — how change machines work.
Hand in
Save your machine. Next lesson it stops being the same every morning: the prices, the stock and the offers all start rolling a dice.