CISC-102 Winter 2016 Lecture 11 Greatest Common Divisor

Similar documents
CISC-102 Fall 2017 Week 6

Ch 4.2 Divisibility Properties

CS250: Discrete Math for Computer Science

The Euclidean Algorithm and Multiplicative Inverses

All variables a, b, n, etc are integers unless otherwise stated. Each part of a problem is worth 5 points.

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

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

4 Powers of an Element; Cyclic Groups

Discrete Mathematics GCD, LCM, RSA Algorithm

8 Primes and Modular Arithmetic

2 Elementary number theory

2. THE EUCLIDEAN ALGORITHM More ring essentials

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:

Elementary Number Theory

Math Circle Beginners Group February 28, 2016 Euclid and Prime Numbers Solutions

Number Theory and Graph Theory. Prime numbers and congruences.

Chapter 1 A Survey of Divisibility 14

5: The Integers (An introduction to Number Theory)

The set of integers will be denoted by Z = {, -3, -2, -1, 0, 1, 2, 3, 4, }

Divisibility. Def: a divides b (denoted a b) if there exists an integer x such that b = ax. If a divides b we say that a is a divisor of b.

Chapter 5: The Integers

Notes on Systems of Linear Congruences

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

Lecture 7 Number Theory Euiseong Seo

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

Number Theory and Divisibility

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

cse547, math547 DISCRETE MATHEMATICS Professor Anita Wasilewska

Chapter 3 Basic Number Theory

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.

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

Number Theory Notes Spring 2011

MATH 433 Applied Algebra Lecture 4: Modular arithmetic (continued). Linear congruences.

Intermediate Math Circles February 26, 2014 Diophantine Equations I

Intermediate Math Circles February 29, 2012 Linear Diophantine Equations I

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

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

CMPUT 403: Number Theory

Number Theory and Group Theoryfor Public-Key Cryptography

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

COT 3100 Applications of Discrete Structures Dr. Michael P. Frank

Homework #2 solutions Due: June 15, 2012

1. multiplication is commutative and associative;

Solution Sheet (i) q = 5, r = 15 (ii) q = 58, r = 15 (iii) q = 3, r = 7 (iv) q = 6, r = (i) gcd (97, 157) = 1 = ,

Introduction to Number Theory. The study of the integers

Algorithms CMSC Basic algorithms in Number Theory: Euclid s algorithm and multiplicative inverse

Solutions Math 308 Homework 9 11/20/2018. Throughout, let a, b, and c be non-zero integers.

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

Chapter 4 Finite Fields

Applied Cryptography and Computer Security CSE 664 Spring 2017

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

Discrete Structures Lecture Primes and Greatest Common Divisor

Rings and modular arithmetic

Some Facts from Number Theory

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

MATH10040 Chapter 1: Integers and divisibility

NOTES ON INTEGERS. 1. Integers

WORKSHEET ON NUMBERS, MATH 215 FALL. We start our study of numbers with the integers: N = {1, 2, 3,...}

Remainders. We learned how to multiply and divide in elementary

M3P14 LECTURE NOTES 8: QUADRATIC RINGS AND EUCLIDEAN DOMAINS

WORKSHEET MATH 215, FALL 15, WHYTE. We begin our course with the natural numbers:

CSE 20 DISCRETE MATH. Winter

1. (16 points) Circle T if the corresponding statement is True or F if it is False.

NOTES ON SIMPLE NUMBER THEORY

The Fundamental Theorem of Arithmetic

Outline. Some Review: Divisors. Common Divisors. Primes and Factors. b divides a (or b is a divisor of a) if a = mb for some m

Outline. AIT 682: Network and Systems Security. GCD and Euclid s Algorithm Modulo Arithmetic Modular Exponentiation Discrete Logarithms

OWO Lecture: Modular Arithmetic with Algorithmic Applications

2x 1 7. A linear congruence in modular arithmetic is an equation of the form. Why is the solution a set of integers rather than a unique integer?

4. Number Theory (Part 2)

REAL NUMBERS. Any positive integer a can be divided by another positive integer b in such a way that it leaves a remainder r that is smaller than b.

