Skip to content
The Lumenary
Grades 3–5Ages 8–11 35 min

Expressions, unknowns and rules

Why 2+3×42 + 3 \times 4 is 14, how to find the number hiding in 3×□=243 \times \square = 24, and how a rule like "double and add 1" can be written in one short line.

Formally verified · 18 LeanComputationally tested · 2 PythonAI-drafted · awaiting human reviewMathematically reviewed: Awaiting human review

Your guides: Chloe, Margaret, Gideon

MotivationH

Two calculators, two answers

Kojo and Ama are shopping at the market in Kumasi, in Ghana. They buy a bag for 2 cedis and 3 bottles of water at 4 cedis each. Kojo types into his small calculator:

2+3×4=2 + 3 \times 4 =

His calculator says 20. Ama types exactly the same keys into the calculator on her phone. It says 14.

Which one is right? The shopkeeper knows: the bag costs 2 cedis, and the three bottles cost 3×4=123 \times 4 = 12 cedis, so the total is 2+12=142 + 12 = 14 cedis. Kojo's calculator worked from left to right as he typed, 2+3=52 + 3 = 5, then 5×4=205 \times 4 = 20 — as if he had bought 5 bottles!

Mathematicians all over the world have agreed on a rule for reading an expression like 2+3×42 + 3 \times 4, so that everybody gets the same answer. In this lesson you will learn that rule, and then use it to find numbers that are hiding.

IntuitionH

Names for numbers

An expression like 3×4+23 \times 4 + 2 is a name for a number. It names 14. The number 14 has many other names: 10+410 + 4, 2×72 \times 7, 20−620 - 6, (3+4)×2(3 + 4) \times 2.

Brackets say "work this out first". In (2+3)×4(2 + 3) \times 4, we add first: 5×4=205 \times 4 = 20. In 2+(3×4)2 + (3 \times 4), we multiply first: 2+12=142 + 12 = 14.

What if there are no brackets? Think about how we talk. "Three bags of 4 oranges, and 2 more oranges" is 3×4+23 \times 4 + 2. It would be a nuisance to write brackets every time: (3×4)+2(3 \times 4) + 2. So everybody agrees: multiplication and division are done before addition and subtraction, unless brackets say otherwise. Then 3×4+23 \times 4 + 2 and 2+3×42 + 3 \times 4 both mean "some groups, plus some more" — just what we usually want.

Sometimes a number is hidden, and we only know something about it:

3×□=24.3 \times \square = 24.

"Three times some number makes 24." Which number is in the box? You may know straight away: 8. In this lesson you will learn a way that works even when the numbers are big — by undoing what was done to the hidden number.

DefinitionsH

Big ideas

Definition— expression

An expression is a combination of numbers and operations (++, −-, ×\times, ÷\div), possibly with brackets, that names a number.

Definition— order of operations

To work out an expression:

  1. work out anything in brackets first;
  2. then do multiplication and division, from left to right;
  3. then do addition and subtraction, from left to right.

Definition— equation

An equation says that two expressions have the same value. The equals sign "=" means "has the same value as". So 3+4=73 + 4 = 7, 7=3+47 = 3 + 4 and 3+4=6+13 + 4 = 6 + 1 are all true equations.

Definition— unknown

An unknown is a number we are asked to find. It is shown by a box, □\square, or by a letter, such as nn. A letter in an expression always stands for a number.

Definition— inverse operations

Two operations are inverse operations when one undoes the other.

  • Adding 17 is undone by subtracting 17 (and subtracting 17 is undone by adding 17).
  • Multiplying by 3 is undone by dividing by 3 (and dividing by 3 is undone by multiplying by 3).

Multiplying by any number except 0 can be undone in this way.

Definition— rule

An input–output rule tells you what to do to any input number to get the output. "Double, then add 1" is a rule. Written as an expression, with the letter nn standing for the input, it is 2×n+12 \times n + 1.

Worked examplesH

Let's try together

Example— Using the order of operations

  • 2+3×4=2+12=142 + 3 \times 4 = 2 + 12 = 14 (multiply first).
  • (2+3)×4=5×4=20(2 + 3) \times 4 = 5 \times 4 = 20 (brackets first).
  • 20−8÷4=20−2=1820 - 8 \div 4 = 20 - 2 = 18 (divide first).
  • 18÷3×2=6×2=1218 \div 3 \times 2 = 6 \times 2 = 12. Division and multiplication are on the same level, so we go from left to right. (Not 18÷6=318 \div 6 = 3!)
  • 10−4−3=6−3=310 - 4 - 3 = 6 - 3 = 3, also from left to right. (Not 10−1=910 - 1 = 9.)

