

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The euclidean algorithm for finding the greatest common divisor (gcd) of two integers and its extension to find integers r and s such that gcd(a, b) = ar + bs. A proof of the algorithm's correctness and a matrix formulation of the extended algorithm.
Typology: Summaries
1 / 3
This page cannot be seen from the preview
Don't miss anything!


The Euclidean algorithm for the computation of the greatest common divisor of two integers is one of the oldest algorithms known to us. This algorithm was described by Euclid in Book VII of his Elements, which was written about 300BC. In modern formulation, this algorithm can be stated as follows:
Algorithm 1 (Euclidean Algorithm)
Input: Integers a > b ≥ 0. Output: The greatest common divisor of a and b. while b 6 = 0 do (a, b) := (b, a mod b); od; return a.
The algorithm terminates after a finite number of iterations, since b is replaced in each iteration by the remainder r = a mod b, which is a non- negative integer that is strictly smaller than b. Therefore, the algorithm terminates after at most b iterations. We show now that the algorithm is correct. We denote by 〈a, b〉 the set {ax + by | x, y ∈ Z}; this set is called the ideal generated by a and b in the ring of integers. Notice that if 〈a, b〉 contains the integers c and d, then 〈c, d〉 is a subset of 〈a, b〉.
Lemma 1 If b 6 = 0, then 〈a, b〉 = 〈b, a mod b〉.
Proof. The ideal 〈a, b〉 contains the remainder r = a mod b, since r = a − qb with q = ba/bc. Thus, if b 6 = 0 then the ideal 〈b, a mod b〉 is a subset of 〈a, b〉.
On the other hand, a is an element of the ideal 〈b, a mod b〉, since a = qb + r. Therefore, 〈a, b〉 is contained in the ideal 〈b, a mod b〉. It follows that the ideals 〈a, b〉 and 〈b, a mod b〉 are the same. 2
The lemma shows that the ideal generated by the values of the variables a and b in Algorithm 1 is an invariant of the loop. Suppose that the variable a in Algorithm 1 contains the value g when the algorithm terminates, then 〈a, b〉 = 〈g, 0 〉. It follows that a and b are multiples of g, hence g is a common divisor of a and b. On the other hand, g can be expressed in the form g = ax+by. Therefore, each common divisor of a and b divides g, hence g = gcd(a, b). This proves that Algorithm 1 is correct.
Extension. The correctness proof of Algorithm 1 showed that there exist integers r and s such that gcd(a, b) = ar + bs. We want to extend the Euclidean algorithm to determine r and s. Each iteration in the Euclidean algorithm replaces (a, b) by (b, a mod b). We can formulate this as a matrix multiplication:
(b, a mod b) = (a, b)
1 −q
, with q = ba/bc.
Suppose that the algorithm terminates after k iterations, then the operations performed on (a, b) can be summarized in matrix notation by
(gcd(a, b), 0) = (a, b)
1 −q 1
1 −q 2
1 −qk
The product of the quotient matrices
1 −qi
gives a 2 × 2 matrix such that
(gcd(a, b), 0) = (a, b)
r u s v
and the first column of the resulting matrix gives the desired integers r and s. The idea of the extended Euclidean algorithm is to keep track of the product of the quotient matrices along with the remainder computation. For example, the Euclidean algorithm computes the greatest common divisor of 15 and 6 by the following swap and remainder steps (15, 6) → (6, 3) → (3, 0). The extended Euclidean algorithm performs these steps in matrix formulation, and records the product of the quotient matrices as follows:
^.