The Euclidean Algorithm

Similar documents
Remainders. We learned how to multiply and divide in elementary

Ch 4.2 Divisibility Properties

Induction. Induction. Induction. Induction. Induction. Induction 2/22/2018

EUCLID S ALGORITHM AND THE FUNDAMENTAL THEOREM OF ARITHMETIC after N. Vasiliev and V. Gutenmacher (Kvant, 1972)

Notes for Recitation 6

NOTES ON SIMPLE NUMBER THEORY

COMP239: Mathematics for Computer Science II. Prof. Chadi Assi EV7.635

2. THE EUCLIDEAN ALGORITHM More ring essentials

Intermediate Math Circles February 29, 2012 Linear Diophantine Equations I

The Euclidean Algorithm and Multiplicative Inverses

CSE 311: Foundations of Computing. Lecture 12: Two s Complement, Primes, GCD

Chapter 1. Greatest common divisor. 1.1 The division theorem. In the beginning, there are the natural numbers 0, 1, 2, 3, 4,...,

2301 Assignment 1 Due Friday 19th March, 2 pm

Intermediate Math Circles February 26, 2014 Diophantine Equations I

Mat Week 8. Week 8. gcd() Mat Bases. Integers & Computers. Linear Combos. Week 8. Induction Proofs. Fall 2013

Applied Cryptography and Computer Security CSE 664 Spring 2017

Student Responsibilities Week 8. Mat Section 3.6 Integers and Algorithms. Algorithm to Find gcd()

Exercises Exercises. 2. Determine whether each of these integers is prime. a) 21. b) 29. c) 71. d) 97. e) 111. f) 143. a) 19. b) 27. c) 93.

The following is an informal description of Euclid s algorithm for finding the greatest common divisor of a pair of numbers:

5: The Integers (An introduction to Number Theory)

Algebra for error control codes

Introduction to Number Theory. The study of the integers

Chapter 5.1: Induction

8 Primes and Modular Arithmetic

EDULABZ INTERNATIONAL NUMBER SYSTEM

Math Circles - Lesson 2 Linear Diophantine Equations cont.

Fibonacci Numbers. Justin Stevens. Lecture 5. Justin Stevens Fibonacci Numbers (Lecture 5) 1 / 10

4 Powers of an Element; Cyclic Groups

Greatest Common Divisor MATH Greatest Common Divisor. Benjamin V.C. Collins, James A. Swenson MATH 2730

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

Elementary Properties of the Integers

Homework #2 solutions Due: June 15, 2012

NOTES ON INTEGERS. 1. Integers

Finite Fields. Mike Reiter

a the relation arb is defined if and only if = 2 k, k

Fall 2017 Test II review problems

Notes 6: Polynomials in One Variable

CMPUT 403: Number Theory

Math 110 FOUNDATIONS OF THE REAL NUMBER SYSTEM FOR ELEMENTARY AND MIDDLE SCHOOL TEACHERS

Number Theory Basics Z = {..., 2, 1, 0, 1, 2,...} For, b Z, we say that divides b if z = b for some. Notation: b Fact: for all, b, c Z:

Notes on Systems of Linear Congruences

Chapter 3: The Euclidean Algorithm and Diophantine. Math 138 Burger California State University, Fresno

On the least absolute remainder Euclidean algorithm

2 Arithmetic. 2.1 Greatest common divisors. This chapter is about properties of the integers Z = {..., 2, 1, 0, 1, 2,...}.

This is a recursive algorithm. The procedure is guaranteed to terminate, since the second argument decreases each time.

18 Divisibility. and 0 r < d. Lemma Let n,d Z with d 0. If n = qd+r = q d+r with 0 r,r < d, then q = q and r = r.

not to be republished NCERT REAL NUMBERS CHAPTER 1 (A) Main Concepts and Results

Algorithms (II) Yu Yu. Shanghai Jiaotong University

Chapter 5: The Integers

Direct Proof MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Direct Proof Fall / 24

4. Number Theory (Part 2)

Lecture 4: Number theory

Divisibility. Chapter Divisors and Residues

cse 311: foundations of computing Spring 2015 Lecture 12: Primes, GCD, applications

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

PROBLEMS ON CONGRUENCES AND DIVISIBILITY

Homework 3, solutions

2 Elementary number theory

PUTNAM TRAINING NUMBER THEORY. Exercises 1. Show that the sum of two consecutive primes is never twice a prime.

Math 109 HW 9 Solutions

INTEGERS. In this section we aim to show the following: Goal. Every natural number can be written uniquely as a product of primes.