Example— Finding an unknown by undoing

□+17=40\square + 17 = 40. Something plus 17 makes 40. To find it, undo "add 17" by subtracting 17: □=40−17=23\square = 40 - 17 = 23. Check: 23+17=4023 + 17 = 40 ✓.

3×□=243 \times \square = 24. Three times something makes 24. Undo "multiply by 3" by dividing by 3: □=24÷3=8\square = 24 \div 3 = 8. Check: 3×8=243 \times 8 = 24 ✓.

24=3×□24 = 3 \times \square. This is the same equation written the other way round — "24 has the same value as 3 times something". The answer is still 8.

□−9=15\square - 9 = 15. Undo "subtract 9" by adding 9: □=15+9=24\square = 15 + 9 = 24. Check: 24−9=1524 - 9 = 15 ✓.

36÷□=436 \div \square = 4. Careful: here the unknown is the number we divide by. 36 shared into groups of some size gives 4 groups, so the size is 36÷4=936 \div 4 = 9. Check: 36÷9=436 \div 9 = 4 ✓. Always check your answer in the original equation!

Example— Think of a number

Ama says: "I thought of a number, multiplied it by 3, then added 1. I got 22." What was her number?

Two things happened to the number, in this order: multiply by 3, then add 1. To undo them, we start from the end and work backwards, undoing the last step first:

  • undo "add 1": 22−1=2122 - 1 = 21;
  • undo "multiply by 3": 21÷3=721 \div 3 = 7.

Her number was 7. Check: 3×7+1=21+1=223 \times 7 + 1 = 21 + 1 = 22 ✓. As an equation: 3×□+1=223 \times \square + 1 = 22, so □=7\square = 7.

7× 214+ 115forwards15− 114÷ 27backwards
The rule “double, then add 1” takes 7 to 15. To go back from 15 to 7, undo the steps in reverse order: first take away 1, then halve.

Example— Rules and tables

The rule "double, then add 1" is 2×n+12 \times n + 1. To complete a table, put each input in place of nn:

input nn123410
output 2×n+12 \times n + 1357921

Going backwards: which input gives the output 15? Undo the steps in reverse order: 15−1=1415 - 1 = 14, then 14÷2=714 \div 2 = 7. The input is 7.

Now a table without its rule:

input nn1234
output471013

Each time the input goes up by 1, the output goes up by 3. That suggests "multiply by 3": 3×1=33 \times 1 = 3, 3×2=63 \times 2 = 6, … — always 1 less than the output. So the rule is "multiply by 3, then add 1", or 3×n+13 \times n + 1. Check: 3×4+1=133 \times 4 + 1 = 13 ✓. Now we can predict: input 10 gives 3×10+1=313 \times 10 + 1 = 31.

Example— Letters are numbers

At a fruit stall, one apple costs aa cedis and one banana costs bb cedis. Then 4 apples and 3 bananas cost

4×a+3×b cedis.4 \times a + 3 \times b \text{ cedis.}

Here aa is not "an apple": it is the number of cedis one apple costs. If a=5a = 5 and b=2b = 2, the cost is 4×5+3×2=20+6=264 \times 5 + 3 \times 2 = 20 + 6 = 26 cedis.

ProofH

Why undoing finds the unknown — and why it is the only answer

Theorem— Finding an unknown by subtracting

The only number that makes □+17=40\square + 17 = 40 true is 2323.

Proof

The proof has two parts.

If a number works, it must be 23. Suppose some number makes □+17=40\square + 17 = 40 true. Then □+17\square + 17 and 4040 are the same number. Take 17 away from this same number, and you get the same result either way:

(□+17)−17=40−17.(\square + 17) - 17 = 40 - 17.

On the left, adding 17 and then taking 17 away gets you back where you started, so the left side is just □\square. On the right, 40−17=2340 - 17 = 23. So □=23\square = 23.

And 23 does work: 23+17=4023 + 17 = 40.

∎

The same reasoning shows that 3×□=243 \times \square = 24 has exactly one answer: if 3×□=243 \times \square = 24, divide both sides by 3. Dividing by 3 undoes multiplying by 3, so □=24÷3=8\square = 24 \div 3 = 8; and 3×8=243 \times 8 = 24.

