BRAGA

02 Writing02-02 Notes02-02-076

Difference of squares

Published 16 Apr 202610 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
a2−b2=(a−b)(a+b)a^2 - b^2 = (a - b)(a + b)
(1)

Deep Breakdown

What it tells me (Situation Awareness): The difference of squares identity is one of the most frequently used algebraic factoring formulas. It states that the difference of two perfect squares factors into the product of two conjugate binomials: (a−b)(a - b) and (a+b)(a + b). Crucially, the middle term cancels: (a−b)(a+b)=a2+ab−ab−b2=a2−b2(a-b)(a+b) = a^2 + ab - ab - b^2 = a^2 - b^2. In DA/DS, this appears in: simplifying rational expressions (canceling (x−a)(x-a) or (x+a)(x+a) factors), matrix algebra (A2−B2=(A−B)(A+B)A^2 - B^2 = (A-B)(A+B) when AB=BAAB = BA), loss function algebra (squared error decompositions), solving quadratic equations (x2−c2=0⇒x=±cx^2 - c^2 = 0 \Rightarrow x = \pm c), computing the variance as Var(X)=E[X2]−(E[X])2=E[X2]−μ2\text{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 = \mathbb{E}[X^2] - \mu^2, and simplifying terms involving squared distances in distance/similarity computations. The formula is the degree-2 case of the general identity an−bn=(a−b)(an−1+an−2b+⋯+bn−1)a^n - b^n = (a-b)(a^{n-1} + a^{n-2}b + \cdots + b^{n-1}).

Flowchart

Expression: a² -b²Recognize asdifference of twosquaresFactor: (a - b)(a+ b)Linear factor 1:(a - b) — zerowhen a = bLinear factor 2:(a + b) — zerowhen a = -bTwo real roots: a= b and a = -bDA: Var(X) =E[X²] - (E[X])² =E[X²] - μ²DA: cancel (x-a)in rationalexpressionsDA: (x-a)(x+a)pattern indistanceformulasTRAP: (a-b)² ≠a²-b² — missingmiddle term!

Real-world use

Where it is used

Variance formula derivation: Var(X)=E[(X−μ)2]=E[X2]−μ2=E[X2]−(E[X])2\text{Var}(X) = \mathbb{E}[(X - \mu)^2] = \mathbb{E}[X^2] - \mu^2 = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 — this is a difference of squares in the form E[X2]−(E[X])2\mathbb{E}[X^2] - (\mathbb{E}[X])^2. Simplifying rational expressions: x2−9x−3=(x−3)(x+3)x−3=x+3\frac{x^2 - 9}{x - 3} = \frac{(x-3)(x+3)}{x-3} = x + 3. Solving quadratic equations: x2=c2⇒x2−c2=0⇒(x−c)(x+c)=0⇒x=±cx^2 = c^2 \Rightarrow x^2 - c^2 = 0 \Rightarrow (x-c)(x+c) = 0 \Rightarrow x = \pm c. Computing squared differences in distance metrics: ∥a∥2−∥b∥2=(∥a∥−∥b∥)(∥a∥+∥b∥)\|a\|^2 - \|b\|^2 = (\|a\| - \|b\|)(\|a\| + \|b\|).

Problem it solves

a2−b2a^2 - b^2 appears frequently but is not immediately factorable by inspection unless you know the identity. Without it, simplifying rational expressions like x2−4x+2\frac{x^2 - 4}{x + 2} requires either polynomial long division or trial factoring. The identity makes the factoring instantaneous: x2−4=(x−2)(x+2)x^2 - 4 = (x-2)(x+2), so (x−2)(x+2)x+2=x−2\frac{(x-2)(x+2)}{x+2} = x - 2.

How it solves the problem

The identity is verified by expansion: (a−b)(a+b)=a2+ab−ab−b2=a2−b2(a-b)(a+b) = a^2 + ab - ab - b^2 = a^2 - b^2. The +ab+ab and −ab-ab middle terms cancel exactly because the two factors are conjugates (they differ only in the sign of bb). This cancellation is why there is no middle term in a2−b2a^2 - b^2 — unlike the expanded square (a±b)2=a2±2ab+b2(a \pm b)^2 = a^2 \pm 2ab + b^2, which has a middle term.

Example in a business

A data analyst computes variance using the computational formula. For a batch of revenue observations with n=1000n = 1000: rather than computing ∑(xi−xˉ)2\sum (x_i - \bar{x})^2 directly (two passes over the data), they use Var(X)=∑xi2n−(∑xin)2=E[X2]−(E[X])2\text{Var}(X) = \frac{\sum x_i^2}{n} - \left(\frac{\sum x_i}{n}\right)^2 = \mathbb{E}[X^2] - (\mathbb{E}[X])^2. This is a2−b2=(a−b)(a+b)a^2 - b^2 = (a-b)(a+b) applied to the mean-of-squares and square-of-mean, allowing computation in a single pass.

The formula, piece by piece

Breaking the notation:

  • a2−b2a^2 - b^2: the difference of two perfect squares — requires both terms to be squares (or expressible as squares).
  • aa: the square root of the first term.
  • bb: the square root of the second term.
  • (a−b)(a-b): the "difference" factor — zero when a=ba = b.
  • (a+b)(a+b): the "sum" factor — zero when a=−ba = -b.
  • =(a−b)(a+b)= (a-b)(a+b): both factors are linear, producing two real roots. Two roots of a2=b2a^2 = b^2: a=ba = b and a=−ba = -b — the two solutions correspond to the two factors. Common forms:
  • x2−c2=(x−c)(x+c)x^2 - c^2 = (x-c)(x+c) — roots at x=±cx = \pm c.
  • x2−1=(x−1)(x+1)x^2 - 1 = (x-1)(x+1) — roots at x=±1x = \pm 1.
  • (f(x))2−(g(x))2=(f(x)−g(x))(f(x)+g(x))(f(x))^2 - (g(x))^2 = (f(x) - g(x))(f(x) + g(x)) — works for any expressions f,gf, g. Variance form: E[X2]−(E[X])2=(E[X2]−μ2)\mathbb{E}[X^2] - (\mathbb{E}[X])^2 = (\mathbb{E}[X^2] - \mu^2) — here a=E[X2]a = \sqrt{\mathbb{E}[X^2]} and b=μb = \mu conceptually, though the algebraic form applies directly at the expression level. NOT the same as (a−b)2(a-b)^2: (a−b)2=a2−2ab+b2≠a2−b2(a-b)^2 = a^2 - 2ab + b^2 \ne a^2 - b^2.

This is the most common confusion. The difference of squares has no middle term; the squared difference has a −2ab-2ab middle term.

The formula in words

"a2−b2a^2 - b^2 factors into (a−b)(a+b)(a-b)(a+b) — the product of the difference and the sum. No middle term: the two factors are conjugates, and when you multiply them, the cross terms +ab+ab and −ab-ab cancel exactly. Use this whenever you see a squared quantity minus another squared quantity. In Python: (a - b) * (a + b) == a**2 - b**2 is always True."

Critical Concepts

  1. Not the same as (a−b)2(a-b)^2: a2−b2≠(a−b)2a^2 - b^2 \ne (a-b)^2. The difference of squares has NO middle term. The squared difference has middle term −2ab-2ab. This is the single most common algebra error with these forms.
  2. Variance computational formula: Var(X)=E[X2]−(E[X])2\text{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 is the most-used form of this identity in statistics. It reduces two-pass variance computation to one pass: accumulate ∑xi\sum x_i and ∑xi2\sum x_i^2 simultaneously.
  3. Works for any "squares": aa and bb don't have to be simple variables. f(x)2−g(x)2=(f(x)−g(x))(f(x)+g(x))f(x)^2 - g(x)^2 = (f(x)-g(x))(f(x)+g(x)) for any functions f,gf, g. Example: sin⁡2θ−cos⁡2θ=(sin⁡θ−cos⁡θ)(sin⁡θ+cos⁡θ)\sin^2\theta - \cos^2\theta = (\sin\theta - \cos\theta)(\sin\theta + \cos\theta).
  4. Rational expression simplification: a2−b2a+b=a−b\frac{a^2 - b^2}{a + b} = a - b (for a≠−ba \ne -b) and a2−b2a−b=a+b\frac{a^2 - b^2}{a - b} = a + b (for a≠ba \ne b). This is a standard simplification step in limits, derivatives from first principles, and algebra.
  5. Numerical identity for quick mental math: 53×47=(50+3)(50−3)=502−32=2500−9=249153 \times 47 = (50+3)(50-3) = 50^2 - 3^2 = 2500 - 9 = 2491.

More generally, products of numbers that are equidistant from a round number can be computed as a difference of squares.

Step-by-step calculation (tiny example)

Task: compute the variance of XX using the computational formula, given E[X2]=29\mathbb{E}[X^2] = 29 and E[X]=5\mathbb{E}[X] = 5.

Step 1: Identify the form Var(X)=E[X2]−(E[X])2=a2−b2\text{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2 = a^2 - b^2 where a=29a = \sqrt{29} is conceptual; the direct form is 29−5229 - 5^2.

Step 2: Apply Var(X)=29−25=4\text{Var}(X) = 29 - 25 = 4.

Step 3: Cross-check If E[X]=5\mathbb{E}[X] = 5 and Var(X)=4\text{Var}(X) = 4, then SD(X)=2\text{SD}(X) = 2.

Step 4: Factored interpretation E[X2]−μ2=(E[X2]−μ)(E[X2]+μ)\mathbb{E}[X^2] - \mu^2 = (\sqrt{\mathbb{E}[X^2]} - \mu)(\sqrt{\mathbb{E}[X^2]} + \mu) — algebraically, but in practice the direct computation 29−25=429 - 25 = 4 is cleaner.

Step 5: Verify with tiny dataset X∈{3,5,7}X \in \{3, 5, 7\}: Xˉ=5\bar{X} = 5, E[X2]=(9+25+49)/3=83/3≈27.67\mathbb{E}[X^2] = (9+25+49)/3 = 83/3 \approx 27.67, Var(X)=83/3−25=(83−75)/3=8/3≈2.67\text{Var}(X) = 83/3 - 25 = (83-75)/3 = 8/3 \approx 2.67. Formula gives the same result.

Code examples

Beginner example (Python)

python
# Difference of squares: a² - b² = (a-b)(a+b)
# Basic verification
pairs = [(5, 3), (10, 4), (7, 2), (0.5, 0.25), (-3, 2)]
print(f"{'a':>6} {'b':>6} {'a²-b²':>8} {'(a-b)(a+b)':>12} {'match':>6}")
for a, b in pairs:
    lhs = a**2 - b**2
    rhs = (a - b) * (a + b)
    print(f"{a:>6.2f} {b:>6.2f} {lhs:>8.4f} {rhs:>12.4f} {str(abs(lhs-rhs)<1e-10):>6}")

# TRAP: contrast with (a-b)²
a, b = 5.0, 3.0
print(f"\nTRAP comparison for a={a}, b={b}:")
print(f" a²-b² = {a**2-b**2:.1f} (difference of squares = (a-b)(a+b))")
print(f" (a-b)² = {(a-b)**2:.1f} (squared difference = a²-2ab+b²)")
print(f" They are NOT equal: {a**2-b**2 != (a-b)**2}")

# Variance computational formula
import numpy as np
np.random.seed(42)
data = np.array([3.0, 7.0, 5.0, 9.0, 1.0, 11.0, 4.0, 8.0])

# Two methods — should be equal
var_direct = np.mean((data - data.mean())**2)   # E[(X - μ)²]
var_formula = np.mean(data**2) - data.mean()**2   # E[X²] - μ² (diff of squares)
print(f"\nVariance computation:")
print(f" E[X²] = {np.mean(data**2):.4f} μ² = {data.mean()**2:.4f}")
print(f" Var (direct): {var_direct:.6f}")
print(f" Var (formula): {var_formula:.6f}")
print(f" Match: {abs(var_direct - var_formula) < 1e-10}")

# Mental math trick: a×b = (m-d)(m+d) = m² - d²
m, d = 50, 3
print(f"\nMental math: {m-d} × {m+d} = {m}² - {d}² = {m**2} - {d**2} = {m**2 - d**2}")

Beginner example (R)

r
# Difference of squares in R # Verification
pairs <- list(c(5,3), c(10,4), c(7,2), c(0.5,0.25))
cat(sprintf("%-6s %-6s %-8s %-12s %s\n", "a", "b", "a²-b²", "(a-b)(a+b)", "match"))
for (p in pairs) { a <- p[1]; b <- p[2]
  lhs <- a^2 - b^2
  rhs <- (a - b) * (a + b)
  cat(sprintf("%-6.2f %-6.2f %-8.4f %-12.4f %s\n", a, b, lhs, rhs, abs(lhs-rhs)<1e-10))
}  # TRAP
a <- 5; b <- 3
cat(sprintf("\nTRAP: a²-b² = %.0f vs (a-b)² = %.0f — NOT equal!\n", a^2 - b^2, (a-b)^2))  # Variance: E[X²] - μ²
data <- c(3, 7, 5, 9, 1, 11, 4, 8)
var_direct <- mean((data - mean(data))^2)
var_formula <- mean(data^2) - mean(data)^2
cat(sprintf("\nVariance:\n"))
cat(sprintf(" Direct: %.6f\n", var_direct))
cat(sprintf(" Formula: %.6f\n", var_formula))
cat(sprintf(" Match: %s\n", abs(var_direct - var_formula) < 1e-10))

Professional example (Python)

python
import numpy as np
import pandas as pd

# Scenario: online (single-pass) variance computation using E[X²] - (E[X])²
# Demonstrates the difference-of-squares form of variance in a streaming context
# Critical for large datasets where two-pass computation is expensive
np.random.seed(42)
N_STREAM = 100_000  # simulate streaming observations


class OnlineVarianceTracker:
    """Single-pass online variance using the computational formula.

    Var(X) = E[X²] - (E[X])² — the difference-of-squares identity.
    Numerically uses Welford's algorithm for stability, but also tracks
    the naive formula for illustration.
    """

    def __init__(self) -> None:
        self._n: int = 0
        self._sum: float = 0.0
        self._sum_sq: float = 0.0

    def update(self, x: float) -> None:
        """Accumulate a new observation."""
        self._n += 1
        self._sum += x
        self._sum_sq += x * x

    @property
    def mean(self) -> float:
        """Current running mean E[X]."""
        return self._sum / self._n if self._n > 0 else 0.0

    @property
    def variance(self) -> float:
        """Variance using E[X²] - (E[X])² (difference of squares formula)."""
        if self._n < 2:
            return 0.0
        e_x2 = self._sum_sq / self._n
        mu = self.mean
        return e_x2 - mu**2  # a² - b² form


# Stream data
true_mean = 50.0
true_std = 10.0
tracker = OnlineVarianceTracker()
snapshots = []
for i, x in enumerate(np.random.normal(true_mean, true_std, N_STREAM)):
    tracker.update(float(x))
    if (i + 1) in {100, 1_000, 10_000, N_STREAM}:
        snapshots.append({
            "n": i + 1,
            "mean": tracker.mean,
            "variance": tracker.variance,
            "std": tracker.variance**0.5,
            "true_mean": true_mean,
            "true_var": true_std**2,
        })

report = pd.DataFrame(snapshots)
print("Online Variance via E[X²] - (E[X])² (Difference-of-Squares Formula)")
print("=" * 72)
print(report.round(4).to_string(index=False))
print(f"\nFormula: Var(X) = E[X²] - μ² = (a²-b²) with a=√E[X²], b=μ")
print(f"True variance = {true_std**2:.1f} | Converges as n → ∞")

Professional example (R)

r
# Online variance via E[X²] - (E[X])² (difference-of-squares formula)
set.seed(42)
N_STREAM <- 10000
true_mean <- 50
true_std <- 10
x_stream <- rnorm(N_STREAM, true_mean, true_std)
online_var <- function(x, check_at = c(100, 1000, 10000, N_STREAM)) {
  n <- length(x)
  sum_x <- 0
  sum_sq <- 0
  out <- list()
  for (i in seq_len(n)) {
    sum_x <- sum_x + x[i]
    sum_sq <- sum_sq + x[i]^2
    if (i %in% check_at) {
      mu <- sum_x / i
      e_x2 <- sum_sq / i
      var_x <- e_x2 - mu^2
      out[[length(out) + 1]] <- data.frame(n = i, mean = mu, variance = var_x, std = sqrt(var_x), true_mean = true_mean, true_var = true_std^2)
    }
  }
  do.call(rbind, out)
}
report <- online_var(x_stream)
cat("Online Variance via E[X²] - (E[X])² (Difference-of-Squares Formula)\n")
print(round(report, 4))
cat(sprintf("\nTrue variance = %.1f | Converges as n → ∞\n", true_std^2))

Source: 1300 p. 20

#algebra#algebra-and-factoring#factoring-and-product-formulas

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close