Dividing Polynomials
Dividing polynomials works like long division of whole numbers: divide, multiply, subtract, bring down, repeat, until what is left is "smaller" than the divisor. The result is a quotient and a remainder. This lesson does the division by hand, then implements both polynomial long division and the shortcut called synthetic division in Python, and ends with the remainder theorem and the CRC checksums that protect network packets.
Dividing by a monomial
Divide each term separately and subtract exponents:
Long division
Divide by . Each round looks only at the leading terms:
2x² + x + 6
┌───────────────────────
x − 2 │ 2x³ − 3x² + 4x − 5
2x³ − 4x² 2x³ ÷ x = 2x²; 2x²·(x − 2)
─────────
x² + 4x subtract, bring down 4x
x² − 2x x² ÷ x = x; x·(x − 2)
───────
6x − 5 subtract, bring down −5
6x − 12 6x ÷ x = 6; 6·(x − 2)
───────
7 remainderSo . In general, for a dividend and divisor :
Tip: Write every missing power with a 0 coefficient before you start ( becomes ). Otherwise the columns slip and the subtraction goes wrong.
Long division in Python
The algorithm only touches coefficients. The version below stores them highest power first, like the hand layout, and uses Fraction so that a leading coefficient other than 1 gives exact answers:
from fractions import Fraction
def poly_divmod(p, d):
"""p, d: coefficients, highest power first. Returns (quotient, remainder)."""
p = [Fraction(c) for c in p]
q = []
while len(p) >= len(d):
factor = p[0] / d[0] # divide the leading terms
q.append(factor)
for i in range(len(d)): # subtract factor · d, lined up on the left
p[i] -= factor * d[i]
p.pop(0) # the leading term is now 0
return q, p or [Fraction(0)]
def show(c):
return [str(x) for x in c]
q, r = poly_divmod([2, -3, 4, -5], [1, -2])
print("quotient:", show(q), " remainder:", show(r))
q, r = poly_divmod([6, 5, -4, 7], [2, 1, -1])
print("quotient:", show(q), " remainder:", show(r))quotient: ['2', '1', '6'] remainder: ['7']
quotient: ['3', '1'] remainder: ['-2', '8']
The second line says has quotient and remainder . Check: , and adding gives back the dividend.
Synthetic division
When the divisor is , the long division can be squeezed into one row of numbers. Write on the left and the coefficients on top. Bring down the first coefficient, then repeatedly multiply by and add to the next column:
2 │ 2 −3 4 −5
│ 4 2 12
└───────────────────
2 1 6 │ 7The bottom row gives the quotient and, after the bar, the remainder 7: the same as the long division, with far less writing. For a divisor , use .
def synthetic(coeffs, r):
"""Divide by (x - r). coeffs highest power first."""
row = [coeffs[0]]
for c in coeffs[1:]:
row.append(c + r * row[-1]) # multiply by r, add
return row[:-1], row[-1] # quotient, remainder
print(synthetic([2, -3, 4, -5], 2))
print(synthetic([1, 0, 0, -8], 2)) # x³ − 8 by x − 2
print(synthetic([2, 3, -11, -6], 2)) # remainder 0: x − 2 is a factor([2, 1, 6], 7)
([1, 2, 4], 0)
([2, 7, 3], 0)
The loop "multiply by , add the next coefficient" is exactly Horner's rule for evaluating a polynomial at . That is no coincidence.
The remainder and factor theorems
Dividing by leaves a constant remainder , and . Put in: the first part vanishes, so
Check: ✓. And since gives remainder 0 at , it factors as . Finding one root and dividing it out is the standard way to break a cubic down to a quadratic, which you can then factor or solve with the formula.
SymPy has both operations built in:
import sympy as sp
x = sp.symbols("x")
q, r = sp.div(6*x**3 + 5*x**2 - 4*x + 7, 2*x**2 + x - 1, x)
print("q =", q, " r =", r)
print(sp.factor(2*x**3 + 3*x**2 - 11*x - 6))q = 3*x + 1 r = 8 - 2*x
(x - 2)*(x + 3)*(2*x + 1)
In software: CRC checksums
Ethernet frames, ZIP files and PNG images carry a CRC (cyclic redundancy check). The sender treats the message bits as coefficients of a polynomial, divides it by a fixed generator polynomial, and appends the remainder. Arithmetic is modulo 2, where subtraction is XOR, so the long division becomes "XOR the generator in wherever the leading bit is 1". The receiver repeats the division: a nonzero remainder means the data was corrupted.
def crc_remainder(bits, gen):
"""Polynomial long division over GF(2). bits, gen: strings of 0/1."""
work = list(map(int, bits + "0" * (len(gen) - 1))) # make room for the remainder
g = list(map(int, gen))
for i in range(len(bits)):
if work[i]: # leading coefficient is 1
for j in range(len(g)):
work[i + j] ^= g[j] # subtract = XOR
return "".join(map(str, work[-(len(gen) - 1):]))
msg, gen = "1101011", "1011" # generator x³ + x + 1
crc = crc_remainder(msg, gen)
print("CRC:", crc)
print("check sent frame:", crc_remainder(msg + crc, gen)[-3:] == "000")CRC: 110
check sent frame: True
Real CRCs (CRC-32 uses a degree-32 generator) work the same way, just with table lookups to process a byte at a time (hashing explains why a CRC detects accidents but is no defence against attackers).
Exercises
Exercise 1: By a monomial
Simplify .
Answer
Solution
Exercise 2: Synthetic division
Divide by .
Answer
Quotient , remainder 0.
Solution
Use :
−1 │ 1 4 −7 −10
│ −1 −3 10
└─────────────────
1 3 −10 │ 0Since the remainder is 0, is a factor, and .
Exercise 3: Remainder without dividing
Find the remainder when is divided by .
Answer
−3
Solution
By the remainder theorem it is .
def synthetic(coeffs, r):
row = [coeffs[0]]
for c in coeffs[1:]:
row.append(c + r * row[-1])
return row[:-1], row[-1]
print(synthetic([1, -3, 0, 2, -9], 3))([1, 0, 0, 2], -3)
Exercise 4: Long division with a quadratic divisor
Divide by .
Answer
Quotient , remainder .
Solution
import sympy as sp
x = sp.symbols("x")
print(sp.div(x**4 + 2*x**3 - x + 5, x**2 + 1, x))(x**2 + 2*x - 1, 6 - 3*x)
Exercise 5: Find k
For which is a factor of ?
Answer
Solution
By the factor theorem : , so and . Check: .
Test yourself
What is the remainder of ?
- 0
- 1
- −1
- 2
When dividing by with synthetic division, which number goes in the box?
- 4
- 1
- −4
- 1/4
A degree-5 polynomial is divided by a degree-2 polynomial. What are the possible degrees of the remainder?
- Exactly 2
- 1 or 0 (or the remainder is 0)
- 3
- Any degree up to 5