When there is no answer. Not every equation has a whole-number answer.

  • 3×□=253 \times \square = 25: we know 3×8=243 \times 8 = 24 (too small) and 3×9=273 \times 9 = 27 (too big), and the bigger the number in the box, the bigger 3×□3 \times \square gets. So no whole number works. (A fraction does: 25÷3=81325 \div 3 = 8\tfrac{1}{3}. You can meet it in Fractions and decimals.)
  • 0×□=50 \times \square = 5: zero times any number is 0, never 5. No number works at all.
  • 0×□=00 \times \square = 0: this is true for every number, so it does not tell us what is in the box.

That is why "multiplying is undone by dividing" needs the number to be non-zero. Multiplying by 0 sends 3 to 0 and 7 to 0 — and once you only see the 0, nothing can tell you whether it came from 3 or from 7. It cannot be undone.

Explore

Explore: balance the scale

An equation is like a balance: the two sides weigh the same. Each box on the scale holds the same unknown number of weights. Do the same thing to both sides — take 1 away from both, or divide both into equal shares — and the scale stays level. When one box is left alone, you have found the unknown.

□ + 7 = 19

Take 1 from both sides until the box is alone. How many times must you do it?

x11111111111111111111111111
  • x (unknown weight)
  • 1 (unit weight)

x + 7 = 19

Do the same thing to both sides. Try to get x on its own.

The scale is still level: both sides changed by the same amount.

Your steps

  1. x + 7 = 19
Make your own equation

Left side

Number of x's1
Number of 1s7

Right side

Number of x's0
Number of 1s19

3 × □ + 2 = 20

Two steps: what should you take away first? Then divide both sides by 3.

xxx1111111111111111111111
  • x (unknown weight)
  • 1 (unit weight)

3x + 2 = 20

Do the same thing to both sides. Try to get x on its own.

The scale is still level: both sides changed by the same amount.

Your steps

  1. 3x + 2 = 20
Make your own equation

Left side

Number of x's3
Number of 1s2

Right side

Number of x's0
Number of 1s20

What to notice:

  • In the second scale, try dividing by 3 first. The scale refuses: 20 weights cannot be split into 3 equal shares. Undoing the "+ 2" first makes the division work — the last step is undone first.
  • The record beside the scale is a list of equations, each one true. That list is your working.

Rules make growing patterns, too. Here each step nn has 2×n+12 \times n + 1 tiles. Predict the number of tiles in step 10 before you reveal the rule.

Double, then add 1

How many tiles in step 10? Which step has 25 tiles?

Step 1
Step 2
Step 3
Step 4
Step123410
Tiles3579?
Change+2+2+2
Formal mathematicsF

Gideon's true-or-false game

Gideon only plays with statements that are exactly true or exactly false. Decide, and give a reason.

  1. "2+3×4=202 + 3 \times 4 = 20." — False. Multiplication comes first: 2+12=142 + 12 = 14.
  2. "(2+3)×4=20(2 + 3) \times 4 = 20." — True. The brackets are worked out first.
  3. "The only number that makes □+17=40\square + 17 = 40 true is 23." — True. Subtracting 17 from both sides shows it must be 23, and 23+17=4023 + 17 = 40.
  4. "3×□=253 \times \square = 25 has a whole-number answer." — False. 3×8=243 \times 8 = 24 and 3×9=273 \times 9 = 27; 25 is in between.
  5. "24=3×□24 = 3 \times \square is not a proper equation, because the answer must come after the equals sign." — False! "=" means "has the same value as". The equation is fine, and □=8\square = 8.
For grown-ups · the same ideas in Lean, a proof checker

Lean reads expressions with the same order of operations as we do:

order_of_operationsLean 4Proved here from basic factsFormally verified
/-- Without brackets, multiplication comes before addition: `2 + 3 * 4` is `2 + 12 = 14`.
Brackets change the grouping: `(2 + 3) * 4` is `5 * 4 = 20`. Lean follows the same convention
as the lesson. -/
theorem order_of_operations : 2 + 3 * 4 = 14 ∧ (2 + 3) * 4 = 20 := by
  decide
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
left_to_rightLean 4Proved here from basic factsFormally verified
/-- Operations of the same kind are done from left to right: `18 / 3 * 2` is `6 * 2 = 12`
(not `18 / 6 = 3`), and `10 - 4 - 3` is `6 - 3 = 3` (not `10 - 1 = 9`). And `20 - 8 / 4` is
`20 - 2 = 18`, because division comes before subtraction. -/
theorem left_to_right : 18 / 3 * 2 = 12 ∧ 10 - 4 - 3 = 3 ∧ 20 - 8 / 4 = 18 := by
  decide
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor

Inverse operations, for every number. Lean's whole numbers (Nat) stop at 0 when you subtract and round down when you divide, so these two facts are exactly what "undoes" means here. Multiplying by 0 cannot be undone:

sub_undoes_addLean 4Applies a library theoremFormally verified
/-- Subtracting `b` undoes adding `b`: whatever number `n` you start with, add `b` and then take
`b` away, and you are back at `n`. This is core Lean's `Nat.add_sub_cancel`. -/
theorem sub_undoes_add (n b : Nat) : n + b - b = n :=
  Nat.add_sub_cancel n b
Axioms used: propextformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
div_undoes_mulLean 4Applies a library theoremFormally verified
/-- Dividing by `a` undoes multiplying by `a`, as long as `a` is not 0. This is core Lean's
`Nat.mul_div_cancel_left`. -/
theorem div_undoes_mul (n a : Nat) (ha : 0 < a) : a * n / a = n :=
  Nat.mul_div_cancel_left n ha
Axioms used: propextformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
zero_cannot_be_undoneLean 4Proved here from basic factsFormally verified
/-- Multiplying by 0 **cannot** be undone: it sends 3 and 7 (and every other number) to the same
answer, 0, so nothing can tell us afterwards which number we started with. -/
theorem zero_cannot_be_undone : 0 * 3 = 0 * 7 ∧ 3 ≠ 7 := by
  decide
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor

Solving equations. The symbol ↔ ("exactly when") packs both parts of the human proof into one statement: if the equation holds, the unknown must be 23; and if the unknown is 23, the equation holds. The calc steps follow the human reasoning line by line:

add_unknownLean 4Proved here from basic factsFormally verified
/-- **□ + 17 = 40 exactly when □ = 23.** If □ + 17 = 40, then taking 17 away from both sides
gives □ = 40 − 17 = 23 — so 23 is the only possible answer. And 23 really works. -/
theorem add_unknown (x : Nat) : x + 17 = 40 ↔ x = 23 := by
  constructor
  · intro h
    calc x = x + 17 - 17 := (Nat.add_sub_cancel x 17).symm
      _ = 40 - 17 := by rw [h]
      _ = 23 := by decide
  · intro h
    rw [h]
Axioms used: propextformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
add_solutionLean 4Proved here from basic factsFormally verified
/-- The same reasoning for **any** numbers: when `b ≤ c`, the equation □ + b = c has exactly one
whole-number answer, □ = c − b. -/
theorem add_solution (b c x : Nat) (h : b ≤ c) : x + b = c ↔ x = c - b := by
  omega
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
mul_unknownLean 4Proved here from basic factsFormally verified
/-- **3 × □ = 24 exactly when □ = 8.** If 3 × □ = 24, dividing both sides by 3 gives
□ = 24 ÷ 3 = 8. And 8 really works. -/
theorem mul_unknown (x : Nat) : 3 * x = 24 ↔ x = 8 := by
  constructor
  · intro h
    calc x = 3 * x / 3 := (Nat.mul_div_cancel_left x (by decide)).symm
      _ = 24 / 3 := by rw [h]
      _ = 8 := by decide
  · intro h
    rw [h]
Axioms used: propextformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
unknown_on_leftLean 4Proved here from basic factsFormally verified
/-- The equals sign means "has the same value as", so an equation can be written either way
round: 24 = 3 × □ is the same equation as 3 × □ = 24, with the same answer. -/
theorem unknown_on_left (x : Nat) : 24 = 3 * x ↔ x = 8 := by
  constructor
  · intro h
    exact (mul_unknown x).mp h.symm
  · intro h
    exact ((mul_unknown x).mpr h).symm
Axioms used: propextformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
think_of_a_numberLean 4Proved here from basic factsFormally verified
/-- **Two steps.** "Multiply by 3, then add 1" gives 22 exactly when the number was 7. To find
it, undo the steps in reverse order: take away 1, then divide by 3. -/
theorem think_of_a_number (x : Nat) : 3 * x + 1 = 22 ↔ x = 7 := by
  constructor
  · intro h
    calc x = (3 * x + 1 - 1) / 3 := by
          rw [Nat.add_sub_cancel, Nat.mul_div_cancel_left x (by decide)]
      _ = (22 - 1) / 3 := by rw [h]
      _ = 7 := by decide
  · intro h
    rw [h]
