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 | ||
| square of a difference | ||
| difference of squares | ||
| sum of cubes | ||
| difference of cubes |
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 , because the middle terms cancel.
Note: is not . With , : , while . The missing 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:
( still factors, as , with the usual method.) Perfect squares are the heart of completing the square.
Difference of squares
Any expression of the form factors at once:
A sum of squares such as 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: .
Sum and difference of cubes
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
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 is returned unchanged.
In software
Accurate . When and 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:
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, , the schoolbook way needs four products . Karatsuba notices that , so three multiplications are enough. Applied recursively this beats the method, and Python's own big integers use it for large values.
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 .
Answer
Solution
Exercise 2: Difference of squares
Factor completely.
Answer
Solution
Exercise 3: Cubes
Factor .
Answer
Solution
Exercise 4: Mental math
Use a special product to compute and without a calculator.
Answer
,
Solution
print(98**2, 102 * 98)9604 9996
Exercise 5: Perfect square or not?
Decide which are perfect squares and factor them: , , .
Answer
and ; is not a perfect square (it would need ).
Solution
Test yourself
Expand .
Factor .
- It does not factor
Which factorization is correct?