Pseudo-division returns q and r with lc(g)^(m - n + 1) f = q g + r and deg r < n full
This proof shows: correctness.
Let R be a commutative ring, f,g∈R[x] with g=0, m=degf, n=degg, ℓ=lc(g) and g=∑j=0ngjxj, so gn=ℓ.
The case m<n. Line L2 returns (0,f). Here δ=0, and ℓ0f=0⋅g+f with degf<n. If f=0, then m=−∞<n and the same holds.
The case m≥n. Let d=m−n≥0. Write r(k) for the value of the array r, read as a polynomial, before step k of line L4, for k=d,d−1,…,0, and r(−1) for its value after step 0. Let
Qk=i=k+1∑dtiℓi−k−1xi. Invariant. For each k from d down to −1:
- ℓd−kf=Qkg+r(k);
- every coefficient of r(k) at a position j>n+k is 0.
Initialization (k=d). Qd=0 and r(d)=f, so part 1 reads f=f. Part 2 holds because degf=m=n+d.
Maintenance. Assume the invariant for k≥0. Line L5 sets tk to the coefficient of xn+k in r(k). Lines L6 and L7 change the positions 0,…,n+k−1, and line L8 sets position n+k to 0. The positions above n+k are not changed and stay 0 by part 2.
We claim that r(k−1)=ℓr(k)−tkxkg. At a position j<n+k, the coefficient of ℓr(k) is ℓ⋅rj(k), which line L6 computes, and the coefficient of tkxkg is tkgj−k (taken as 0 when j<k), which line L7 subtracts. At position n+k, the coefficient of ℓr(k)−tkxkg is ℓtk−tkgn=ℓtk−tkℓ=0, because R is commutative, and line L8 stores this 0. At positions above n+k, both r(k) and xkg have zero coefficients. This proves the claim and part 2 for k−1, since now position n+k is 0 too.
For part 1, multiply the invariant for k by ℓ:
ℓd−k+1f=ℓQkg+ℓr(k)=(ℓQk+tkxk)g+(ℓr(k)−tkxkg). The second summand is r(k−1) by the claim, and
ℓQk+tkxk=i=k+1∑dtiℓi−kxi+tkxk=i=k∑dtiℓi−kxi=Qk−1. So ℓd−(k−1)f=Qk−1g+r(k−1).
End. For k=−1 the invariant gives ℓd+1f=Q−1g+r(−1) with Q−1=∑i=0dtiℓixi, and r(−1) has zero coefficients at all positions j>n−1, that is degr(−1)<n. Line L9 computes the coefficients qk=tkℓk of Q−1, with ℓk obtained by repeated multiplication, and line L10 returns r(−1) truncated to positions 0,…,n−1, which loses only zero coefficients. Stripping zero top coefficients does not change either polynomial. Since d+1=m−n+1=δ, the output satisfies ℓδf=qg+r with degr<n.
Termination. The loop of line L4 runs d+1 times, and each inner loop is finite. ■