Axioms used: propextformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor

Equations without whole-number answers:

no_whole_answerLean 4Proved here from basic factsFormally verified
/-- Some equations have **no** whole-number answer: 3 × □ = 25 has none, because 25 is not a
multiple of 3 (`3 × 8 = 24` is too small and `3 × 9 = 27` too big). The answer is the fraction
25/3, which lives outside the whole numbers. -/
theorem no_whole_answer : ¬ ∃ x : Nat, 3 * x = 25 := by
  omega
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
zero_times_unknownLean 4Proved here from basic factsFormally verified
/-- Why "multiplying by a **non-zero** number" matters: 0 × □ = 5 has no answer at all, and
0 × □ = 0 is true for every number, so it does not pin down □. -/
theorem zero_times_unknown : (∀ x : Nat, 0 * x ≠ 5) ∧ (∀ x : Nat, 0 * x = 0) :=
  ⟨fun x => by rw [Nat.zero_mul]; decide, fun x => Nat.zero_mul x⟩
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor

A rule is a function: give it an input, and it gives an output. Lean can fill in a table, work backwards, and prove why the rule from the table has the multiplier 3 — for every input, not just the ones in the table:

doubleAddOneLean 4Formally verified
/-- The rule "double, then add 1", written as an expression in `n`: `2 * n + 1`. -/
def doubleAddOne (n : Nat) : Nat := 2 * n + 1
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
doubleAddOne_tableLean 4Proved here from basic factsFormally verified
/-- Completing the table for the rule: the inputs 1, 2, 3 and 10 give 3, 5, 7 and 21. -/
theorem doubleAddOne_table : [1, 2, 3, 10].map doubleAddOne = [3, 5, 7, 21] := by
  decide
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
doubleAddOne_backwardsLean 4Proved here from basic factsFormally verified
/-- Working backwards: the rule gives 15 only for the input 7 (undo: take away 1, then
halve). -/
theorem doubleAddOne_backwards (n : Nat) : doubleAddOne n = 15 ↔ n = 7 := by
  unfold doubleAddOne
  omega
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
threeNPlusOneLean 4Formally verified
/-- The rule found from the table 1 → 4, 2 → 7, 3 → 10, 4 → 13: "multiply by 3, then add 1". -/
def threeNPlusOne (n : Nat) : Nat := 3 * n + 1
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
threeNPlusOne_tableLean 4Proved here from basic factsFormally verified
/-- The rule fits every row of the table. -/
theorem threeNPlusOne_table : [1, 2, 3, 4].map threeNPlusOne = [4, 7, 10, 13] := by
  decide
Axioms used: No axiomsformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor
threeNPlusOne_stepLean 4Proved here from basic factsFormally verified
/-- Why the multiplier is 3: for **every** input, going up by 1 makes the output go up by 3. -/
theorem threeNPlusOne_step (n : Nat) : threeNPlusOne (n + 1) = threeNPlusOne n + 3 := by
  unfold threeNPlusOne
  omega
Axioms used: propext, Quot.soundformal/Lumenary/ExpressionsAndUnknowns.leanTry it in the Lean web editor

What is not formalised: Lean does not prove that the order of operations is "right" — it is a convention, and Lean simply follows it. Nor does it prove that 3×n+13 \times n + 1 is the rule behind the table; it proves that this rule fits and grows by 3 at each step. A table on its own can never prove which rule produced it.

ComputationC

Margaret's calculators

Left to right, or multiplication first?PythonExact computationExhaustive finite checkComputationally tested
"""Two ways to read an expression: left to right, or by the convention.

A simple calculator works from left to right as you type, so it reads
2 + 3 x 4 as (2 + 3) x 4 = 20. The convention used in mathematics (and by
scientific calculators and programming languages) does multiplication and
division before addition and subtraction, giving 2 + 12 = 14. The program
evaluates some expressions both ways, then checks every expression
'a op b op c' with digits 1 to 9 and the four operations, and reports for
each pair of operations how often the two readings agree. Division is exact
(fractions), so nothing is rounded.
"""

import textwrap
from fractions import Fraction

OPS = {
    "+": lambda x, y: x + y,
    "-": lambda x, y: x - y,
    "x": lambda x, y: x * y,
    "/": lambda x, y: x / y,
}

