Polynomial long division (source code)

= Polynomial long division
{wiki}

Start with a provisional <polynomial quotient> $q=0$ and remainder $h=f$. If $h$ has <polynomial degree> $n$ at least the divisor's <polynomial degree> $m$, with <leading coefficients> $a$ and $b$, subtract $t g$, where
$$
t=\frac{a}{b}X^{n-m},\qquad q\longleftarrow q+t,\qquad h\longleftarrow h-tg.
$$
The identity $f=gq+h$ survives each step, while the leading term cancels and the <polynomial degree> of nonzero $h$ strictly decreases. Stop when $h=0$ or $\deg h<m$.

For uniqueness over a <field>, subtract two proposed decompositions:
$$
g(q_1-q_2)=r_2-r_1.
$$
If $q_1\ne q_2$, the left side has <polynomial degree> at least $\deg g$, while the right side is zero or has smaller <polynomial degree>. Hence $q_1=q_2$ and then $r_1=r_2$.<Abstract Algebra by Romyar Sharifi/Chapter 3 Ring theory>[Abstract Algebra, Chapter 3 Ring theory, §3.9, Theorem 3.9.1 and proof: cancellation, termination and degree comparison for uniqueness]{ref}