The Euclidean algorithm returns the monic gcd full
This proof shows: correctness.
Let F be a field and f,g∈F[x]. For a,b∈F[x] let D(a,b) be the set of common divisors of a and b in F[x].
Lemma. If b=0 and a=qb+c, then D(a,b)=D(b,c).
Proof. If d∣a and d∣b, then d∣a−qb=c. If d∣b and d∣c, then d∣qb+c=a. □
Invariant. Before each test of line L2, D(r0,r1)=D(f,g).
Initialization. Line L1 only strips zero coefficients, which does not change the polynomials, so r0=f and r1=g.
Maintenance. Line L3 runs only when r1=0. Over a field, lc(r1) is a unit, so long division gives r0=qr1+c with c=r0remr1 and degc<degr1 (see the proof for long division). By the lemma, D(r1,c)=D(r0,r1)=D(f,g), and the new pair is (r1,c).
Termination. Let dt be degr1 before the t-th test of line L2, with deg0=−∞. While the loop runs, r1=0, so dt≥0, and line L3 gives dt+1=deg(r0remr1)<dt. A strictly decreasing sequence of integers that are at least 0 has at most d1+1 terms, so after at most degg+1 passes the new remainder is 0 and the loop stops. If g=0, it stops at once.
Result. When the loop stops, r1=0 and D(f,g)=D(r0,0), the set of divisors of r0.
- If r0=0, every polynomial is in D(f,g), so f=g=0, and line L4 returns 0=gcd(0,0).
- If r0=0, then r0 divides itself, so r0∈D(f,g), and every d∈D(f,g) divides r0. So r0 is a gcd of f and g. Lines L5 and L6 multiply it by the unit u=lc(r0)−1, which does not change the set of its divisors or of its multiples, and give a gcd with leading coefficient ulc(r0)=1. The monic gcd is unique, since two gcds divide each other and so differ by a unit factor, and only one unit multiple is monic.
So EUCLID(f,g) returns the monic gcd(f,g).
Cost. Let m=degf≥n=degg≥0 and let n1=n>n2>⋯>nℓ≥0 be the degrees of the nonzero remainders r1=g,r2,…,rℓ, with n0=m. The pass that divides ri−1 by ri costs at most (2ni+1)(ni−1−ni+1) field operations and one inversion, by the count for long division. Since 2ni+1≤2n+1 and ∑i=1ℓ(ni−1−ni+1)=n0−nℓ+ℓ, the passes cost at most (2n+1)(m−nℓ+ℓ). The degrees n1,…,nℓ are distinct integers in [0,n], so ℓ≤n+1, and nℓ≥0. This gives at most (2n+1)(m+n+1) operations and n+1 inversions for the loop. Lines L5 and L6 add one inversion and nℓ+1≤n+1 multiplications. ■