Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

EXTENDED EUCLID

Greatest Common Divisor


 The greatest common divisor of two positive integers a and b is the largest
divisor common to a and b.
 For example, GCD(3,5)=1, GCD(12,60)=12, and GCD(12,90)=6.
 The Euclidean Algorithm is a technique for quickly finding the GCD of two
integers.  
 If we subtract smaller number from larger (we reduce larger number), GCD
doesn’t change. So if we keep subtracting repeatedly the larger of two, we
end up with GCD.
 Now instead of subtraction, if we divide smaller number, the algorithm stops
when we find remainder 0.
Euclid’s algorithm

 Euclid’s algorithm works in O(logn) time, where n = min(a,b).


 gcd(a, b, c) is equal to gcd(a, gcd(b, c))
Least Common Multiple

 The GCD is closely related to Least (or Lowest) Common Multiple


(LCM).
 The LCM of (a, b) is the smallest positive integer l such that a | l and
b | l.
 Example: lcm(4, 8) = 8, lcm(6, 9) = 18, lcm(20, 12) = 60.
 It has been shown that: lcm(a, b) = a × b/gcd(a, b).
Extended Euclid
 The extended Euclidean algorithm is an algorithm to compute
integers x and y such that: ax+by=gcd(a,b) (Linear Diophantine Equation)
 The existence of such integers is guaranteed by Bézout's lemma.
 The equation ax + by = c has no integral solutions if d | c is not true, with d =
gcd(a, b)
 But if d | c, then there are infinitely many integral solutions. (proof?)
 The first solution (x0, y0) can be found using the Extended Euclid by
reversing the steps in the Euclidean algorithm
 The rest can be derived from x = x0 + (b/d)n, y = y0 − (a/d)n
Find x and y satisfy:
1914x + 899y =gcd(1914,899)

 First use Euclid's algorithm to find the GCD:


1914 = 2×899 + 116
899 = 7×116 + 87
116 = 1×87 + 29
87 = 3×29 + 0
Find x and y satisfy:
1914x + 899y = 29
1914x + 899y = gcd(1914,899)
1914 vs. 899
899 vs. 116
116 vs. 87
87 vs. 29

29 vs. 0
1914x + 899y = 29
1914 . 8 + 899. (-17) = 29
899 . (-1) + 116. 8 = 29
116. 1 + 87.(-1) = 29

87.0 + 29.1 = 29
29.1 + 0.0 = 29
Find x, y satisfy: 245x + 96y = 1
Solve ax + by = d, d = gcd(a, b)
Xuất phát từ nhận xét: gcd (a,b) = gcd (b, r) với r = a % b
Xét phương trình: a*x + b*y = gcd (a,b)
Ta có thể giả sử x1, y1 là nghiệm của phương trình:
b*x1 + (a % b) *y1 = gcd(a,b)
Đây chính là phương trình ở bước tiếp theo của giải thuật euclidean.
(b đóng vai trò a, và a%b đóng vai trò b trong hàm gcd(a, b))
Ta sẽ thay a % b = a – (a/b) * b để có phương trình
b*x1 + (a – (a/b) *b ) * y1 = gcd (a, b)
Khai triển ta được:
a * y1 + b * (x1 – (a/b) * y1) = gcd (a,b)
Applied problem
 Suppose a housewife buys apples and oranges with cost of 8.39 USD.
An apple is 25 cents. An orange is 18 cents. How many of each fruit does she
buy?
 25x + 18y = 839
 a = 25, b = 18, extendedEuclid(25, 18) produces x = -5, y = 7, d = 1;
 or 25 × (-5) + 18 × 7 = 1
  25 × (-4195) + 18 × 5873 = 839.
 Thus x = -4195 + (18/1)n and y = 5873 - (25/1)n.
 x and y are non-negative so -4195 + 18n ≥ 0 and 5873 - 25n ≥ 0
 4195/18 ≤ n ≤ 5873/25, or 233.05 ≤ n ≤ 234.92.  n =234
 Thus x = -4195 +18 × 234 = 17 and y = 5873 - 25 × 234 = 23

You might also like