Wednesday, February 21. Today we will begin Course Notes Chapter 5 (Number Theory).

MATH 215 Final. M4. For all a, b in Z, a b = b a.

With Question/Answer Animations. Chapter 4

Numbers. 2.1 Integers. P(n) = n(n 4 5n 2 + 4) = n(n 2 1)(n 2 4) = (n 2)(n 1)n(n + 1)(n + 2); 120 =

Arithmetic Algorithms, Part 1

MATH FINAL EXAM REVIEW HINTS

CSE 311 Lecture 13: Primes and GCD. Emina Torlak and Kevin Zatloukal

PROBLEM SET 1 SOLUTIONS 1287 = , 403 = , 78 = 13 6.

Elementary Algebra Chinese Remainder Theorem Euclidean Algorithm

(e) Commutativity: a b = b a. (f) Distributivity of times over plus: a (b + c) = a b + a c and (b + c) a = b a + c a.

g(x) = 1 1 x = 1 + x + x2 + x 3 + is not a polynomial, since it doesn t have finite degree. g(x) is an example of a power series.

CPSC 467: Cryptography and Computer Security

Number Theory. CSS322: Security and Cryptography. Sirindhorn International Institute of Technology Thammasat University CSS322. Number Theory.

Algorithmic number theory. Questions/Complaints About Homework? The division algorithm. Division

Ma/CS 6a Class 1. Course Details

NUMBER THEORY AND CODES. Álvaro Pelayo WUSTL

The Fundamental Theorem of Arithmetic

Basic Algorithms in Number Theory

Chapter 2. Divisibility. 2.1 Common Divisors

An Algorithm for Prime Factorization

Math.3336: Discrete Mathematics. Primes and Greatest Common Divisors

Discrete Mathematics GCD, LCM, RSA Algorithm

Proofs. Methods of Proof Divisibility Floor and Ceiling Contradiction & Contrapositive Euclidean Algorithm. Reading (Epp s textbook)

Chapter V. Theory of the Integers. Mathematics is the queen of the sciences and number theory is the queen of mathematics. Carl Friedrich Gauss

CS250: Discrete Math for Computer Science

SEVENTH EDITION and EXPANDED SEVENTH EDITION

Writing Assignment 2 Student Sample Questions

Chinese Remainder Theorem

Discrete valuation rings. Suppose F is a field. A discrete valuation on F is a function v : F {0} Z such that:

A number that can be written as, where p and q are integers and q Number.

12x + 18y = 50. 2x + v = 12. (x, v) = (6 + k, 2k), k Z.

CS2800 Questions selected for fall 2017

Some Facts from Number Theory

Number Theory Proof Portfolio

Input Decidable Language -- Program Halts on all Input Encoding of Input -- Natural Numbers Encoded in Binary or Decimal, Not Unary

Elementary Number Theory Review. Franz Luef

MAS114: Solutions to Exercises

MATH 55 - HOMEWORK 6 SOLUTIONS. 1. Section = 1 = (n + 1) 3 = 2. + (n + 1) 3. + (n + 1) 3 = n2 (n + 1) 2.

ECE 646 Lecture 5. Mathematical Background: Modular Arithmetic

Transcription:

MATH 324 Summer 2006 Elementary Number Theory Notes on the Euclidean Algorithm Department of Mathematical and Statistical Sciences University of Alberta The Euclidean Algorithm Given two positive integers a and b such that b a, we know from the division algorithm that there exist unique integers q 1 and r 1, such that a = b q 1 + r 1 (1) with 0 < r 1 < b From (1), the integers a and b have the same common divisors as the integers b and r 1, and therefore gcd(a, b) = gcd(b, r 1 ) Continuing, we find unique integers q 2 and r 2, such that with 0 < r 2 < r 1 if r 1 b, and continuing in this manner, we find unique integers q k and r k, such that with 0 < r k < r k 1 if r k 1 r k 2 b = r 1 q 2 + r 2 (2) r k 2 = r k 1 q k + r k This process must terminate, since the remainders are all nonnegative and are strictly decreasing If r n is the last nonzero remainder, then the last two equations are r n 2 = r n 1 q n + r n r n 1 = r n q n+1 (k) It is clear that gcd(a, b) = gcd(b, r 1 ) = gcd(r 1, r 2 ) = = gcd(r n 1, r n ) = gcd(r n, 0) = r n, since at any stage the integers r k 2 and r k 1 have the same common divisors as the integers r k 1 and r k, and hence the same greatest common divisor This is Euclid s algorithm for computing the greatest common divisor of two positive integers a and b The extended Euclidean algorithm allows us to write gcd(a, b) = s a + t b for some integers s and t This can be done by working from the bottom up in the equations in the Euclidean algorithm However, we have to know all the quotients q k and remainders r k in order to do this On the other hand, we can work from the top down by first writing r 1 as a linear combination of a and b, and then using the second equation to write r 2 as a linear combination of b and r 1, and hence of a and b, and then using the third equation to write r 3 as a linear combination of r 2 and r 1, and hence of a and b Once a quotient q k is used in a calculation, it is no longer needed and so can be forgotten, in fact, the calculation of the s s and t s can be done as the equations in the Euclidean algorithm are derived

