Skip to content
The Lumenary
Grades 6–8Ages 11–14 60 min

Variables and algebraic expressions

Letters that stand for numbers, rules that work for every number at once, and why (a+b)2(a + b)^2 is not a2+b2a^2 + b^2.

Formally verified · 13 LeanComputationally tested · 1 PythonAI-drafted · awaiting human reviewMathematically reviewed: Awaiting human review

Your guides: Chloe, Margaret, Gideon

MotivationH

Three ways to count matchsticks

Amara in Lagos, Kenji in Osaka and Lucía in Bogotá are building a row of squares out of matchsticks. Neighbouring squares share a side.

4+3+3+3
A row of four squares. The first square needs 4 sticks (gold); each new square needs only 3 more (teal), because it shares a side with the square before it.

How many sticks does a row of nn squares need? Each of them finds a rule:

  • Amara: "Start with one stick on the left. Every square then adds 3 sticks." That gives 3n+13n + 1.
  • Kenji: "The first square needs 4. Each of the other n−1n - 1 squares needs 3." That gives 4+3(n−1)4 + 3(n - 1).
  • Lucía: "Every square has 4 sides, but n−1n - 1 sides are shared and were counted twice." That gives 4n−(n−1)4n - (n - 1).

They try n=1,2,3,4,5n = 1, 2, 3, 4, 5 and get 4,7,10,13,164, 7, 10, 13, 16 every time. Are the three rules always the same — for a row of a thousand squares, or a million? Trying values cannot settle that, because there are infinitely many values to try. In this lesson we learn the language that lets us answer such questions once and for all: variables and algebraic expressions.

IntuitionH

Letters that stand for numbers

In 3n+13n + 1, the letter nn is a variable: a name for a number that we have not fixed. It is like the box in 3×□+13 \times \square + 1 from your work on expressions and unknowns, except that a letter can appear several times and always means the same number each time it appears.

An expression such as 3n+13n + 1 is a recipe: give it a number, follow the instructions, and a number comes out. Putting a number in place of the variable is called substituting or evaluating.

Example— Evaluating with a negative number

Evaluate 2x2−5x2x^2 - 5x when x=−3x = -3. Put the number in brackets first, then follow the order of operations (powers, then multiplication, then addition and subtraction):

2x2−5x=2⋅(−3)2−5⋅(−3)=2⋅9+15=33.2x^2 - 5x = 2 \cdot (-3)^2 - 5 \cdot (-3) = 2 \cdot 9 + 15 = 33.

The brackets matter: (−3)2=9(-3)^2 = 9, but −32-3^2 means −(32)=−9-(3^2) = -9.

DefinitionsH

The vocabulary of algebra

Definition— variable

A variable is a letter that stands for any number from a given set — for example, any integer, or any rational number. Different letters may stand for the same number; the same letter always stands for the same number within one expression.

Definition— algebraic expression

An algebraic expression is built from numbers and variables using addition, subtraction, multiplication, division and powers, with brackets to show the order. The parts joined by ++ or −- are its terms; the number multiplying the variables in a term is its coefficient; a term with no variable is a constant. In 4x2−7x+24x^2 - 7x + 2 the terms are 4x24x^2, −7x-7x and 22, and the coefficient of xx is −7-7.

We write products without a dot: 3n3n means 3⋅n3 \cdot n, and abab means a⋅ba \cdot b.

Definition— like terms

Two terms are like terms if they contain the same variables raised to the same powers. 3x3x and −5x-5x are like terms; 3x3x and 3x23x^2 are not; neither are 2ab2ab and 2a2a.

Definition— equivalent expressions

Two expressions are equivalent (on a given set of numbers) if they have the same value for every value of their variables in that set. We then write them with an equals sign, as an identity: 4+3(n−1)=3n+14 + 3(n - 1) = 3n + 1.

Definition— power

For a whole number n≥1n \geq 1, the power ana^n is the product of nn factors, each equal to aa:   an=a⋅a⋯a\;a^n = a \cdot a \cdots a. The number aa is the base and nn the exponent. For a≠0a \neq 0 we define a0=1a^0 = 1 (we shall see why).

Note

The set matters. The expressions xx\tfrac{x}{x} and 11 are equivalent on the non-zero numbers, but not on all numbers: at x=0x = 0 the first one has no value at all.

Worked examplesH

Writing, simplifying, expanding, factorising

Example— Writing expressions for situations

  • A taxi in Lima charges a fixed 5 soles plus 2 soles per kilometre. A ride of kk kilometres costs 5+2k5 + 2k soles.
  • A rectangle with sides ℓ\ell and ww has perimeter ℓ+w+ℓ+w=2ℓ+2w\ell + w + \ell + w = 2\ell + 2w.
  • The temperature in Ulaanbaatar was tt degrees at dawn and rose by 99 degrees by noon: it is now t+9t + 9 degrees. If t=−23t = -23, that is −14-14 degrees.

Example— Combining like terms

4x+3y−x+5y−2=(4−1)x+(3+5)y−2=3x+8y−2.4x + 3y - x + 5y - 2 = (4 - 1)x + (3 + 5)y - 2 = 3x + 8y - 2.

We may only combine like terms: 3x+8y3x + 8y cannot be squeezed into one term (it is not 11xy11xy), just as 3 metres plus 8 kilograms is not 11 of anything.

