TExcelTAD
Home › Algebra › Special Products and Factoring

Special Products and Factoring

A few products come up so often that they are worth memorizing: the square of a sum or difference, the difference of two squares, and the sum and difference of two cubes. Recognizing them lets you expand and factor in one step, do some mental arithmetic, and understand a couple of tricks used in fast multiplication and accurate floating-point code.

The patterns

Name Product form Expanded form
square of a sum (a+b)2(a + b)^2 a2+2ab+b2a^2 + 2ab + b^2
square of a difference (a−b)2(a - b)^2 a2−2ab+b2a^2 - 2ab + b^2
difference of squares (a+b)(a−b)(a + b)(a - b) a2−b2a^2 - b^2
sum of cubes (a+b)(a2−ab+b2)(a + b)(a^2 - ab + b^2) a3+b3a^3 + b^3
difference of cubes (a−b)(a2+ab+b2)(a - b)(a^2 + ab + b^2) a3−b3a^3 - b^3

Read left to right, the table expands. Read right to left, it factors. Each row is just FOIL or the distributive law done once in general (see Multiplying Polynomials); for example (a+b)(a−b)=a2−ab+ab−b2=a2−b2(a + b)(a - b) = a^2 - ab + ab - b^2 = a^2 - b^2, because the middle terms cancel.

Note: (a+b)2(a + b)^2 is not a2+b2a^2 + b^2. With a=3a = 3, b=4b = 4: (3+4)2=49(3 + 4)^2 = 49, while 32+42=253^2 + 4^2 = 25. The missing 2ab=242ab = 24 is the most common algebra error there is.

Perfect-square trinomials

A trinomial is a perfect square when its first and last terms are squares and the middle term is twice the product of their roots:

9x2+30x+25:9x2=(3x)2, 25=52, 2⋅3x⋅5=30x ✓→ (3x+5)2x2−14x+49:x2=x2, 49=72, 2⋅x⋅7=14x, sign −→ (x−7)2x2+10x+16:16=42, but 2⋅x⋅4=8x≠10x→ not a perfect square\begin{aligned} 9x^2 + 30x + 25{:}\quad &9x^2 = (3x)^2,\ 25 = 5^2,\ 2 \cdot 3x \cdot 5 = 30x\ \checkmark && \to\ (3x + 5)^2 \\ x^2 - 14x + 49{:}\quad &x^2 = x^2,\ 49 = 7^2,\ 2 \cdot x \cdot 7 = 14x,\ \text{sign } - && \to\ (x - 7)^2 \\ x^2 + 10x + 16{:}\quad &16 = 4^2,\ \text{but } 2 \cdot x \cdot 4 = 8x \ne 10x && \to\ \text{not a perfect square} \end{aligned}

(x2+10x+16x^2 + 10x + 16 still factors, as (x+2)(x+8)(x + 2)(x + 8), with the usual method.) Perfect squares are the heart of completing the square.

Difference of squares

Any expression of the form (something)2−(something)2(\text{something})^2 - (\text{something})^2 factors at once:

x2−49=(x+7)(x−7)16y2−25=(4y+5)(4y−5)x4−81=(x2+9)(x2−9)=(x2+9)(x+3)(x−3)(x+1)2−4=(x+1+2)(x+1−2)=(x+3)(x−1)\begin{aligned} x^2 - 49 &= (x + 7)(x - 7) \\ 16y^2 - 25 &= (4y + 5)(4y - 5) \\ x^4 - 81 &= (x^2 + 9)(x^2 - 9) = (x^2 + 9)(x + 3)(x - 3) \\ (x + 1)^2 - 4 &= (x + 1 + 2)(x + 1 - 2) = (x + 3)(x - 1) \end{aligned}

A sum of squares such as x2+9x^2 + 9 does not factor with real numbers. In the third line, keep going until no factor is a difference of squares any more.