The equations in the Euclidean algorithm have the form a = b q 1 + r 1 b = r 1 q 2 + r 2 r 1 = r 2 q 3 + r 3 r k 2 = r k 1 q k + r k and working from the top down, we want to find integers s k and t k such that for each k, with 1 k n r k = s k a + t k b From the first equation, we have r 1 = 1 a + ( q 1 ) b, so that we can take s 1 = 1 t 1 = q 1 Similarly, from the second equation, r 2 = b r 1 q 2 = ( q 2 ) a + (1 + q 1 q 2 ) b, so that we can take s 2 = q 2 t 2 = 1 + q 1 q 2 Assuming that we have found s i and t i so that r i = s i a+t i b, for i = 1, 2,, k 1, then from the equation for the remainder r k, we need r k = r k 2 q k r k 1 = s k 2 a + t k 2 b q k (s k 1 a + t k 1 b) = (s k 2 q k s k 1 ) a + (t k 2 q k t k 1 ) b Therefore, we need s k = s k 2 q k s k 1 t k = t k 2 q k t k 1, ( ) for k > 2 Now, if we define s 1, t 1, s 2, t 2 as above, then we can use ( ) to define s k and t k for larger values of k In particular, if we define then the equations in ( ) are true for all k 1, and s 1 = 1 and s 0 = 0, t 1 = 0 and t 0 = 1, r k = s k a + t k b for all k 1 If r n is the last nonzero remainder, then gcd(a, b) = r n = s n a + t n b