Example— Expanding with the distributive law

The distributive law a(b+c)=ab+aca(b + c) = ab + ac multiplies every term inside the bracket:

  • 3(2x−5)=6x−153(2x - 5) = 6x - 15.
  • −2(x−4)=−2x+8-2(x - 4) = -2x + 8 — the −2-2 multiplies both terms, and (−2)(−4)=+8(-2)(-4) = +8.
  • 5−(x−3)=5−x+3=8−x5 - (x - 3) = 5 - x + 3 = 8 - x.

To multiply two brackets, use the law twice. A grid keeps track of the four products:

×\timesxx33
xxx2x^23x3x
222x2x66

so (x+3)(x+2)=x2+3x+2x+6=x2+5x+6(x + 3)(x + 2) = x^2 + 3x + 2x + 6 = x^2 + 5x + 6.

Example— Factorising: the distributive law backwards

Factorising writes a sum as a product. Look for a common factor of all the terms:

  • 6x+9=3(2x+3)6x + 9 = 3(2x + 3), because 33 divides both 6x6x and 99.
  • 12x+18=6(2x+3)12x + 18 = 6(2x + 3). (Writing 3(4x+6)3(4x + 6) is correct but not complete: 4x+64x + 6 still has the common factor 22.)
  • x2−2x=x(x−2)x^2 - 2x = x(x - 2).

You can always check a factorisation by expanding it again.

Example— Non-examples: tempting but wrong

  • 3x+2y3x + 2y is not 5xy5xy. Try x=y=1x = y = 1: both expressions give 55, so the test passes! But x=1x = 1, y=2y = 2 gives 77 for 3x+2y3x + 2y and 1010 for 5xy5xy.
  • x+x=2xx + x = 2x, not x2x^2: at x=3x = 3, x+x=6x + x = 6 but x2=9x^2 = 9.
  • (x+3)2≠x2+9(x + 3)^2 \neq x^2 + 9: at x=1x = 1 the left side is 1616 and the right side is 1010.

Notice that in the first non-example a single test passed by luck. One passing test proves nothing; one failing test is enough to show two expressions are not equivalent.

ProofH

Why the rules work

Every rule for rearranging expressions comes from a few basic laws of arithmetic, which hold for all numbers: commutativity (ab=baab = ba, a+b=b+aa + b = b + a), associativity, and the distributive law a(b+c)=ab+aca(b + c) = ab + ac. Because the laws hold for every number, anything we derive from them holds for every number too. That is how we can answer a question about infinitely many values in a few lines.

Theorem— combining like terms

For all numbers aa, bb and xx:   ax+bx=(a+b)x\;ax + bx = (a + b)x.

Proof

By commutativity, ax+bx=xa+xbax + bx = xa + xb, and by the distributive law xa+xb=x(a+b)=(a+b)xxa + xb = x(a + b) = (a + b)x.

∎

Theorem— the three matchstick rules agree

For every number nn:   4+3(n−1)=3n+1  \;4 + 3(n - 1) = 3n + 1\; and   4n−(n−1)=3n+1\;4n - (n - 1) = 3n + 1.

Proof

By the distributive law, 3(n−1)=3n−33(n - 1) = 3n - 3, so 4+3(n−1)=4+3n−3=3n+14 + 3(n - 1) = 4 + 3n - 3 = 3n + 1. For Lucía's rule, subtracting n−1n - 1 means subtracting nn and adding 11: 4n−(n−1)=4n−n+1=3n+14n - (n - 1) = 4n - n + 1 = 3n + 1.

∎

So Amara, Kenji and Lucía will agree for every row of squares there could ever be. Notice what the proof did not do: it did not try any values.

Theorem— square of a sum

For all numbers aa and bb:   (a+b)2=a2+2ab+b2\;(a + b)^2 = a^2 + 2ab + b^2.

Proof

Use the distributive law twice, then combine like terms (using ba=abba = ab):

(a+b)2=(a+b)(a+b)=a(a+b)+b(a+b)=a2+ab+ba+b2=a2+2ab+b2.\begin{aligned} (a + b)^2 &= (a + b)(a + b) \\ &= a(a + b) + b(a + b) \\ &= a^2 + ab + ba + b^2 \\ &= a^2 + 2ab + b^2. \end{aligned}

∎

a²ababb²abab
The square of a sum as an area: a square with side a + b splits into a square a², a square b² and two rectangles ab. For positive lengths the picture shows why (a + b)² is bigger than a² + b² by exactly 2ab; the algebraic proof covers all numbers, negative ones included.

The theorem tells us exactly how wrong the tempting rule (a+b)2=a2+b2(a + b)^2 = a^2 + b^2 is: it leaves out 2ab2ab. So the tempting rule gives the right answer only when 2ab=02ab = 0.

Theorem— when the tempting rule happens to work

For integers aa and bb:   (a+b)2=a2+b2  \;(a + b)^2 = a^2 + b^2\; if and only if   a=0\;a = 0 or b=0b = 0.

Proof

By the square of a sum, the equation says a2+2ab+b2=a2+b2a^2 + 2ab + b^2 = a^2 + b^2, that is, 2ab=02ab = 0, that is, ab=0ab = 0. A product of two non-zero integers is never 00 (its size is at least 1⋅11 \cdot 1), so ab=0ab = 0 exactly when a=0a = 0 or b=0b = 0.

