Factoring Out a Common Factor
Factoring is multiplying in reverse: you start with a sum like and rewrite it as a product, . The first thing to try, always, is pulling out the greatest common factor (GCF) of all the terms. This lesson shows how to find it, how to factor by grouping when there is no single common factor, and how SymPy does the same job.
Why factor?
A product tells you things a sum hides. From you can read at once that the expression is zero when or , and that it is a multiple of 3. Factored forms also simplify algebraic fractions and solve quadratic equations.
Finding the GCF of terms
Take the GCF of the coefficients (GCF and LCM) and, for each variable that appears in every term, the lowest power of it.
Then divide each term by the GCF to find what stays in the parentheses:
Tip: Check by multiplying back out. Also check that nothing common is left inside the parentheses: multiplies back correctly but is not fully factored.
When a term equals the GCF itself, a 1 stays behind: , not .
Factoring out a negative
If the leading coefficient is negative, it is common to factor out the negative GCF, which flips every sign inside:
This is the form you want for projectile height formulas like : the factors show at once that the height is 0 at and .
The common factor can be a binomial
The GCF does not have to be a monomial. In the common factor is the whole group :
Factoring by grouping
A four-term polynomial often has no factor common to all four terms, but splits into two pairs that do share one:
If the two brackets do not match, try regrouping or factoring a negative out of the second pair: . Grouping is also the engine of the AC method for trinomials.
In Python
Finding the GCF of the coefficients is math.gcd; for the variables, take the minimum exponent. With terms stored as (coefficient, exponent) pairs:
from math import gcd
from functools import reduce
terms = [(12, 4), (-18, 3), (30, 2)] # 12x⁴ − 18x³ + 30x²
g = reduce(gcd, (abs(c) for c, _ in terms))
low = min(e for _, e in terms)
inside = [(c // g, e - low) for c, e in terms]
print(f"GCF = {g}x^{low}")
print("inside:", inside)
print("check:", [(c * g, e + low) for c, e in inside] == terms)GCF = 6x^2
inside: [(2, 2), (-3, 1), (5, 0)]
check: True
So . SymPy's factor finds the GCF and much more; factor_terms pulls out only the common factor:
import sympy as sp
x, y = sp.symbols("x y")
print(sp.factor_terms(12*x**4*y**2 - 18*x**3*y + 30*x**2*y**3))
print(sp.factor(x**3 - 4*x**2 + 3*x - 12))
print(sp.factor(-5*x**2 + 20*x))6*x**2*y*(2*x**2*y - 3*x + 5*y**2)
(x - 4)*(x**2 + 3)
-5*x*(x - 4)
In software
- Fewer operations. costs two multiplications, one. Compilers apply this rewrite (and its reverse) when optimizing arithmetic, and the same idea, applied over and over, gives Horner's rule: .
- Refactoring. Pulling the shared part of several functions into one helper is factoring out a common factor in code.
- Lower precision risk. Computing instead of can save a rounding step in floating point, and grouping terms of similar size is a standard trick in numerical code.
Exercises
Exercise 1: Monomial GCF
Factor .
Answer
Solution
Exercise 2: Two variables
Factor .
Answer
Solution
Exercise 3: Grouping
Factor .
Answer
Solution
import sympy as sp
x = sp.symbols("x")
print(sp.factor(6*x**3 + 9*x**2 - 4*x - 6))(2*x + 3)*(3*x**2 - 2)
Exercise 4: Zeros from the factors
The height of a ball is metres after seconds. Factor the expression and say when the ball is at height 0.
Answer
; height 0 at s (launch) and s (landing).
Solution
The GCF is (negative because the leading coefficient is negative). , . A product is zero when a factor is zero: or .
Exercise 5: GCF of a whole list
Extend the code above to handle the case where the leading coefficient is negative: make the GCF negative then, so the first term inside is positive. Test on .
Answer
GCF , inside .
Solution
from math import gcd
from functools import reduce
def common_factor(terms):
g = reduce(gcd, (abs(c) for c, _ in terms))
if terms[0][0] < 0:
g = -g
low = min(e for _, e in terms)
return (g, low), [(c // g, e - low) for c, e in terms]
print(common_factor([(-6, 3), (9, 2), (-3, 1)]))((-3, 1), [(2, 2), (-3, 1), (1, 0)])
Test yourself
What is the GCF of and ?
Factor completely.
Factor by grouping: .