It is also a mental-math trick: 47×53=(50−3)(50+3)=2500−9=249147 \times 53 = (50 - 3)(50 + 3) = 2500 - 9 = 2491.

Sum and difference of cubes

x3−8=x3−23=(x−2)(x2+2x+4)27y3+1=(3y)3+13=(3y+1)(9y2−3y+1)\begin{aligned} x^3 - 8 &= x^3 - 2^3 = (x - 2)(x^2 + 2x + 4) \\ 27y^3 + 1 &= (3y)^3 + 1^3 = (3y + 1)(9y^2 - 3y + 1) \end{aligned}

Memory aid: the binomial has the same sign as the original, the trinomial's middle term the opposite sign, and its last term is always ++. The trinomial left over never factors further over the real numbers.

Checking with Python

python · try it yourself
import sympy as sp

x, y = sp.symbols("x y")
for e in [9*x**2 + 30*x + 25, x**4 - 81, 27*y**3 + 1, x**3 - 8, x**2 + 9]:
    print(e, " =>  ", sp.factor(e))

print("47 × 53 =", 47 * 53, "   50² − 3² =", 50**2 - 3**2)
9*x**2 + 30*x + 25  =>   (3*x + 5)**2
x**4 - 81  =>   (x - 3)*(x + 3)*(x**2 + 9)
27*y**3 + 1  =>   (3*y + 1)*(9*y**2 - 3*y + 1)
x**3 - 8  =>   (x - 2)*(x**2 + 2*x + 4)
x**2 + 9  =>   x**2 + 9
47 × 53 = 2491    50² − 3² = 2491

SymPy factors over the rationals by default, so x2+9x^2 + 9 is returned unchanged.

In software

Accurate a2−b2a^2 - b^2. When aa and bb are close floating-point numbers, a*a - b*b subtracts two large, nearly equal numbers and loses digits to rounding (catastrophic cancellation). (a - b)*(a + b) subtracts first, while the inputs are still exact, and keeps far more correct digits:

python · try it yourself
from fractions import Fraction

a, b = 1e8 + 1.0, 1e8
exact = Fraction(a)**2 - Fraction(b)**2
print("exact      :", float(exact))
print("a*a - b*b  :", a * a - b * b)
print("(a-b)*(a+b):", (a - b) * (a + b))
exact      : 200000001.0
a*a - b*b  : 200000000.0
(a-b)*(a+b): 200000001.0

Karatsuba multiplication. To multiply two big numbers split into halves, (a⋅B+b)(c⋅B+d)(a \cdot B + b)(c \cdot B + d), the schoolbook way needs four products ac,ad,bc,bdac, ad, bc, bd. Karatsuba notices that ad+bc=(a+b)(c+d)−ac−bdad + bc = (a + b)(c + d) - ac - bd, so three multiplications are enough. Applied recursively this beats the n2n^2 method, and Python's own big integers use it for large values.

python · try it yourself
def karatsuba_step(x, y, B=10**4):
    a, b = divmod(x, B)
    c, d = divmod(y, B)
    ac, bd = a * c, b * d
    middle = (a + b) * (c + d) - ac - bd       # = ad + bc, one product instead of two
    return ac * B * B + middle * B + bd

x, y = 12345678, 87654321
print(karatsuba_step(x, y), x * y)
1082152022374638 1082152022374638

Exercises

Exercise 1: Expand

Expand (2x−3y)2(2x - 3y)^2.

Answer

4x2−12xy+9y24x^2 - 12xy + 9y^2

Solution
a=2x, b=3ya2−2ab+b2=4x2−2⋅2x⋅3y+9y2=4x2−12xy+9y2\begin{aligned} &a = 2x,\ b = 3y \\ &a^2 - 2ab + b^2 = 4x^2 - 2 \cdot 2x \cdot 3y + 9y^2 = 4x^2 - 12xy + 9y^2 \end{aligned}

Exercise 2: Difference of squares

Factor 50x2−850x^2 - 8 completely.

Answer