∎

The same kind of argument shows exactly when 2x2x and x2x^2 — two expressions that are often confused — take the same value.

Theorem— 2x and x² agree only at 0 and 2

For an integer xx:   2x=x2  \;2x = x^2\; if and only if   x=0\;x = 0 or x=2x = 2.

Proof

2x=x22x = x^2 means x2−2x=0x^2 - 2x = 0. Factorising, x(x−2)=0x(x - 2) = 0. A product of integers is 00 exactly when one of the factors is 00, so x=0x = 0 or x−2=0x - 2 = 0, that is, x=2x = 2. Conversely, 2⋅0=0=022 \cdot 0 = 0 = 0^2 and 2⋅2=4=222 \cdot 2 = 4 = 2^2.

∎

ProofH

Powers and the laws of exponents

Powers are shorthand for repeated multiplication: 25=2⋅2⋅2⋅2⋅2=322^5 = 2 \cdot 2 \cdot 2 \cdot 2 \cdot 2 = 32. Counting factors gives three laws.

Theorem— laws of exponents

For all numbers aa, bb and all whole numbers m,n≥1m, n \geq 1:

  1. am⋅an=am+na^m \cdot a^n = a^{m+n},
  2. (am)n=amn(a^m)^n = a^{mn},
  3. (ab)n=anbn(ab)^n = a^n b^n.

Proof

  1. am⋅ana^m \cdot a^n is mm factors of aa followed by nn more. By associativity the brackets do not matter, so this is a product of m+nm + n factors of aa, which is am+na^{m+n}.
  2. (am)n(a^m)^n is nn blocks, each consisting of mm factors of aa: m⋅nm \cdot n factors in all, so it is amna^{mn}.
  3. (ab)n=(ab)(ab)⋯(ab)(ab)^n = (ab)(ab)\cdots(ab) with nn blocks. By commutativity and associativity we may reorder the 2n2n factors as (a⋯a)(b⋯b)=anbn(a \cdots a)(b \cdots b) = a^n b^n.

∎

Example— Using the laws — and avoiding the traps

  • 23⋅24=27=1282^3 \cdot 2^4 = 2^7 = 128. It is not 2122^{12} (that would be multiplying the exponents) and not 474^7 (the bases are not multiplied).
  • (23)4=212=4096(2^3)^4 = 2^{12} = 4096, while 234=2812^{3^4} = 2^{81} is enormously bigger: the brackets matter.
  • (3x)2=9x2(3x)^2 = 9x^2, not 3x23x^2.
  • 26+26=2⋅26=272^6 + 2^6 = 2 \cdot 2^6 = 2^7, not 2122^{12}: the laws are about products of powers, not sums.
  • (−2)3=−8(-2)^3 = -8 and (−2)4=16(-2)^4 = 16: an odd power of a negative number is negative, an even power is positive.

Why a0=1a^0 = 1? The laws above were proved for exponents 1,2,3,…1, 2, 3, \dots. If we want the first law to keep working when an exponent is 00, then a1⋅a0=a1+0=a1a^1 \cdot a^0 = a^{1+0} = a^1, so a⋅a0=aa \cdot a^0 = a. For a≠0a \neq 0 we can divide both sides by aa, and the only possible value is a0=1a^0 = 1. So a0=1a^0 = 1 is not an arbitrary rule: it is the only choice that keeps the laws true.

Note

What about 000^0? Many mathematicians (and Lean) define 00=10^0 = 1 too, because it makes formulas such as the binomial theorem work without exceptions. Some school books leave it undefined. Both are consistent; the argument above simply cannot decide the value when a=0a = 0.

ExplanationH

Testing is not proving

Testing values is a good way to check algebra, and it is all you need to show that two expressions are not equivalent: one counterexample is enough. But passing tests can never prove an equivalence on its own. Here is a striking example.

Theorem— four tests can mislead

The expressions x+x(x−1)(x−2)(x−3)x + x(x - 1)(x - 2)(x - 3) and xx agree for x=0,1,2,3x = 0, 1, 2, 3, but they are not equivalent.

Proof

For x=0,1,2x = 0, 1, 2 or 33, one of the factors xx, x−1x - 1, x−2x - 2, x−3x - 3 is zero, so the product is 00 and the first expression equals xx. But for x=4x = 4 the first expression is 4+4⋅3⋅2⋅1=28≠44 + 4 \cdot 3 \cdot 2 \cdot 1 = 28 \neq 4.

∎

Someone who tested only 0,1,2,30, 1, 2, 3 would be fooled. By changing the factors, you can build two expressions that agree on a thousand test values and still differ at the next one.

Note

There is a surprising twist, which you will prove in the lesson on polynomials: if two polynomial expressions in one variable both have degree at most dd, and they agree at d+1d + 1 different values, then they are equivalent. For instance, 4+3(n−1)4 + 3(n - 1) and 3n+13n + 1 have degree 11, so agreeing at n=1n = 1 and n=2n = 2 already forces them to agree everywhere. But notice what does the proving here: the theorem, and the knowledge of the degree — not the tests on their own.

Formal mathematicsF

Identities, checked by Lean