Note that if we define r 1 = a and r 0 = b, then the three sequences {r k k 0, {s k k 0, {t k k 0, all satisfy the same difference equation, but with different initial conditions Therefore, as the Euclidean algorithm computes the remainders r k in order to find the greatest common divisor of a and b, the same calculations can be used to simultaneously calculate the s k s and t k s Now it is easy to write the program which uses the extended Euclidean algorithm to compute the r k s using subtractions instead of the division algorithm, and the same operations will calculate the s k s and t k s A working implementation of this algorithm, written in C, can be found below /* THE EXTENDED EUCLIDEAN ALGORITHM This program calculates the gcd of two non-negative integers a and b and then writes (a,b) = s*a + t*b for some integers s and t To exit the program, enter a = 0 and b = 0 */ #include<stdioh> main( ) { int x,y,a,b,s,t,sprime,tprime,r,temp; do { printf("\nfind the GCD of the positive integers a and b \n"); printf("\nenter a: \n"); scanf("%d",&a); printf("\nenter b: \n"); scanf("%d",&b); if (a!= 0 && b == 0) { temp = a; a = b; b = temp; if ( b!= 0) { x = a; y = b; s = 1; t = 0; sprime = 0; tprime = 1; if (x!= 0) { while (x!= y) { if (x > y) { x = x - y; s = s - sprime; t = t - tprime; if (x < y) { y = y - x; sprime = sprime - s; tprime = tprime - t; r = x; else r = y; printf("\nthe GCD of %d and %d is %d \n", a,b,r); printf("\n%d = (%d,%d) = (%d)*%d + (%d)*%d \n",r,a,b,s,a,t,b); /* if */ else printf("\nyou CAN T DIVIDE BY ZERO!\n"); while (a!= 0 b!= 0); printf("\nbye!\n"); /* main */

Example Let F n be the n th term in the Fibonacci sequence for n 0 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, Use the Euclidean Algorithm to compute the greatest common divisor of F 12 and F 18, and then express (F 12, F 18 ) as a linear combination of F 12 and F 18 Solution Note first that F 11 = 89, F 12 = 144, F 13 = 233, F 14 = 377, F 15 = 610, F 16 = 987, F 17 = 1597, F 18 = 2584 Using the Euclidean Algorithm, with F 12 = 144 and F 18 = 2584, we have 2584 = 17 144 + 136 144 = 1 136 + 8 136 = 17 8 + 0 Therefore, (F 12, F 18 ) = (144, 2584) = 8 Working backwards, we have (F 12, F 18 ) = 8 = 144 136 = 144 (2584 17 144) and therefore (F 12, F 18 ) = 8 = 18 144 + ( 1) 2584 As a check, note that we can factor these integers as: 144 = 12 2 = 3 2 4 2 = 3 2 2 4 2584 = 8 323 = 2 3 17 19, from which it is obvious that (F 12, F 18 ) = 2 3 = 8 Note that we used only 2 divisions in the Euclidean algorithm, so the algorithm can find the greatest common divisor fairly rapidly in most cases However, in the case of consecutive Fibonacci numbers, in each division, the quotient is 1, since F n+2 = 1 F n+1 + F n, and the remainder is F n, so the process is much slower Now we show that it takes exactly n divisions to compute the greatest common divisor of the consecutive Fibonacci numbers F n+1 and F n+2, and that this is, in fact, the worst case scenario for the Euclidean algorithm

Theorem If F n+1 and F n+2 are successive terms in the Fibonacci sequence, with n > 1, then F n+1 and F n+2 are relatively prime and the Euclidean algorithm takes exactly n divisions to verify that gcd(f n+1, F n+2 ) = 1 More generally, the number of divisions needed by the Euclidean algorithm to find the greatest common divisor of two positive integers does not exceed five times the number of decimal digits in the smaller of the two integers proof Applying the Euclidean algorithm to F n+1 and F n+2, and using the defining relation for the Fibonacci numbers at each step, we have F n+2 = 1 F n+1 + F n F n+1 = 1 F n + F n 1 F n = 1 F n 1 + F n 2 F 4 = 1 F 3 + F 2 F 3 = 2 F 2 and the last nonzero remainder is F 2, so that the greatest common divisor gcd(f n+2, F n+1 ) = F 2 = 1, and F n+2 and F n+1 are relatively prime It is clear from the calculations above that the Euclidean algorithm takes exactly n divisions to show that gcd(f n+2, F n+1 ) = F 2 = 1 Now let a and b be positive integers with 0 < b < a We will apply the Euclidean algorithm to find the greatest common divisor gcd(a, b) of a and b Let r 0 = a and r 1 = b, then we get the following sequence of equations: r 0 = q 1 r 1 + r 2, 0 r 2 < r 1, r 1 = q 2 r 2 + r 3, 0 r 3 < r 2, r 2 = q 3 r 3 + r 4, 0 r 4 < r 3, r n 3 = q n 2 r n 2 + r n 1, 0 r n 1 < r n 2, r n 2 = q n 1 r n 1 + r n, 0 r n < r n 1, r n 1 = q n r n Here we have used n divisions Note that each of the quotients q 1, q 2,, q n 1 is greater than or equal to 1, and q n 2, since r n < r n 1 Therefore, r n 1 = F 2 r n 1 2r n 2F 2 = F 3 r n 2 r n 1 + r n F 3 + F 2 = F 4 r n 3 r n 2 + r n 1 F 4 + F 3 = F 5 r 2 r 3 + r 4 F n 1 + F n 2 = F n b = r 1 r 2 + r 3 F n + F n 1 = F n+1 Thus, if there are n divisions used in the Euclidean algorithm, then we must have b F n+1

Now we show by induction that F n+1 > α n 1 for n 2, where α = 1 + 5 2 The basis step consists of verifying this inequality for n = 2 and n = 3 Since α < 2 = F 3 then the result is true for n = 2 Also, since α 2 = 3 + 5 < 3 = F 4, the result is true for n = 3 2 The inductive hypothesis consists of assuming that α k 1 < F k+1 for all integers k with 2 k < n Since α = 1 + 5 is a solution of the quadratic equation x 2 x 1 = 0, then α 2 = α + 1, so that 2 From the induction hypothesis, we have and adding these inequalities, we get α n 1 = α 2 α n 3 = (α + 1) α n 3 = α n 2 + α n 3 α n 2 < F n and α n 3 < F n 1, α n 1 = α n 2 + α n 3 < F n + F n 1 = F n+1, so the result is also true for k = n By the principle of mathematical induction, the result is true for all n 2 Therefore, b F n+1 > α n 1 for n 2, and since log 10 α > 1 5, then and therefore log 10 b > (n 1) log 10 α > n 1 < 5 log 10 b (n 1), 5 Now suppose that b has k decimal digits, so that b < 10 k, then log 10 b < k, so that n 1 < 5k Since k is an integer, this implies that n 5k