def tokens(text):
    """Split '2 + 3 x 4' into numbers (as Fractions) and symbols."""
    return [Fraction(t) if t.isdigit() else t for t in text.split()]

def left_to_right(toks):
    """Work strictly from left to right, as a simple calculator does."""
    value = toks[0]
    for i in range(1, len(toks), 2):
        value = OPS[toks[i]](value, toks[i + 1])
    return value

def by_convention(toks):
    """Brackets first, then x and /, then + and -, each left to right."""
    pos = 0

    def factor():
        nonlocal pos
        if toks[pos] == "(":
            pos += 1
            value = expression()
            pos += 1  # skip ")"
            return value
        pos += 1
        return toks[pos - 1]

    def term():
        nonlocal pos
        value = factor()
        while pos < len(toks) and toks[pos] in ("x", "/"):
            op = toks[pos]
            pos += 1
            value = OPS[op](value, factor())
        return value

    def expression():
        nonlocal pos
        value = term()
        while pos < len(toks) and toks[pos] in ("+", "-"):
            op = toks[pos]
            pos += 1
            value = OPS[op](value, term())
        return value

    return expression()

    """How many of the 729 'a op1 b op2 c' read the same both ways."""
def agreement(op1, op2):
    """How many of the 729 expressions 'a op1 b op2 c' read the same both ways."""
    same = 0
    for a in range(1, 10):
        for b in range(1, 10):
            for c in range(1, 10):
                toks = [Fraction(a), op1, Fraction(b), op2, Fraction(c)]
                same += left_to_right(toks) == by_convention(toks)
    return same

def main():
    print("Expression        left to right   by the convention")
    for text in ["2 + 3 x 4", "20 - 8 / 4", "18 / 3 x 2", "10 - 4 - 3",
                 "3 x 4 + 2"]:
        toks = tokens(text)
        print(f"{text:<17} {str(left_to_right(toks)):>13}"
              f"   {str(by_convention(toks)):>17}")
    print("(2 + 3) x 4       brackets first: "
          f"{by_convention(tokens('( 2 + 3 ) x 4'))}")
    print()
    print("Every 'a op1 b op2 c' with digits 1-9: out of 729, how many")
    print("give the same answer read both ways?")
    print("op1 \\ op2 " + "".join(f"{op2:>7}" for op2 in OPS))
    for op1 in OPS:
        row = "".join(f"{agreement(op1, op2):>7}" for op2 in OPS)
        print(f"   {op1}      {row}")
    print("The readings can only differ when + or - comes before x or /.")
    print()
    print(textwrap.fill(
        "Epistemic status: exact (fractions, no rounding) and exhaustive for "
        "the 11664 expressions with three digits from 1 to 9. It shows when "
        "the convention matters; it does not show that the convention is "
        "'true'. The order of operations is an agreement, chosen so that "
        "expressions like 3 x 4 + 2 need no brackets.",
        width=78,
    ))

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

The two calculators always agree except when ++ or −- comes before ×\times or ÷\div. Even then they agree in 81 of the 729 cases — exactly when the last number is 1, because multiplying or dividing by 1 changes nothing.

Which rule made this table?PythonExact computationExhaustive finite checkComputationally tested
"""Which rule made this table? Searching input-output rules.

A table of inputs and outputs is evidence about a rule. The program tries
every rule of the form 'multiply by a, then add b' (a x n + b) with a and b
from 0 to 10 and lists those that fit the first rows of a table. One row
fits several rules; two rows pin down one rule of this kind - but a rule of
a different kind (n x n + 3) also fits those two rows, and only a third row
tells them apart. Finally it works backwards: which input gives a chosen
output, found by undoing the rule's steps in reverse order and checked by
running the rule forwards.
"""

import textwrap

TABLE = [(1, 4), (2, 7), (3, 10), (4, 13)]

def fits(rule, rows):
    """Does the rule give the right output for every row (input, output)?"""
    return all(rule(n) == out for n, out in rows)

def linear_rules_that_fit(rows, largest=10):
    """All (a, b) with 0 <= a, b <= largest such that a x n + b fits."""
    return [(a, b)
            for a in range(largest + 1)
            for b in range(largest + 1)
            if fits(lambda n, a=a, b=b: a * n + b, rows)]

def square_plus_three(n):
    """A rule of a different kind: n x n + 3."""
    return n * n + 3

def undo_three_n_plus_one(output):
    """Undo 'multiply by 3, then add 1' in reverse: take 1, then divide by 3.

    Returns None when there is no whole-number input."""
    if output < 1 or (output - 1) % 3 != 0:
        return None
    return (output - 1) // 3