In Lean, a variable is exactly what it is on paper: a name for an arbitrary number. A statement with (n : Int) among its inputs claims something for every integer at once — which is precisely what "equivalent expressions" means.

First, combining like terms, which is the distributive law read backwards. This one just applies core Lean's law Int.add_mul:

like_termsLean 4Applies a library theoremFormally verified
/-- Combining like terms: `a·x + b·x = (a + b)·x` for all integers. This is the distributive
law read from right to left; the proof applies core Lean's `Int.add_mul`. -/
theorem like_terms (a b x : Int) : a * x + b * x = (a + b) * x :=
  (Int.add_mul a b x).symm
Axioms used: propextformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

The three matchstick rules agree for every integer nn:

stick_formulas_agreeLean 4Proved here from basic factsFormally verified
/-- The matchstick pattern: a row of `n` squares needs `3n + 1` sticks. Two other ways of
counting give `4 + 3(n − 1)` ("first square, then 3 more each time") and `4n − (n − 1)`
("4 per square, minus the shared sides"). All three expressions are equivalent: they agree for
*every* integer `n`, not just for the values we tried. -/
theorem stick_formulas_agree (n : Int) :
    4 + 3 * (n - 1) = 3 * n + 1 ∧ 4 * n - (n - 1) = 3 * n + 1 := by
  constructor
  · calc 4 + 3 * (n - 1) = 4 + (3 * n - 3 * 1) := by rw [Int.mul_sub]
      _ = 3 * n + 1 := by omega
  · omega
Axioms used: propext, Quot.soundformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

The square of a sum, by the distributive law twice — and then the square of a difference, obtained by putting −b-b in place of bb (a variable may stand for a negative number):

square_of_sumLean 4Proved here from basic factsFormally verified
/-- The square of a sum: `(a + b)² = a² + 2ab + b²`, proved by using the distributive law
twice, exactly as in the lesson:
`(a + b)(a + b) = a(a + b) + b(a + b) = (a² + ab) + (ba + b²)`, then collect like terms. -/
theorem square_of_sum (a b : Int) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 :=
  calc (a + b) ^ 2 = (a + b) * (a + b) := by
        rw [Int.pow_succ, Int.pow_succ, Int.pow_zero, Int.one_mul]
    _ = a * (a + b) + b * (a + b) := Int.add_mul a b (a + b)
    _ = (a * a + a * b) + (b * a + b * b) := by rw [Int.mul_add, Int.mul_add]
    _ = a ^ 2 + 2 * a * b + b ^ 2 := by grind
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor
square_of_differenceLean 4Proved here from basic factsFormally verified
/-- The square of a difference, `(a − b)² = a² − 2ab + b²`, obtained from `square_of_sum`
by putting `−b` in place of `b`: a variable can stand for *any* integer, negative ones
included. -/
theorem square_of_difference (a b : Int) : (a - b) ^ 2 = a ^ 2 - 2 * a * b + b ^ 2 :=
  calc (a - b) ^ 2 = (a + -b) ^ 2 := by rw [Int.sub_eq_add_neg]
    _ = a ^ 2 + 2 * a * -b + (-b) ^ 2 := square_of_sum a (-b)
    _ = a ^ 2 - 2 * a * b + b ^ 2 := by grind
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

A single counterexample refutes the tempting rule, and the next theorem says exactly when the tempting rule happens to give the right answer:

square_of_sum_counterexampleLean 4Proved here from basic factsFormally verified
/-- `(a + b)² = a² + b²` is **not** an identity: one counterexample is enough to refute it.
With `a = b = 1` the left side is `4` and the right side is `2`. -/
theorem square_of_sum_counterexample : ¬ ∀ a b : Int, (a + b) ^ 2 = a ^ 2 + b ^ 2 := by
  intro h
  have h11 := h 1 1
  -- `h11 : (1 + 1) ^ 2 = 1 ^ 2 + 1 ^ 2`, i.e. `4 = 2`, which `decide` refutes.
  revert h11
  decide
Axioms used: No axiomsformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor
square_of_sum_eq_iffLean 4Proved here from basic factsFormally verified
/-- Exactly when does the tempting mistake give the right answer?
`(a + b)² = a² + b²` holds precisely when `a = 0` or `b = 0`. By `square_of_sum` the equation
says `2ab = 0`, so `ab = 0`, and a product of integers is `0` only if a factor is `0`
(`Int.mul_eq_zero`). -/
theorem square_of_sum_eq_iff (a b : Int) :
    (a + b) ^ 2 = a ^ 2 + b ^ 2 ↔ a = 0 ∨ b = 0 := by
  rw [square_of_sum]
  constructor
  · intro h
    have hab : a * b = 0 := by grind
    exact Int.mul_eq_zero.mp hab
  · rintro (rfl | rfl) <;> grind
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor
two_mul_eq_sq_iffLean 4Proved here from basic factsFormally verified
/-- `2x` and `x²` are different expressions, but they *can* take equal values: exactly when
`x = 0` or `x = 2`. Proof: `2x = x²` means `x² − 2x = 0`, that is `x(x − 2) = 0`, and a product
is zero only if one factor is zero. -/
theorem two_mul_eq_sq_iff (x : Int) : 2 * x = x ^ 2 ↔ x = 0 ∨ x = 2 := by
  constructor
  · intro h
    have hfac : x * (x - 2) = 0 := by grind
    rcases Int.mul_eq_zero.mp hfac with h0 | h2
    · exact Or.inl h0
    · right; omega
  · rintro (rfl | rfl) <;> decide
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

