Docsity

Euclidean Algorithm for GCD: Understanding the Main Idea, Algorithm, and Correctness - Pro, Study notes of Cryptography and System Security

Toggle sidebar
bg1
CSCI 162 and CSCI 284/Vora/GWU 1
CSCI 162/284 Cryptography: Euclidean Algorithm for the GCD
The euclidean algorithm for the gcd of two elements is thought to be the oldest existing algorithm. It is recursive. We
describe the main idea behind it as a theorem with proof, then the algorithm, and then an argument that the algorithm
is correct.
1 Main Idea
Theorem:gcd(m, a) = gcd(a, m rem a)
Proof: Let gbe any common factor of mand a, and r=m rem a, then m=r+qa, q ∈Z.
g|m, g|a
⇒m=q1g, a =q2g f or some q1, q2∈Z
⇒r=g(q1−qq2)
⇒g|r, g|a
Further, let hbe any common factor of aand r, then:
h|a, h|r
⇒a=q3h, r =q4h for some q3, q4∈Z
⇒m=h(q4+qq3)
⇒h|r, h|m
Thus common factors of aand rare exactly the common factors of mand a. Hence the greatest common factor of a
and ris the greatest common factor of aand m.
Note here that any number is considered a factor of 0, so we do not consider the case r= 0 separately.
2 Algorithm
The euclidean algorithm is as follows:
Algorithm gcd(m, a) /* m > a */
(X, Y ) := (m, a)/* Initialize */
while (Y6= 0)(X, Y ) := (Y , X rem Y )/* Recursion */
return(X)
Example Use the euclidean algorithm to determine gcd(575,299).
(X, Y ) = (575,299)
(X, Y ) = (299,276)
(X, Y ) = (276,23)
(X, Y ) = (23,0)
return(23)
pf2

Partial preview of the text

Download Euclidean Algorithm for GCD: Understanding the Main Idea, Algorithm, and Correctness - Pro and more Study notes Cryptography and System Security in PDF only on Docsity!

CSCI 162 and CSCI 284/Vora/GWU 1

CSCI 162/284 Cryptography: Euclidean Algorithm for the GCD

The euclidean algorithm for the gcd of two elements is thought to be the oldest existing algorithm. It is recursive. We describe the main idea behind it as a theorem with proof, then the algorithm, and then an argument that the algorithm is correct.

1 Main Idea

Theorem:gcd(m, a) = gcd(a, m rem a) Proof: Let g be any common factor of m and a, and r = m rem a, then m = r + qa, q ∈ Z.

g|m, g|a ⇒ m = q 1 g, a = q 2 g f or some q 1 , q 2 ∈ Z ⇒ r = g(q 1 − qq 2 ) ⇒ g|r, g|a

Further, let h be any common factor of a and r, then:

h|a, h|r ⇒ a = q 3 h, r = q 4 h f or some q 3 , q 4 ∈ Z ⇒ m = h(q 4 + qq 3 ) ⇒ h|r, h|m

Thus common factors of a and r are exactly the common factors of m and a. Hence the greatest common factor of a and r is the greatest common factor of a and m. Note here that any number is considered a factor of 0, so we do not consider the case r = 0 separately.

2 Algorithm

The euclidean algorithm is as follows: Algorithm gcd(m, a) /* m > a / (X, Y ) := (m, a) / Initialize / while (Y 6 = 0) (X, Y ) := (Y, X rem Y ) / Recursion */ return(X)

Example Use the euclidean algorithm to determine gcd(575, 299).

(X, Y ) = (575, 299) (X, Y ) = (299, 276) (X, Y ) = (276, 23) (X, Y ) = (23, 0) return(23)

CSCI 162 and CSCI 284/Vora/GWU 2

Example Use the euclidean algorithm to determine gcd(4316, 1245).

(X, Y ) = (4316, 1245) (X, Y ) = (1245, 581) (X, Y ) = (581, 83) (X, Y ) = (83, 0) return(83)

Example Use the euclidean algorithm to determine gcd(2652, 8855).

(X, Y ) = (8855, 2652) (X, Y ) = (2652, 899) (X, Y ) = (899, 854) (X, Y ) = (854, 45) (X, Y ) = (45, 44) (X, Y ) = (44, 1) (X, Y ) = (1, 0) return(1)

3 Correctness of Algorithm

In each recursion, gcd(X, Y ) stays the same while X and Y change. Further, at each step, we decrease both X and Y , and, because both are remainders, neither is ever negative. Hence the algorithm will end some time, in fact, in at most a steps. Finally, at the last but one recursion, because X rem Y is zero, X is a multiple of Y and hence gcd(X, Y ) = Y. At the last recursion, (X, Y ) := (Y, 0) and the returned value X is the correct gcd (it is the value of Y from the previous recursion). A more formal proof is as follows.

Theorem: Algorithm gcd(m, a) returns gcd(m, a).

Proof: We denote the value of (X, Y ) in the ith^ iteration as (Xi, Yi). Then, (Xi, Yi) := (Yi− 1 , Xi− 1 rem Yi− 1 ), and (X 0 , Y 0 ) = (m, a). Let the total number of iterations be N. That is, YN = 0, and Algorithm gcd(m, a) returns XN. Then, from the theorem proved earlier, if N is finite,

gcd(m, a) = gcd(X 0 , Y 0 ) = gcd(X 1 , Y 1 ) = ... = gcd(XN − 1 , YN − 1 )

Further, YN − 1 |XN − 1 , hence gcd(XN − 1 , YN − 1 ) = YN − 1 = XN. Hence gcd(m, a) = XN , which is what Algorithm gcd(m, a) returns. Further, 0 < Yi < Yi− 1 unless Yi = 0. Hence Yi is strictly decreasing and non-negative. As the algorithm stops when Yi = 0, and Yi decreases by at least 1 at each step, N ≤ a = Y 0. Hence N is finite.