Factoring
A quadratic trinomial such as or can often be written as a product of two binomials. This lesson starts with the easy case where the leading coefficient is 1, then teaches the AC method, a step-by-step recipe that works for any leading coefficient, and finally writes that recipe as a small Python function.
Case : find two numbers
Multiplying out gives . So to factor you need two numbers whose
- product is , and
- sum is .
The signs tell you where to look:
| the two numbers | ||
|---|---|---|
| positive | positive | both positive |
| positive | negative | both negative |
| negative | either | opposite signs; the larger one (in size) has the sign of |
Tip: List the factor pairs of in order (, , ) and stop at the pair with the right sum. If none works, the trinomial does not factor over the integers. That is fine: the quadratic formula still solves the equation.
The AC method for any
When , guessing gets slow. The AC method turns the problem into factoring by grouping:
- Multiply .
- Find two numbers and with and .
- Split the middle term: .
- Factor the four terms by grouping.
Example: .
Why does it work? If the answer is , then , and . The two numbers and add to and multiply to . Step 2 finds exactly those two numbers, and grouping separates them back into the two factors.
Note: Always take out a common factor first. . Skipping this gives larger numbers in step 2 and an answer that is not fully factored.
The AC method in Python
The search in step 2 is the only hard part, and a computer can just try every divisor of :
from math import gcd
def ac_factor(a, b, c):
ac = a * c
for m in range(-abs(ac), abs(ac) + 1):
if m != 0 and ac % m == 0 and m + ac // m == b:
n = ac // m
# group: (ax² + mx) + (nx + c); each pair's GCF (sign from its first term)
g1 = gcd(a, m) * (1 if a > 0 else -1)
g2 = gcd(n, c) * (1 if n > 0 else -1)
# a x² + m x = g1·x·(a/g1 · x + m/g1); n x + c = g2·(n/g2 · x + c/g2)
return (g1, g2), (a // g1, m // g1)
return None
def show(a, b, c):
r = ac_factor(a, b, c)
if r is None:
print(f"{a}x² + {b}x + {c}: no integer factorization")
else:
(g1, g2), (p, q) = r
print(f"{a}x² + {b}x + {c} = ({g1}x + {g2})({p}x + {q})")
show(6, 11, -10)
show(1, 7, 12)
show(2, -7, 3)
show(1, 1, 1)6x² + 11x + -10 = (2x + 5)(3x + -2)
1x² + 7x + 12 = (1x + 4)(1x + 3)
2x² + -7x + 3 = (2x + -1)(1x + -3)
1x² + 1x + 1: no integer factorization
The printing is crude (+ -2 means ), but the factors are right: , and . Because the leftover bracket after grouping is the same for both pairs, the code only needs the first pair's bracket and the two GCFs.
SymPy factors any polynomial with integer coefficients, so it is a convenient checker:
import sympy as sp
x = sp.symbols("x")
for expr in [6*x**2 + 11*x - 10, 4*x**2 + 10*x - 6, 12*x**2 - 17*x + 6, x**2 + x + 1]:
print(expr, " => ", sp.factor(expr))6*x**2 + 11*x - 10 => (2*x + 5)*(3*x - 2)
4*x**2 + 10*x - 6 => 2*(x + 3)*(2*x - 1)
12*x**2 - 17*x + 6 => (3*x - 2)*(4*x - 3)
x**2 + x + 1 => x**2 + x + 1
comes back unchanged: it has no factors with rational coefficients (its roots are complex, see The Discriminant and Complex Roots).
In software
- Root finding with exact numbers. Computer algebra systems factor polynomials over the integers to find exact rational roots before falling back on floating-point methods.
- Quick test. factors over the integers exactly when is a perfect square.
math.isqrt(d)**2 == dchecks that without any floating-point error. - Search problems. The AC search ("two numbers with a given product and sum") is a tiny instance of the two-sum problem from two pointers: with the divisors sorted, one pass from both ends finds the pair.
Exercises
Exercise 1: Leading coefficient 1
Factor .
Answer
Solution
Product −24, sum 2. Pairs of opposite sign: , , , . Only .
Exercise 2: Both negative
Factor .
Answer
Solution
Product +36, sum −13, so both numbers are negative. and .
Exercise 3: AC method
Factor .
Answer
Solution
Exercise 4: Common factor first
Factor completely.
Answer
Solution
Exercise 5: Which ones factor?
Use the perfect-square test to decide which of , and factor over the integers. Write the check in Python.
Answer
() and () factor; () does not.
Solution
from math import isqrt
for a, b, c in [(2, 5, 3), (1, 3, 1), (6, -1, -15)]:
d = b * b - 4 * a * c
ok = d >= 0 and isqrt(d) ** 2 == d
print((a, b, c), "D =", d, "factors" if ok else "does not factor")(2, 5, 3) D = 1 factors
(1, 3, 1) D = 5 does not factor
(6, -1, -15) D = 361 factors
The factorizations are and .
Test yourself
Factor .
In the AC method for , which two numbers split the middle term?
- 2 and 4
- 4 and 5
- 1 and 8
- 3 and 6
For a trinomial with , the two numbers are…
- both positive
- both negative
- of opposite signs
- equal