Four passing tests, and still not equivalent:

few_tests_misleadLean 4Proved here from basic factsFormally verified
/-- Testing can refute an equivalence but cannot prove it. The expressions
`x + x(x − 1)(x − 2)(x − 3)` and `x` agree for `x = 0, 1, 2, 3` — four successful tests — yet
they are not equivalent, because at `x = 4` the first is `28`. -/
theorem few_tests_mislead :
    (∀ x : Int, 0 ≤ x → x ≤ 3 → x + x * (x - 1) * (x - 2) * (x - 3) = x) ∧
      (4 : Int) + 4 * (4 - 1) * (4 - 2) * (4 - 3) ≠ 4 := by
  constructor
  · intro x h0 h3
    have hx : x = 0 ∨ x = 1 ∨ x = 2 ∨ x = 3 := by omega
    rcases hx with rfl | rfl | rfl | rfl <;> decide
  · decide
Axioms used: propext, Quot.soundformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

Lean defines powers just as we did: a0=1a^0 = 1 and an+1=an⋅aa^{n+1} = a^n \cdot a. So the laws of exponents are proved by induction: check the law when the exponent is 00, then show that whenever it holds for an exponent kk it also holds for k+1k + 1. Like a row of dominoes, that reaches every whole number. This is the precise version of "count the factors".

pow_add_lawLean 4Proved here from basic factsFormally verified
/-- `aᵐ · aⁿ = aᵐ⁺ⁿ`: `m` factors of `a` followed by `n` more make `m + n` factors.
Proof by induction on `n`, using associativity of multiplication for the step. -/
theorem pow_add_law (a : Int) (m n : Nat) : a ^ m * a ^ n = a ^ (m + n) := by
  induction n with
  | zero => rw [Int.pow_zero, Int.mul_one, Nat.add_zero]
  | succ k ih =>
    calc a ^ m * a ^ (k + 1) = a ^ m * (a ^ k * a) := by rw [Int.pow_succ]
      _ = (a ^ m * a ^ k) * a := (Int.mul_assoc _ _ _).symm
      _ = a ^ (m + k) * a := by rw [ih]
      _ = a ^ (m + (k + 1)) := by rw [← Nat.add_assoc, Int.pow_succ]
Axioms used: propextformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor
pow_mul_lawLean 4Proved here from basic factsFormally verified
/-- `(aᵐ)ⁿ = aᵐⁿ`: `n` blocks of `m` factors each make `m·n` factors.
Proof by induction on `n`, using `pow_add_law` for the step. -/
theorem pow_mul_law (a : Int) (m n : Nat) : (a ^ m) ^ n = a ^ (m * n) := by
  induction n with
  | zero => rw [Int.pow_zero, Nat.mul_zero, Int.pow_zero]
  | succ k ih =>
    calc (a ^ m) ^ (k + 1) = (a ^ m) ^ k * a ^ m := Int.pow_succ _ _
      _ = a ^ (m * k) * a ^ m := by rw [ih]
      _ = a ^ (m * k + m) := pow_add_law a (m * k) m
      _ = a ^ (m * (k + 1)) := by rw [Nat.mul_succ]
Axioms used: propextformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor
mul_pow_lawLean 4Proved here from basic factsFormally verified
/-- `(ab)ⁿ = aⁿbⁿ`: regroup the factors `ab · ab ⋯ ab` as `a ⋯ a · b ⋯ b`. The regrouping
uses commutativity, which is why this law fails for operations that do not commute (such as
matrix multiplication, later). Proof by induction on `n`. -/
theorem mul_pow_law (a b : Int) (n : Nat) : (a * b) ^ n = a ^ n * b ^ n := by
  induction n with
  | zero => rw [Int.pow_zero, Int.pow_zero, Int.pow_zero, Int.mul_one]
  | succ k ih =>
    calc (a * b) ^ (k + 1) = (a * b) ^ k * (a * b) := Int.pow_succ _ _
      _ = (a ^ k * b ^ k) * (a * b) := by rw [ih]
      _ = (a ^ k * a) * (b ^ k * b) := by grind
      _ = a ^ (k + 1) * b ^ (k + 1) := by rw [Int.pow_succ, Int.pow_succ]
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

Core Lean also has the first law in its library, so a one-line proof is possible. Both versions are machine-checked; the difference is honesty about who did the work — pow_add_law proves it here, pow_add_library relies on core Lean's Int.pow_add.

pow_add_libraryLean 4Applies a library theoremFormally verified
/-- The same law, by applying core Lean's `Int.pow_add` (which states it the other way
round). Compare `pow_add_law`, which proves it here by induction. -/
theorem pow_add_library (a : Int) (m n : Nat) : a ^ m * a ^ n = a ^ (m + n) :=
  (Int.pow_add a m n).symm
Axioms used: propextformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

Finally, the argument that a0a^0 has no choice but to be 11:

pow_zero_forcedLean 4Proved here from basic factsFormally verified
/-- Why `a⁰ = 1`: if the law `a¹ · a⁰ = a¹⁺⁰ = a¹` is to hold, then for `a ≠ 0` the number
`a⁰` must be a `z` with `a · z = a`, and the only such `z` is `1` (cancel the non-zero
factor `a`, using `Int.eq_of_mul_eq_mul_left`). So the definition `a⁰ = 1` is not arbitrary:
it is the only choice that keeps the exponent laws true. -/
theorem pow_zero_forced (a z : Int) (ha : a ≠ 0) (h : a * z = a) : z = 1 := by
  have h' : a * z = a * 1 := by rw [Int.mul_one]; exact h
  exact Int.eq_of_mul_eq_mul_left ha h'
Axioms used: propextformal/Lumenary/VariablesExpressions.leanTry it in the Lean web editor

What is formalised here: every identity and law in this lesson for the integers. The same proofs work for rational numbers, because they use only the laws of arithmetic; Mathlib proves them once for every commutative ring (add_sq, pow_add, pow_mul, mul_pow).

ComputationC

Testing, refuting and expanding by computer

Equivalent expressions: tests, counterexamples and expansionPythonCounterexampleExhaustive finite checkSymbolic computationComputationally tested
"""Are two expressions equivalent? Testing, refuting and expanding.

Two expressions are equivalent when they agree for EVERY value of their
variables. This experiment (1) tests several pairs of expressions on a block
of integer values, reporting the first counterexample if there is one;
(2) shows how a pair can pass four tests in a row and still be different;
(3) counts exactly, on a finite grid, how often the false rule
(a + b)^2 = a^2 + b^2 happens to give the right answer; and (4) asks SymPy to
expand the difference of each pair. Tests can refute an equivalence with one
counterexample; passing tests is evidence only. The symbolic expansion is a
genuine verification for polynomials, as trustworthy as SymPy's
implementation of the laws of algebra.
"""

import sympy

a, b, n, x = sympy.symbols("a b n x")

#: (name, left expression, right expression) - each pair is tested below.
PAIRS = [
    ("4 + 3(n - 1)  vs  3n + 1", 4 + 3 * (n - 1), 3 * n + 1),
    ("4n - (n - 1)  vs  3n + 1", 4 * n - (n - 1), 3 * n + 1),
    ("(a + b)^2  vs  a^2 + b^2", (a + b) ** 2, a ** 2 + b ** 2),
    ("(a + b)^2  vs  a^2 + 2ab + b^2", (a + b) ** 2, a ** 2 + 2 * a * b + b ** 2),
    ("2x  vs  x^2", 2 * x, x ** 2),
]

#: The pair that agrees at x = 0, 1, 2, 3 but is not equivalent.
TRAP = (x + x * (x - 1) * (x - 2) * (x - 3), x)

def value(expr, assignment):
    """Exact value of a SymPy expression for a dict {symbol: integer}."""
    return int(expr.subs(assignment))

def ordered_values(radius):
    """The integers -radius..radius in the order 0, 1, -1, 2, -2, ..."""
    if radius < 0:
        raise ValueError("radius must be non-negative")
    out = [0]
    for k in range(1, radius + 1):
        out += [k, -k]
    return out

def assignments(variables, radius):
    """All ways to give each variable a value in -radius..radius.

    The variables are taken in alphabetical order and the values in the
    order of ``ordered_values``, so small values are tried first.
    """
    out = [{}]
    for var in sorted(variables, key=str):
        out = [{**d, var: k} for d in out for k in ordered_values(radius)]
    return out

def first_counterexample(lhs, rhs, radius):
    """Return (tests run, first assignment where lhs != rhs, or None)."""
    variables = lhs.free_symbols | rhs.free_symbols
    tests = assignments(variables, radius)
    for k, assignment in enumerate(tests, start=1):
        if value(lhs, assignment) != value(rhs, assignment):
            return k, assignment
    return len(tests), None

def symbolic_difference(lhs, rhs):
    """SymPy's expansion of lhs - rhs (0 exactly when they are identical)."""
    return sympy.expand(lhs - rhs)

def agreement_count(radius):
    """Count grid points (a, b) in -radius..radius where (a+b)^2 = a^2 + b^2.

    Uses plain integer arithmetic. By the lesson's theorem this happens
    exactly when a = 0 or b = 0, i.e. at 2(2r + 1) - 1 points.
    """
    values = range(-radius, radius + 1)
    hits = sum(1 for p in values for q in values
               if (p + q) ** 2 == p ** 2 + q ** 2)
    return hits, len(values) ** 2

def describe(assignment):
    """Readable form of an assignment such as 'a = -3, b = -3'."""
    return ", ".join(f"{k} = {v}" for k, v in
                     sorted(assignment.items(), key=lambda kv: str(kv[0])))