Review. CS311H: Discrete Mathematics. Number Theory. Computing GCDs. Insight Behind Euclid s Algorithm. Using this Theorem. Euclidian Algorithm

CHAPTER 1 REAL NUMBERS KEY POINTS

ALGEBRA. 1. Some elementary number theory 1.1. Primes and divisibility. We denote the collection of integers

Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices. Integers & Algorithms (2.5)

Math 109 HW 9 Solutions

PRACTICE PROBLEMS: SET 1

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

Divisibility in Z. Definition Let a, b Z be integers. We say that b divides a, if there exists c Z such that a = b c; we write b a.

Direct Proof Divisibility

Ma/CS 6a Class 2: Congruences

CSC 474 Network Security. Outline. GCD and Euclid s Algorithm. GCD and Euclid s Algorithm Modulo Arithmetic Modular Exponentiation Discrete Logarithms

Greatest Common Divisor

Elementary Properties of the Integers

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

Integers and Division

CSE20: Discrete Mathematics

Discrete Mathematics and Probability Theory Fall 2018 Alistair Sinclair and Yun Song Note 6

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

Course 2BA1: Trinity 2006 Section 9: Introduction to Number Theory and Cryptography

2x 1 7. A linear congruence in modular arithmetic is an equation of the form. Why is the solution a set of integers rather than a unique integer?

