Unit 3. Greatest Common Divisor
Unit 3. Greatest Common Divisor
• gcd (8,12) =4
1
Mr. C. D. Bawankar Department of Information Technology
Greatest Common Divisor
• gcd(a,0) = a, and gcd(a,b) = gcd(b, a mod b)
• a >b , if not then swap and if in gcd(b, a mod b) where
a mob b is zero than stop.
Find gcd(132, 28) :
1. r = 132 mod 28 = 20 => gcd(28, 20)
2. r = 28 mod 20 = 8 => gcd(20,8)
3. r = 20 mod 8 = 4 => gcd(8,4)
4. r = 8 mod 4 = 0 => gcd(4,0)
gcd(132, 28) = 4
1 2 3 4 5
a 132 28 20 8 4
b 28 20 8 4 0
2
Mr. C. D. Bawankar Department of Information Technology
GCD and Relatively Prime
• Def: two integers a and b are said to be relatively prime (also
called co-prime) if gcd(a,b) = 1
• so no prime common divisors.
Find gcd(28, 15) :
1. r = 28 mod 15 = 13 => gcd(15, 13)
2. r = 15 mod 13 = 2 => gcd(13, 2)
3. r = 13 mod 2 = 1 => gcd(2,1)
4. r = 2 mod 1 = 0 => gcd(1,0)
gcd(28,15) = 1
Since a prime number has no factors besides itself, clearly a prime number
is relatively prime to every other number (except for multiples of itself)
3
Mr. C. D. Bawankar Department of Information Technology
Test Relative Prime
Q: Find the following gcd’s:
1. gcd(77,11)
2. gcd(77,33)
3. gcd(36,24)
4. gcd(23,7)
4
Mr. C. D. Bawankar Department of Information Technology
Test Relative Prime
A:
1. gcd(77,11) = 11
2. gcd(77,33) = 11
3. gcd(36, 24) = 12
4. gcd(23,7) = 1. Therefore 23 and 7 are relatively
prime.
2.6
Extended Euclidean algorithm, part a
2.7
Extended Euclidean algorithm, part b
2.8
Example
Given a = 161 and b = 28, find gcd (a, b) and the values of s
and t.
Solution
We get gcd (161, 28) = 7, s = −1 and t = 6.
2.9
Example
Given a = 17 and b = 0, find gcd (a, b) and the values of s
and t.
Solution
We get gcd (17, 0) = 17, s = 1, and t = 0.
2.10
Example
Solution
We get gcd (0, 45) = 45, s = 0, and t = 1.
2.11
Example
Given a = 482 and b = 1180, find gcd (a, b) and the values
of s and t.
Solution
q r1 r2 r s1 s2 s t1 t2 T
1 0 0 1
2.12
Given a = 45 and b =13, find gcd (a, b) and the values of s
and t.
Given a = 259 and b = 119, find gcd (a, b) and the values of
s and t.