def main():
    print("1. Testing pairs on the values 0, 1, -1, 2, -2, 3, -3 until one fails")
    for name, lhs, rhs in PAIRS:
        tests, bad = first_counterexample(lhs, rhs, 3)
        verdict = "all agree" if bad is None else "differ at " + describe(bad)
        print(f"   {name:<31} {tests:>2} test(s): {verdict}")
    print()
    print("2. A pair that passes four tests:  x + x(x-1)(x-2)(x-3)  vs  x")
    lhs, rhs = TRAP
    for k in range(6):
        left, right = value(lhs, {x: k}), value(rhs, {x: k})
        mark = "agree" if left == right else "DIFFER"
        print(f"   x = {k}:  {left:>4}  vs  {right}   {mark}")
    print()
    hits, total = agreement_count(100)
    print("3. (a + b)^2 = a^2 + b^2 on the grid -100..100 (every point checked):")
    print(f"   true at {hits} of {total} points ({100 * hits / total:.2f}%),")
    print("   exactly the points where a = 0 or b = 0.")
    print()
    print("4. SymPy expands  left - right  (0 means identical polynomials)")
    for name, lhs, rhs in PAIRS + [("x + x(x-1)(x-2)(x-3)  vs  x", *TRAP)]:
        diff = str(symbolic_difference(lhs, rhs)).replace("**", "^")
        print(f"   {name:<31} {diff}")
    print()
    print("Epistemic status: parts 1 and 2 are finite tests - a single")
    print("counterexample refutes an equivalence, but passing tests proves")
    print("nothing (part 2 passes four in a row). Part 3 is an exhaustive count")
    print("over a finite grid only. Part 4 is a symbolic verification for these")
    print("polynomials, as reliable as SymPy's built-in algebra; the lesson's")
    print("proofs from the distributive law explain why the identities hold.")

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

Read the output carefully. The tests catch the false square rule after nine tries — but the trap expression in part 2 sails through four tests. Part 3 checks every point of a finite grid: the false rule is right less than 1% of the time, at exactly the points our theorem predicts. Part 4 is different in kind. SymPy rewrites left−right\text{left} - \text{right} into a standard form using the laws of algebra, so a result of 00 is a check of the identity for all values — as reliable as SymPy's own implementation of those laws. The difference x4−6x3+11x2−6xx^4 - 6x^3 + 11x^2 - 6x in the last line is x(x−1)(x−2)(x−3)x(x - 1)(x - 2)(x - 3), which is zero at 0,1,2,30, 1, 2, 3 and nowhere else.

Explore

Explore: when do two expressions agree?

Graphing turns "do these expressions agree?" into "where do these curves meet?". Equivalent expressions have the same graph; different expressions can still cross at a few points.

2x and x²

Two different expressions that agree at exactly two values.

x = 0x = 2
  • y = 2x
  • y = x²
  • y = 2x: y = 2
  • y = x²: y = 1

(x + k)² versus x² + k²

Move k. When do the two curves coincide?

  • y = (x + k)²
  • y = x² + k²
  • y = (x + k)²: y = 9
  • y = x² + k²: y = 5

Four tests passed

The curves meet at x = 0, 1, 2, 3 — and then part company.

  • y = x + x(x − 1)(x − 2)(x − 3)
  • y = x
  • y = x + x(x − 1)(x − 2)(x − 3): y = 1
  • y = x: y = 1

Things to try:

  • In the second plot, set k=0k = 0: the curves coincide. For any other kk they meet only at x=0x = 0, and the vertical gap between them at xx is exactly 2kx2kx.
  • Use the trace slider on the third plot just past x=3x = 3 and watch the curves separate.
  • Open "Edit the functions" and graph 4+3(x−1)4 + 3(x - 1) and 3x+13x + 1. Why can you see only one line?
Exercises

Exercises

1. Variables and algebraic expressions

Warm-upHuman

Evaluate 2x2−5x2x^2 - 5x when x=−3x = -3.

2. Variables and algebraic expressions

Warm-upHuman

Simplify 4x+3y−x+5y−24x + 3y - x + 5y - 2.

Choose one

3. Variables and algebraic expressions

Warm-upHuman

Kofi sells bags of oranges at a market in Kumasi for 1212 cedis each. He pays 1515 cedis a day for his stall. Which expression gives his profit, in cedis, on a day when he sells xx bags?

Choose one

4. Variables and algebraic expressions

CoreHuman

Expand (x+5)(x−2)(x + 5)(x - 2) and write it as x2+bx+cx^2 + bx + c. What is bb?

5. Variables and algebraic expressions

CoreHuman

Which is the complete factorisation of 12x+1812x + 18?

Choose one

6. Variables and algebraic expressions

CoreHuman

Which of these are equal to 2122^{12}? (Choose all that apply.)

Choose all that apply

7. Variables and algebraic expressions

CoreHuman

Zainab tests the rule (a+b)2=a2+b2(a + b)^2 = a^2 + b^2 with a=0,b=7a = 0, b = 7 and with a=5,b=0a = 5, b = 0. It works both times. What is the right conclusion?

Choose one

8. Variables and algebraic expressions

CoreFormal

Which Lean statement says that (a+b)2=a2+2ab+b2(a + b)^2 = a^2 + 2ab + b^2 is an identity for the integers?

Choose one

9. Variables and algebraic expressions

CoreComputational

Write find_counterexample(f, g, values) that returns the first v in values with f(v) != g(v), or None if f and g agree on all of them. Then think: if it returns None, have you proved that f and g are equivalent?

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

10. Variables and algebraic expressions

StretchHuman

A row of hexagons is made from matchsticks; neighbouring hexagons share one side. How many matchsticks are needed for a row of 2020 hexagons? (Find an expression for nn hexagons first.)

Common misconceptionsH

Common misconceptions

Misconception