(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.

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

Coding Theory ( Mathematical Background I)

p = This is small enough that its primality is easily verified by trial division. A candidate prime above 1000 p of the form p U + 1 is

2.2 Inverses and GCDs

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

CS 5319 Advanced Discrete Structure. Lecture 9: Introduction to Number Theory II

MATH 145 Algebra, Solutions to Assignment 4

MATH 2112/CSCI 2112, Discrete Structures I Winter 2007 Toby Kenney Homework Sheet 5 Hints & Model Solutions

Transcription:

CISC-102 Winter 2016 Lecture 11 Greatest Common Divisor Consider any two integers, a,b, at least one non-zero. If we list the positive divisors in numeric order from smallest to largest, we would get two lists: a: (1, c1, c2,... a ) b: (1, d1, d2,... b ) Since both lists must contain the number 1, we see that 1 is a common divisor of a and b. Since the greatest divisor of a is a and the greatest divisor of b is b, we can deduce that amongst the common divisors of a and b, there must be one that is the greatest. Thus we can say that given two integers a,b, at least one not zero, there is a unique greatest common divisor of a and b. 1

Computing the greatest common divisor of a non-zero integer a, and 0, is somewhat boring because all non-zero integers divide 0, so the greatest common divisor of a and 0 is always a. So let s just assume from now on that neither a nor b is 0. 2

Example: Let a = 111, and b = 250. We can construct sorted lists of divisors of a and b yielding: a: (1, 3, 37, 111) b: (1, 2, 5, 10, 25, 50, 125, 250) And by inspection we can deduce that 1 is the greatest common divisor of a and b. When the greatest common divisor of two numbers a,b is 1 we say that a and b are relatively prime or coprime. 3

Another example: Let a = 250, and b = 575. We can construct sorted lists of divisors of a and b yielding: a: (1, 2, 5, 10, 25, 50, 125, 250) b:(1, 5, 23, 25, 115, 575) And by inspection we can deduce that 25 is the greatest common divisor of a and b. This method of obtaining all divisors of a and b is very computationally intensive, and would make some essential steps of public key encryption schemes unfeasible. Remarkably an algorithm invented by Euclid (~ 300 BC) finds greatest common divisors in a much more efficient way. 4

Euclid s Algorithm Suppose a,b are non-zero integers then we can define a function on the integers, gcd(a,b), that returns the greatest common divisor of a and b. It will be convenient to further assume that a b. Euclid s algorithm to compute gcd(a,b) is way more efficient than computing all the divisors a and b. The algorithm is based on the following theorem. Euclid s Theorem: Let a,b,q,r be integers such that a = qb + r then gcd (a,b) = gcd(b,r) 5

For example: a = 575, b = 250. ( Note: We already know that gcd(575,250) = 25) 575 = (2)(250) + 75 (Use long division to get q & r) So the claim is that gcd(575, 250) = gcd(250,75). Since we already know that gcd(575,250) = 25 this can be verified by listing the divisors of 250 and 75. 250: (1, 2, 5, 10, 25, 50, 125, 250) 75: (1, 3, 5, 15, 25, 75) 6

We can now iterate this process by renaming a = 250, b = 75 and repeat the previous calculation. That is: 250 = (3)(75) + 25 (Use long division to get q & r) We can again verify that gcd(250,75) = gcd(75,25) by listing the divisors of 75 and 25. 75: (1, 3, 5, 15, 25, 75) 25: (1, 5, 25) Let s repeat this again, so a = 75 and b = 25 75 = (3)(25) + 0 so we have gcd(75,25) = gcd(25,0), and we have already seen that the greatest common divisor of any non-zero integer a and 0 is a. Therefore by Euclid s algorithm we have gcd(575,250) = 25. 7

Euclid s Algorithm in the Python programming language. def euclid_gcd(a,b): # Assume a >= b > 0 r = a % b # this returns r s.t. a = bq + r while r > 0: a,b = b,r r = a % b # this returns r s.t. a = bq + r return b NOTE: The % (mod) operator is found in many programming languages and returns the remainder when doing integer division. 8

Observe that as a side effect of Euclid s algorithm we can always find integers x,y such that gcd(a,b) = ax + by. This can be illustrated with the previous example. (1) 575 = (2) 250 + 75 implies 75 = 575 - (2)250 (2) 250 = (3) 75 + 25 implies 25 = 250 - (3)75 (3) 75 = (3) 25 + 0 Now we can write gcd(575,250) = 25 as: 25 = 250 - (3)75 (Using (2) above) 25 = 250 - (3)[575 - (2)250] (Using (1) above) 25 = (7)250 - (3)575 (Simplify) 9

To prove Euclid s Theorem we will need a preliminary result. Math convention uses the word lemma for preliminary results that are proved in preparation for the proof of the main theorem. Lemma: Let g,a,b be non-zero integers. If g a and g b then g (pa + b) for all integers p. Proof: Since g a and g b we can write ( 1 ) a = cg and b = dg, where c,d are integers. Replacing the values of a and b in g (pa + b) using equation ( 1 ) we get: g (pcg + dg) which simplifies to: g g(pc + d) Now it should be clear that g divides g(pc+d) and thus we conclude that g divides pa + b. 10

Theorem: Let a,b,q,r be integers such that: a = qb + r, 0 r < b, then gcd (a,b) = gcd(b,r). Proof: ( 0 ) Let g1= gcd(a,b) and g2 = gcd(b,r). ( 1 ) Observe that g2 b and g2 r, so g2 pb + r for all integers p, and in particular for q, where a = qb + r. ( a ) Therefore, g2 a, and we have established that g2 is a common divisor of both a and b. ( b) Furthermore, observe that g2 g1 = gcd(a,b) ( 2 ) Using the equation a = qb + r we can write r = - qb + a. g1 b and g1 a so use the lemma (with p = -q) to get g1 -qb + a or g1 r. ( a ) Therefor g1 r and we have established that g1 is a common divisor of b and r. ( b ) Furthermore, observe that g1 g2 = gcd(b,r). ( 3 ) g2 g1 and g1 g2 implies that g1 = g2, so we can conclude that gcd(a,b) = gcd(b,r). 11

Euclid s Algorithm in the Python programming language. def euclid_gcd(a,b): # Assume a >= b > 0 r = a % b # this returns r s.t. a = bq + r while r > 0: a,b = b,r r = a % b # this returns r s.t. a = bq + r return b NOTE: The % (mod) operator is found in many programming languages and returns the remainder when doing integer division. 12

We will argue that euclid_gcd(a,b) finds gcd(a,b) assuming that a b > 0. We first argue that the loop terminates, that is r eventually becomes 0. This is easy to see because the remainder when we divide a by b is less than b. The value of r begins positive and always decreases so it eventually must be zero. The correctness follows from Euclid s theorem. It can also be shown that this function is extremely efficient when compared to looking at all the divisors of a and b. 13