Number Theory The Euclidean algorithm University of Basrah
Number Theory The Euclidean algorithm University of Basrah College of Education for Pure Science Computer Science Department Prof. Dr. Hamid AL-Asadi 2016 -2017 (1) 1 V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi The Euclidean algorithm The key to an efficient algorithm for computing greatest common divisors is division with remainder, which is simply the method of “long division”. Definition: Let a and b be positive integers. Then a divided by b has quotient q and remainder r means that a/b = q + r/b a = b · q + r with 0 ≤ r < b (1) Suppose that d is a common divisor of a and b. Then it is clear from the formula (1) that d is also a divisor of r. Similarly, if e is a common divisor of b and r, then (1) shows that e is a divisor of a. In other words, the common divisors of a and b are the same as the common divisors of b and r, hence (2) V 4 -2 g. c. d(a, b) = g. c. d(b, r)
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Now repeat the process by dividing b by r to get a certain quotient and remainder, say b = r · q' + r' with 0 ≤ r' < r. Then the same reasoning shows that g. c. d(b, r) = g. c. d(r, r'). Continuing this process, the remainders get smaller and smaller, until eventually we get a remainder of 0, at which point the final value g. c. d(s, 0) = s is equal to the g. c. d of a and b. there's a relatively quick way to find g. c. d. (a, b) even when you have no idea of the prime factors of a or b. It's called the Euclidean algorithm. (3) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Theorem : (the Euclidean algorithm): Let a and b be positive integers. The following algorithm computes g. c. d(a, b) in a finite number of steps. (1) If a < b, switch a and b. Let r 0 = a and r 1 = b. (2) Set i = 1. (3) Divide ri− 1 by ri to get a quotient and remainder, ri− 1 = ri · qi + ri+1 with 0 ≤ ri+1 < ri. (4) If the remainder ri+1 = 0, then ri = g. c. d(a, b). Return the value ri. (5) Otherwise ri+1 > 0, so set i = i + 1 and go to Step 3. (4) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Proof. The Euclidean algorithm consists of a sequence of divisions with remainder as illustrated in Figure 1 (remember that we set r 0 = a and r 1 = b). ((Figure 1)) (5) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Example Find g. c. d. (1547, 560) using the Euclidean algorithm. Solution: . we are done: g. c. d. (1547, 560) = 7 (6) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Another Example Find g. c. d. (346, 592) using the Euclidean algorithm. Solution: . g. c. d. (592, 346) � 592 = 346 · 1 + 246 � 346 = 246 · 1 + 100 � 246 = 100 · 2 + 46 � 100 = 46 · 2 + 8 � 46 = 8 · 5 + 6 � 8 = 6 · 1 + 2 � 6 = 2 · 3 + 0 we are done: g. c. d. (346, 592) = 2 (7) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Another Example Find g. c. d. (55, 35) using the Euclidean algorithm. Solution: . 55 = 35*1 + 20 so gcd(55, 35) = gcd(35, 20) 35 = 20*1 + 15 so gcd(35, 20) = gcd(20, 15) 20 = 15*1 + 5 done gcd(55, 35) = 5 15= 5*3 + 0 we are done: g. c. d. (55, 53) = 5 (8) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Theorem : Euclid’s Extended Algorithm Let d = g. c. d. (a, b), where a > b. Then there exist integers u and v such that d = ua + bv. In other words, the g. c. d. of two numbers can be expressed as a linear combination of the numbers with integer coefficients. Example : . Express 7 as a linear combination of 1547 and 560. Solution: . We successively compute: (9) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Example : . Let a= 1547 and b=560 , find g. c. d. (1547, 560) , and express c as a linear combination of the numbers with integer coefficients. Solution: . 1547= 2. 560+ 427 560= 1. 427+ 133 427= 3. 133 + 28 133= 4. 28+ 21 28= 1. 21+ 7 (10) 21= 3. 7+ 0 d = ua + bv. g. c. d. (1547, 560) = 7 u = 21, v= -58. V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi Example : . Let a= 654 and b=1807 , find c=g. c. d(645, 1807), and express c as a linear combination of 654 and 1807. Solution: . 1807 = 2. 654 + 499 = 1807 - (2. 654) 654 = 1. 499 + 155 = 654 – 1. 499 = 654 – 1. (1807 - (2. 654)) =-1807 + (3. 654) 499 = 3. 155 + 34 34= 499 - (3. 155)= 1807 - (2. 654) - (3. (-1807 + (3. 654))) =(4. 1807) - (11 . 654) 155 = 4. 34 + 19 = 155 - (4. 34) =(-1807 + (3. 654)) - (4. ((4. 1807) - (11. 654))) = (-17. 1807) + (47. 654) (11) V 4 -2
2016 -2017 University of Basrah - College of Education for Pure Science - Computer Science Department Prof. Dr. Hamid AL-Asadi 34 = 1. 19 + 15 15 = 34 – 1 . 19 = ((4. 1807) - (11. 654)) – 1 . ((-17. 1807) + (47. 654)) = (21. 1807) - (58. 654) 19 = 1. 15 + 4 4 = 19 – 1. 15 = ((-17. 1807) + (47. 654)) – 1. ((21. 1807) - (58. 654)) = (38. 1807) + (105. 654) 15 = 3. 4 + 3 3 = 15 - (3. 4) = ((21. 1807) - (58. 654)) - (3. ((-38. 1807) + (105. 654))) = (135. 1807) - (373. 654) 4 = 1. 3 +1 1 = 4 – 1. 3 = ((-38. 1807) + (105. 654)) – 1. ((135. 1807) - (373. 654)) = (173. 1807) + (478. 654) 3 = 3. 1 + (12) 0 Therefore, 1 = g. c. d(654, 1807) = (-173. 1807) + (478. 654), u = 478, v = -173 V 4 -2
2016 -2017 University of Basrah College of Education for Pure Science Computer Science Department Prof. Dr. Hamid AL-Asadi Thank you for your Attention (13) V 4 -2
- Slides: 13