"(a+b)2=a2+b2(a + b)^2 = a^2 + b^2." This forgets the two rectangles abab in the area picture. The correct identity is (a+b)2=a2+2ab+b2(a + b)^2 = a^2 + 2ab + b^2, and the tempting rule is right only when a=0a = 0 or b=0b = 0. One counterexample settles it: (1+1)2=4(1 + 1)^2 = 4, but 12+12=21^2 + 1^2 = 2.

Misconception

"2x2x and x2x^2 are the same." 2x=x+x2x = x + x is doubling; x2=x⋅xx^2 = x \cdot x is squaring. At x=3x = 3 they are 66 and 99. They agree only at x=0x = 0 and x=2x = 2 — which is exactly why a quick test with x=2x = 2 can mislead.

Misconception

"A variable must be positive" or "different letters must have different values." A variable stands for any number of the stated kind. In x+yx + y, nothing stops xx from being −4-4, or yy from being equal to xx. When we need a variable to be positive, or two variables to differ, we must say so.

Misconception

"3a3a means three apples." Letters stand for numbers, not objects. If aa is the price of an apple in rupees, 3a3a is the price of three apples. Reading letters as objects leads to errors such as "3a+2b=5ab3a + 2b = 5ab".

Misconception

"−x2-x^2 and (−x)2(-x)^2 are the same." The power is done before the minus sign: −x2=−(x2)-x^2 = -(x^2), which is never positive, while (−x)2=x2(-x)^2 = x^2, which is never negative. At x=3x = 3: −9-9 and 99.

ViewpointsH

Other ways of seeing

Geometry— Algebra as area

For positive numbers, a product abab is the area of an aa by bb rectangle, and the distributive law a(b+c)=ab+aca(b + c) = ab + ac says that a rectangle cut in two has the total area of its pieces. The area picture of (a+b)2(a + b)^2 above is very old: Proposition 4 of Book II of Euclid's Elements (Alexandria, around 300 BCE) states, in the language of lines and squares, that the square on a line cut in two equals the squares on the two parts together with twice the rectangle contained by them. The picture is a proof for positive lengths; the algebraic proof extends it to all numbers.

Algebra— The same rules, in other number systems

Our proofs used only a short list of laws: commutativity, associativity, the distributive law, and the behaviour of 00 and 11. Any number system obeying those laws — mathematicians call it a commutative ring (see rings) — inherits every identity in this lesson for free. One example is clock arithmetic, where we keep only the remainder after dividing by a fixed number (see modular arithmetic). There is a delightful twist: working with remainders on division by 22, the term 2ab2ab is always 00, so there the "tempting rule" (a+b)2=a2+b2(a + b)^2 = a^2 + b^2 is actually true. Whether a rule holds depends on which laws your numbers obey.

Set theory— An expression is a function

An expression in xx, together with the set its variable ranges over, defines a function: x↦2x2−5xx \mapsto 2x^2 - 5x. Two expressions are equivalent exactly when they define the same function — the same set of input–output pairs. So "xx\tfrac{x}{x} and 11 are equivalent on the non-zero numbers, but not on all numbers" is a statement about the domain of a function, and graphing an expression is drawing that set of pairs.

Wonder Trails

Wonder Trail

Clock arithmetic is a whole world where the rules of algebra still work — with surprises like the one in the algebra viewpoint above.

Wonder Trail · Optional · never gradedClock arithmeticIt's 9 o'clock. What time will it be in 5 hours — and in 29 hours? Can a clock teach us a whole new kind of arithmetic?Follow the trail
History & global context

History and global context

  • Around the 3rd century CE, Diophantus of Alexandria wrote his Arithmetica with a special abbreviation for the unknown number and for its powers — a step from writing algebra in full sentences towards symbols.
  • In India, mathematicians from Brahmagupta (Brāhmasphuṭasiddhānta, 628 CE) onwards handled several unknowns at once, naming the first yāvat-tāvat ("as much as") and further ones after colours such as kālaka ("black") and nīlaka ("blue"); Bhāskara II's Bījagaṇita (1150) writes them with abbreviated syllables.
  • In Baghdad around 820 CE, Muḥammad ibn Mūsā al-Khwārizmī called the unknown shayʾ, "thing", and wrote all his algebra in words. The word algebra comes from the al-jabr in the title of his book.
  • In China, Li Ye (Ceyuan haijing, 1248) and Zhu Shijie (1303) wrote polynomials with the tiān yuán ("celestial element") method: a column of counting-rod numerals in which the position of each coefficient showed the power of the unknown.
  • In France, François Viète (1591) used letters both for unknowns (vowels) and for known quantities (consonants), so that one calculation solved a whole family of problems. René Descartes' La Géométrie (1637) introduced the convention we still use — x,y,zx, y, z for unknowns, a,b,ca, b, c for known quantities — and raised exponents such as x3x^3.
Further reading

Further reading

  • I. M. Gelfand and A. Shen, Algebra (1993) — a beautiful, problem-driven introduction, including the square of a sum and the laws of exponents.
  • W. W. Sawyer, Vision in Elementary Mathematics (1964) — algebra through pictures and patterns, including area models.
  • Richard Rusczyk, David Patrick and Ravi Boppana, Prealgebra (2011) — careful practice with expressions, written for curious students.
  • Victor J. Katz, A History of Mathematics: An Introduction (3rd edition, 2009) — the story of algebraic notation in many civilisations.