def main():
    print("The table: " + ", ".join(f"{n} -> {out}" for n, out in TABLE))
    print("Rules 'a x n + b' (a, b from 0 to 10) that fit the first rows:")
    for k in range(1, len(TABLE) + 1):
        found = linear_rules_that_fit(TABLE[:k])
        rules = ", ".join(f"{a} x n + {b}" for a, b in found)
        rows = "row: " if k == 1 else "rows:"
        print(f"  {k} {rows} {rules}")
    print()
    print("A different kind of rule: n x n + 3")
    for k in (2, 3):
        print(f"  fits the first {k} rows:"
              f" {fits(square_plus_three, TABLE[:k])}")
    print("  (3 x 3 + 3 = 12, but the table says 3 -> 10.)")
    print()
    print("Working backwards from outputs of 3 x n + 1:")
    for output in (22, 31, 100, 24):
        n = undo_three_n_plus_one(output)
        if n is None:
            print(f"  output {output}: no whole-number input")
        else:
            print(f"  output {output}: input {n}"
                  f"  (check: 3 x {n} + 1 = {3 * n + 1})")
    print()
    print(textwrap.fill(
        "Epistemic status: exact and exhaustive, but only for rules of the "
        "form a x n + b with a and b from 0 to 10. A table with a few rows "
        "never proves which rule made it - other kinds of rules can fit the "
        "same rows. Knowing the rule, the backward answers are certain: each "
        "is checked by running the rule forwards.",
        width=78,
    ))

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

One row of the table fits five different rules. Two rows leave just one rule of this kind, 3×n+13 \times n + 1. But a rule of a different kind, n×n+3n \times n + 3, also fits the first two rows: 1×1+3=41 \times 1 + 3 = 4 and 2×2+3=72 \times 2 + 3 = 7! Only the third row tells them apart. A computer search like this is exact for the rules it tries, but a table with a few rows can never prove which rule made it.

Exercises

Your turn

Work out brackets first, then multiplication and division, then addition and subtraction. Always check an unknown by putting it back into the equation. You can ask for a hint whenever you like.

1. Expressions, unknowns and rules

Warm-upHuman

Work out 5+4×35 + 4 \times 3.

2. Expressions, unknowns and rules

CoreHuman

Work out (8+4)÷2×3(8 + 4) \div 2 \times 3.

3. Expressions, unknowns and rules

Warm-upHuman

Find the unknown: □+17=40\square + 17 = 40.

4. Expressions, unknowns and rules

CoreHuman

Find the unknown: 45=□×545 = \square \times 5.

5. Expressions, unknowns and rules

CoreHuman

Tendai thought of a number. He doubled it, then added 6, and got 20. What was his number?

6. Expressions, unknowns and rules

CoreHuman

Which rule fits every row of this table?

input nn 1 2 3 4
output 5 8 11 14
Choose one

7. Expressions, unknowns and rules

StretchHuman

Where should the brackets go to make this true?

2+3×4−1=192 + 3 \times 4 - 1 = 19

Choose one

8. Expressions, unknowns and rules

CoreHuman

At a stall, one pencil costs pp shillings and one rubber costs rr shillings. What does 3×p+2×r3 \times p + 2 \times r mean?

Choose one

9. Expressions, unknowns and rules

StretchFormal

In Lean, ↔ means "exactly when", ∀ x : Nat means "for every whole number xx" and ∃ x means "there is a number xx". Which statement says that 23 is the only answer to □+17=40\square + 17 = 40?

Choose one
Common misconceptionsH

Things that trip people up

Misconception

Working strictly from left to right. 2+3×42 + 3 \times 4 is not 20. Multiplication and division come before addition and subtraction, so it is 2+12=142 + 12 = 14. Only operations on the same level are done from left to right: 18÷3×2=1218 \div 3 \times 2 = 12.

Misconception

"The answer must come after the equals sign." The equals sign means "has the same value as", not "and the answer is". 24=3×□24 = 3 \times \square is a perfectly good equation, and so is 3+4=6+13 + 4 = 6 + 1.

Misconception

Writing a chain of calculations with "=". "3×4=12+2=143 \times 4 = 12 + 2 = 14" looks like working, but it claims that 3×43 \times 4 has the same value as 14, which is false. Write instead: 3×4+2=12+2=143 \times 4 + 2 = 12 + 2 = 14, where every "=" is true.