2(5x+2)(5x−2)2(5x + 2)(5x - 2)

Solution
50x2−8=2(25x2−4)common factor first=2((5x)2−22)=2(5x+2)(5x−2)\begin{aligned} 50x^2 - 8 &= 2(25x^2 - 4) && \text{common factor first} \\ &= 2((5x)^2 - 2^2) \\ &= 2(5x + 2)(5x - 2) \end{aligned}

Exercise 3: Cubes

Factor 8a3−1258a^3 - 125.

Answer

(2a−5)(4a2+10a+25)(2a - 5)(4a^2 + 10a + 25)

Solution
8a3=(2a)3, 125=53a3−b3=(a−b)(a2+ab+b2)with a→2a, b→5=(2a−5)(4a2+10a+25)\begin{aligned} &8a^3 = (2a)^3,\ 125 = 5^3 \\ &a^3 - b^3 = (a - b)(a^2 + ab + b^2) \quad \text{with } a \to 2a,\ b \to 5 \\ &= (2a - 5)(4a^2 + 10a + 25) \end{aligned}

Exercise 4: Mental math

Use a special product to compute 98298^2 and 102×98102 \times 98 without a calculator.

Answer

982=960498^2 = 9604, 102×98=9996102 \times 98 = 9996

Solution
982=(100−2)2=10 000−400+4=9604102×98=(100+2)(100−2)=10 000−4=9996\begin{aligned} 98^2 &= (100 - 2)^2 = 10\,000 - 400 + 4 = 9604 \\ 102 \times 98 &= (100 + 2)(100 - 2) = 10\,000 - 4 = 9996 \end{aligned}
python · try it yourself
print(98**2, 102 * 98)
9604 9996

Exercise 5: Perfect square or not?

Decide which are perfect squares and factor them: 4x2+12x+94x^2 + 12x + 9, x2+6x+36x^2 + 6x + 36, 25t2−20t+425t^2 - 20t + 4.

Answer

(2x+3)2(2x + 3)^2 and (5t−2)2(5t - 2)^2; x2+6x+36x^2 + 6x + 36 is not a perfect square (it would need 12x12x).

Solution
4x2+12x+9:(2x)2, 32, 2⋅2x⋅3=12x ✓x2+6x+36:x2, 62, 2⋅x⋅6=12x≠6x ×25t2−20t+4:(5t)2, 22, 2⋅5t⋅2=20t, minus sign ✓\begin{aligned} 4x^2 + 12x + 9{:}\quad &(2x)^2,\ 3^2,\ 2 \cdot 2x \cdot 3 = 12x\ \checkmark \\ x^2 + 6x + 36{:}\quad &x^2,\ 6^2,\ 2 \cdot x \cdot 6 = 12x \ne 6x\ \boldsymbol{\times} \\ 25t^2 - 20t + 4{:}\quad &(5t)^2,\ 2^2,\ 2 \cdot 5t \cdot 2 = 20t,\ \text{minus sign}\ \checkmark \end{aligned}

Test yourself

Expand (x+5)2(x + 5)^2.

  1. x2+25x^2 + 25
  2. x2+5x+25x^2 + 5x + 25
  3. x2+10x+25x^2 + 10x + 25
  4. 2x+102x + 10

Factor 36−y236 - y^2.

  1. (6−y)2(6 - y)^2
  2. (6+y)(6−y)(6 + y)(6 - y)
  3. (y−6)(y+6)(y - 6)(y + 6)
  4. It does not factor

Which factorization is correct?

  1. x3+1=(x+1)(x2−x+1)x^3 + 1 = (x + 1)(x^2 - x + 1)
  2. x3+1=(x+1)(x2+x+1)x^3 + 1 = (x + 1)(x^2 + x + 1)
  3. x3+1=(x+1)3x^3 + 1 = (x + 1)^3
  4. x3+1=(x−1)(x2+x+1)x^3 + 1 = (x - 1)(x^2 + x + 1)