Algorithm Search⌘K

Proofs: Polynomial long division

These are the human proofs of Polynomial long division, at full length.

Long division returns the unique quotient and remainder full

This proof shows: correctness.

Let RR be a commutative ring, f,g∈R[x]f, g \in R[x], g≠0g \ne 0, and let the leading coefficient of gg be a unit. After line L1, f=∑i=0mfixif = \sum_{i=0}^{m} f_i x^i and g=∑j=0ngjxjg = \sum_{j=0}^{n} g_j x^j with gn≠0g_n \ne 0, and u=gn−1u = g_n^{-1} after line L3. For a coefficient array rr we write rr also for the polynomial ∑irixi\sum_i r_i x^i.

Case m<nm < n. Line L2 returns (0,f)(0, f). Then f=0⋅g+ff = 0 \cdot g + f and deg⁡f<n\deg f < n. This also covers f=0f = 0, where m=−1m = -1.

Case m≥nm \ge n. We use this invariant of the loop of line L5. Before the step for kk, with −1≤k≤m−n-1 \le k \le m - n (the value k=−1k = -1 stands for the state after the last step):

(I1)f=Qk g+r,Qk=∑i=k+1m−nqixi,(I2)ri=0 for all i>n+k.\text{(I1)}\quad f = Q_k\, g + r, \qquad Q_k = \sum_{i = k+1}^{m-n} q_i x^i, \qquad\qquad \text{(I2)}\quad r_i = 0 \text{ for all } i > n + k .

Initialization. Before the step for k=m−nk = m - n, line L4 gives r=fr = f and Qm−n=0Q_{m-n} = 0, so (I1) holds. (I2) says ri=0r_i = 0 for i>mi > m, which is true because r=fr = f has degree mm.

Maintenance. Assume (I1) and (I2) before the step for kk. Line L6 sets c=rn+kuc = r_{n+k} u, and line L7 sets qk=cq_k = c, so Qk−1=Qk+c xkQ_{k-1} = Q_k + c\, x^k. Lines L8 and L9 replace rj+kr_{j+k} by rj+k−c gjr_{j+k} - c\, g_j for 0≤j≤n−10 \le j \le n - 1. Line L10 replaces rn+kr_{n+k} by 00. Since

rn+k−c gn=rn+k−rn+k u gn=0,r_{n+k} - c\, g_n = r_{n+k} - r_{n+k}\, u\, g_n = 0,

line L10 writes the value rn+k−c gnr_{n+k} - c\, g_n. So the step replaces rj+kr_{j+k} by rj+k−c gjr_{j+k} - c\, g_j for each 0≤j≤n0 \le j \le n and does not change the other entries, that is, it replaces the polynomial rr by r′=r−c xkgr' = r - c\, x^k g. Then

Qk−1 g+r′=(Qk+c xk)g+r−c xkg=Qk g+r=f,Q_{k-1}\, g + r' = (Q_k + c\, x^k) g + r - c\, x^k g = Q_k\, g + r = f,

which is (I1) for k−1k - 1. For (I2) at k−1k - 1 we need ri′=0r'_i = 0 for i>n+k−1i > n + k - 1. For i>n+ki > n + k, the step does not change rir_i, which is 00 by (I2). For i=n+ki = n + k, line L10 sets rn+k′=0r'_{n+k} = 0.

Termination. The loop runs exactly m−n+1m - n + 1 times. After the step for k=0k = 0, (I1) and (I2) with k=−1k = -1 give

f=q g+r,q=∑i=0m−nqixi,ri=0 for i≥n.f = q\, g + r, \qquad q = \sum_{i=0}^{m-n} q_i x^i, \qquad r_i = 0 \text{ for } i \ge n .

So r=∑i=0n−1rixir = \sum_{i=0}^{n-1} r_i x^i, which line L11 returns, and deg⁡r<n=deg⁡g\deg r < n = \deg g. Stripping zero top coefficients in line L11 does not change the polynomials qq and rr.

Uniqueness. If also f=q′g+r′f = q' g + r' with deg⁡r′<n\deg r' < n, then (q−q′)g=r′−r(q - q') g = r' - r has degree less than nn. If q≠q′q \ne q', the coefficient of xdeg⁡(q−q′)+nx^{\deg(q - q') + n} in (q−q′)g(q - q') g is lc⁡(q−q′) gn\operatorname{lc}(q - q')\, g_n, which is not zero because gng_n is a unit. So deg⁡((q−q′)g)≥n\deg((q - q')g) \ge n, a contradiction. Hence q=q′q = q' and r=r′r = r', and the algorithm returns the unique quotient and remainder of ff by gg.

Operation count. Line L3 does one inversion. Each step does one multiplication in line L6 and nn multiplications and nn subtractions in line L9. Line L10 does no arithmetic. With m−n+1m - n + 1 steps, the total is (2n+1)(m−n+1)(2n + 1)(m - n + 1) operations in RR and one inversion. ■\blacksquare

Back to Polynomial long division