Euclidean (Euclid’s) Algorithm
(for finding GCD)
GCD-Example
Euclidean Algorithm for finding GCD
(Method - 1)
• Prerequisite: a>b
Example - 1
• Find GCD (50,12)
Example - 2
• Find GCD(529,123)
Euclidean Algorithm for finding GCD
(Method - 2)
• gcd(A,B)=?
– Draw the table with Q, A, B, R columns.
– Ensure A > B.
– Compute Q and R.
– Shift B to A and R to B for next row.
– Repeat until B = 0, then gcd(A,B) = A.
Step - 1
• Find GCD (12,33)
Step - 2
Step - 3
Step - 4
Example - 2
• Find GCD (750, 900) using Euclid’s algorithm.
Example - 3
• GCD(252,105)=?
Exercise
• Find GCD of (1005, 105) using Euclid’s
algorithm.
• Find GCD (2740, 1760)
• Find GCD (25, 60)
Extended Euclidean Algorithm
Extended Euclidean Algorithm
• Finding gcd(a,b) and x,y.
• Finding multiplicative inverse.
Extended Euclidean Algorithm –
Finding gcd(a,b) and x,y
• Given two integers a and b , we often need to
find 2 integers x and y such that
ax + by = gcd(a,b)
• So extended ecludian’s algorithm used to find
gcd(a,b), and the values of x and y.
Steps
• gcd(A,B) =? and x,y=?
– Draw the table with Q, A, B, R, X1, X2, X, Y1, Y2,
and Y columns.
– Ensure A > B.
– Assign X1=1, X2=0, Y1=0, Y2=1 for first row.
Compute Q, R and X = X1-QX2 and Y=Y1-QY2
– Shift B to A and R to B , X2 to X1, X to X2, Y2 to Y1,
Y to Y2 for next row.
– Repeat until B = 0, then gcd(A,B) = A, X = X1 and
Y = Y1.
Example
• Find the gcd(161,28) & the values of x and y.
• Solution:
gcd(161,28) = 7 NOTE:
X = -1 ax+by = gcd(a,b)
Y=6 161*(-1)+28*6 = gcd(161,28)
7 = 7 Hence Verified
Extended Euclidean Algorithm –
Multiplicative Inverse (M.I)
• Example
Modular M.I
(M.I under modular arithmetic)
• When , then we get 1 as remainder.
• Definition: A modular M.I of an integer ‘a’ is an
integer ‘x’ such that the product ‘ax’ is congruent to
‘1’ with respect to the modulus ‘m’.
ax ≡ 1 mod m
Where x is a multiplicative inverse of a.
Modular M.I
• Examples: 3,5 are co-prime,
therefore multiplicative
inverse exist.
• Consider, No M.I exists, because
5 and 10 are not relatively prime.
• If gcd(n1,n2) ≠ 1, then it means they are not
relatively prime to each other in such cases we
will never have a M.I.
Modular M.I
(Contd…….)
• M.I of 2 (mod 5) is 3.
• M.I of 2 (mod 7) is 4.
• Find M.I using Extended Euclidean algorithm.
Example - 1
• What is the multiplicative inverse of 3 mod 5?
Example - 2
• What is the multiplicative inverse of 11 mod
13?
Example - 3
• What is the multiplicative inverse of 11 mod
26?
Exercise
• What is the multiplicative inverse of 10 mod
11?