Misconception

"A letter stands for a thing." In 4×a+3×b4 \times a + 3 \times b, the letter aa does not mean "apples". It stands for a number — for example the price of one apple, or the number of apples in a bag.

Misconception

Undoing steps in the original order. To undo "multiply by 3, then add 1", subtract 1 first, then divide by 3. Undoing in the original order gives the wrong answer: for the output 22, dividing by 3 first does not even give a whole number.

ViewpointsH

Other ways of seeing

Algebra— The algebraist: one line for every number

A rule like 2×n+12 \times n + 1 is a tiny piece of algebra: one short line describes infinitely many calculations, one for each number nn. Algebra lets us reason about all of them at once. For example, 2×n+12 \times n + 1 is always odd, whatever whole number nn is, because 2×n2 \times n is even and one more than an even number is odd. Next you will write expressions with letters more freely, and solve harder equations, in variables and algebraic expressions and linear equations.

Computation— Computers follow the same agreement

Scientific calculators, spreadsheets and programming languages all use the order of operations: in Python, 2 + 3 * 4 gives 14. Lean, the proof checker in this lesson, reads 2 + 3 * 4 the same way. A computer cannot ask what you meant, so this agreement matters even more for machines than for people.

Set-theoretic connectionH

The set of answers

For an equation with an unknown, we can collect all the numbers that make it true into a set, called its solution set:

  • □+17=40\square + 17 = 40 has the solution set {23}\{23\}: exactly one answer.
  • 3×□=253 \times \square = 25 has no whole-number answer: its solution set (among whole numbers) is the empty set {}\{\}.
  • 0×□=00 \times \square = 0 is true for every whole number: its solution set is the set of all whole numbers.

"Solve the equation" means "find the solution set" — and the two parts of our proof show exactly that the solution set of □+17=40\square + 17 = 40 is {23}\{23\}: nothing else is in it, and 23 is.

Category-theoretic connectionH

Undoing a chain

"Multiply by 3, then add 1" is a chain of two actions, one after the other. Undoing it means undoing the steps in the reverse order: first "subtract 1", then "divide by 3" — just as you take off your shoes before your socks. You can explore chains of actions, and why undoing goes backwards, in One thing after another.

History & global context

History and global context

  • Egypt. The Rhind Mathematical Papyrus, copied by the scribe Ahmes around 1550 BCE from an older text, contains problems with an unknown quantity, called aha ("heap"). Problem 24 asks for a quantity which, together with one seventh of itself, makes 19. The scribe solved it by "false position": try 7 (then 7 plus its seventh is 8), and then scale up, giving 165816\tfrac{5}{8}.
  • China. The Nine Chapters on the Mathematical Art, compiled about two thousand years ago, solves problems with several unknown quantities at once, using counting rods laid out on a board.
  • India. Indian mathematicians, from Brahmagupta in the 7th century onwards, named unknown quantities with words, such as yāvat-tāvat ("as much as"), and used the names of colours — "black", "blue", "yellow" — when there were several unknowns at once.
  • Baghdad. Around 820 CE, Muḥammad ibn Mūsā al-Khwārizmī wrote a book on solving equations. Its title contains the word al-jabr, "restoring": moving a quantity that is taken away on one side of an equation to the other side. That word became our word algebra.
  • The equals sign. In 1557 the Welsh mathematician Robert Recorde introduced "=" in The Whetstone of Witte, choosing two parallel lines because "no two things can be more equal".
  • Letters. In 1591 François Viète in France used letters for unknown and known numbers; in 1637 René Descartes used xx, yy and zz for unknowns, as we still do.
  • The order of operations grew up gradually as mathematicians wrote more and more algebra; it has no single inventor. Today it is taught, with small differences in the names (such as BODMAS, BIDMAS and PEMDAS), all over the world.
Further reading

For grown-ups: further reading

  • Gay Robins and Charles Shute, The Rhind Mathematical Papyrus: An Ancient Egyptian Text (1987) — the papyrus, with its aha problems, explained.
  • Victor J. Katz, A History of Mathematics: An Introduction (1993; later editions) — the history of algebra in Egypt, Mesopotamia, China, India and the Islamic world.
  • Liping Ma, Knowing and Teaching Elementary Mathematics (1999) — how deep understanding of arithmetic, including inverse operations, prepares children for algebra.
  • George Pólya, How to Solve It (1945) — including "working backwards", the strategy behind undoing steps in reverse order.