Greatest Common Divisor

Similar documents
The Euclidean Algorithm and Multiplicative Inverses

Chapter 4 Finite Fields

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

Chinese Remainder Theorem

Discrete Mathematics and Probability Theory Fall 2013 Vazirani Note 3

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

Number theory (Chapter 4)

1 Overview and revision

CISC-102 Winter 2016 Lecture 11 Greatest Common Divisor

NUMBER SYSTEMS. Number theory is the study of the integers. We denote the set of integers by Z:

Discrete Mathematics and Probability Theory Summer 2014 James Cook Note 5

Chapter 14: Divisibility and factorization

Lecture 5: Arithmetic Modulo m, Primes and Greatest Common Divisors Lecturer: Lale Özkahya

Basic elements of number theory

Basic elements of number theory

3 The fundamentals: Algorithms, the integers, and matrices

Definition For a set F, a polynomial over F with variable x is of the form

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

Solutions to Problem Set 4 - Fall 2008 Due Tuesday, Oct. 7 at 1:00

Chapter 4. Greatest common divisors of polynomials. 4.1 Polynomial remainder sequences

Q 2.0.2: If it s 5:30pm now, what time will it be in 4753 hours? Q 2.0.3: Today is Wednesday. What day of the week will it be in one year from today?

Math 109 HW 9 Solutions

Math 131 notes. Jason Riedy. 6 October, Linear Diophantine equations : Likely delayed 6

Lecture 7: Polynomial rings

Lecture 11 - Basic Number Theory.

CPSC 467b: Cryptography and Computer Security

Numbers. Çetin Kaya Koç Winter / 18

= 1 2x. x 2 a ) 0 (mod p n ), (x 2 + 2a + a2. x a ) 2

Modular numbers and Error Correcting Codes. Introduction. Modular Arithmetic.

CPSC 467: Cryptography and Computer Security

1. Factorization Divisibility in Z.

Mathematical Foundations of Cryptography

3.2 Solving linear congruences. v3

4 Number Theory and Cryptography

Number Theory. Zachary Friggstad. Programming Club Meeting

Lecture 4: Number theory

CMPUT 403: Number Theory

CS250: Discrete Math for Computer Science

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

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

Chapter 1 A Survey of Divisibility 14

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

4 Powers of an Element; Cyclic Groups

2 Elementary number theory

Intermediate Math Circles February 26, 2014 Diophantine Equations I

CSE 521: Design and Analysis of Algorithms I

Definition 6.1 (p.277) A positive integer n is prime when n > 1 and the only positive divisors are 1 and n. Alternatively

Factoring. there exists some 1 i < j l such that x i x j (mod p). (1) p gcd(x i x j, n).

Introduction to Cybersecurity Cryptography (Part 5)

Chapter 5. Number Theory. 5.1 Base b representations

The next sequence of lectures in on the topic of Arithmetic Algorithms. We shall build up to an understanding of the RSA public-key cryptosystem.

Lecture 2. The Euclidean Algorithm and Numbers in Other Bases

Lecture 7.5: Euclidean domains and algebraic integers

The Fundamental Theorem of Arithmetic

A Few Primality Testing Algorithms

Algebra for error control codes

CHAPTER 3. Congruences. Congruence: definitions and properties

Intermediate Math Circles February 29, 2012 Linear Diophantine Equations I

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 = ,

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

MTH 346: The Chinese Remainder Theorem

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

Ch 4.2 Divisibility Properties

Further linear algebra. Chapter II. Polynomials.

Chapter 9 Mathematics of Cryptography Part III: Primes and Related Congruence Equations

MODEL ANSWERS TO HWK #10

Rings and modular arithmetic

Finite Fields. Mike Reiter

a + bi by sending α = a + bi to a 2 + b 2. To see properties (1) and (2), it helps to think of complex numbers in polar coordinates:

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

8 Primes and Modular Arithmetic

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.

LECTURE 4: CHINESE REMAINDER THEOREM AND MULTIPLICATIVE FUNCTIONS

Beautiful Mathematics

Equations. Rational Equations. Example. 2 x. a b c 2a. Examine each denominator to find values that would cause the denominator to equal zero

(x 1 +x 2 )(x 1 x 2 )+(x 2 +x 3 )(x 2 x 3 )+(x 3 +x 1 )(x 3 x 1 ).

Elementary Properties of the Integers

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

CHAPTER 6. Prime Numbers. Definition and Fundamental Results

Commutative Rings and Fields

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

1/30: Polynomials over Z/n.

2.3 In modular arithmetic, all arithmetic operations are performed modulo some integer.

Lecture Notes. Advanced Discrete Structures COT S

Outline. Number Theory and Modular Arithmetic. p-1. Definition: Modular equivalence a b [mod n] (a mod n) = (b mod n) n (a-b)

NOTES ON SIMPLE NUMBER THEORY

Remainders. We learned how to multiply and divide in elementary

Fall 2017 Test II review problems

Algebra. Modular arithmetic can be handled mathematically by introducing a congruence relation on the integers described in the above example.

Gaussian integers. 1 = a 2 + b 2 = c 2 + d 2.

Algebra. Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Number Theory A focused introduction

Discrete Mathematics and Probability Theory Summer 2017 Course Notes Note 6

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

Homework #2 solutions Due: June 15, 2012

Lecture 6: Introducing Complexity

A group of figures, representing a number, is called a numeral. Numbers are divided into the following types.

5 Keeping the Data Small: Modular Methods

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:

This is an auxiliary note; its goal is to prove a form of the Chinese Remainder Theorem that will be used in [2].

Transcription:

Greatest Common Divisor Graeme Taylor February 8, 2005 In a computer algebra setting, the greatest common divisor is necessary to make sense of fractions, whether to work with rational numbers or ratios of polynomials. Generally a canonical form will require common factors in the numerator and denominator to be cancelled. For instance, the expressions 8 6, 4 3, (1 + (1 3 )), 1 (12 9 ), 2 3 2 all mean the same thing, which would be preferable to write as 4 (that is, a single fraction of the 3 form z, z Z (an integer), n N (natural), gcd(z,n) = 1). n Some useful properties of the gcd Suppose we are working with a suitable ring R (for instance, the integers, or the rational polynomials). Then the greatest (in the sense of magnitude for numbers, or degree for polynomials) divisor of an element r is clearly itself (1r = r). Furthermore, for any element r of R, 0r = 0 so r is a divisor of 0. Hence a first observation: For any r,gcd(r,0) = r Next up, it s completely trivial that if c is the gcd of a and b, it is the gcd of b and a. That is, gcd(a,b) = gcd(b,a) Letting c be any divisor, there must be α and β such that a = αc and b = βc (otherwise, c isn t a divisor!). So we observe that a b = αc βc = (α β)c. That is, If c divides a and c divides b, then c divides a b Then, since division can be thought of as repeated subtraction, we deduce If c divides a and c divides b, and if a b, then c divides the remainder of a. (for non-zero b) b First appeared on Everything2, at http://www.everything2.com/index.pl?node id=1698077 1

Integer GCDs Equipped with just these four simple facts, you might be able to determine a process for finding the greatest divisor of two integers (note that this is defined to be a natural number.) If you can t, the work has conveniently already been done by Euclid, some 2 millenia ago. Despite its age, Euclid s algorithm remains the best method for this task, and the so called extended euclid algorithm even allows you to express the gcd in terms of the original integers. Polynomial GCDs To prove that expertise in this area has in fact progressed in the past two thousand years, the rest of this writeup considers the problem of finding the gcd of two polynomials (in a single variable). This is equivalent to finding their common roots, meaning that gcd calculations can be applied to solving systems of equations. Euclidean Techniques A natural first approach is to refine Euclid s algorithm as devised for numbers to work on polynomials. It is indeed possible to create such an algorithm. However, one of two problems arises. Either you are forced to work with fractional coefficients (awkward) or a fraction-free approach is employed by rescaling- which causes the size of coefficients in intermediate expressions to skyrocket (again awkward). Non-euclidean techniques can be devised which call for a euclidean gcd algorithm ony in circumstances where these two pitfalls can be avoided. But before discussing these, there is an approach along Euclidean lines which works somewhat better than a naive re-scaling version of Euclid s algorithm. GCDs, Resultants, and the Sylvester Matrix First, some definitions. For polynomials P = a n X n +... + a 0 and Q = b m X m +... + b 0, whose roots are the sets α i and β j respectively, we define the resultant of P and Q as r(p,q) = i=1..n j=1..m That is, the product of all possible differences of roots. (β j α i ) Then the Sylvester Matrix of P and Q is an (m + n) (m + n) square matrix generated by m copies of the coefficients of P and n copies of those of Q, shifted right each row and padded by 2

zeros: a n a n 1... a 0 0... 0 0 a n... a 1 a 0... 0 0 0... 0 a n... a 0 b m b m 1... b 0 0... 0 0 b m... b 1 b 0... 0 0 0... 0 b m... b 0 It is a remarkable result that the determinant of this matrix is precisely the resultant of P and Q. Note that the resultant will be zero iff P and Q have a common root- in which case, they have a non-trivial greatest common divisor. Hence, gaussian elimination can be applied to the Sylvester Matrix, analagous to Euclid s algorithm. If the result is non-zero, the polynomials are co-prime. If a result of zero is obtained, then there is a gcd of interest- with a bit more work, an algorithm can keep track of the cancellations that occur during elimination and in this way the common factors determined. Finally, a system of rescalings exists which allows for fraction-free calculation whilst generating coefficients with predictable common factors, which may therefore be efficiently cancelled. This entire technique is encapsulated in the Dodgson-Bareiss algorithm, and it represents the most efficient way to find the gcd along euclidean lines. Non-Euclidean techniques- modular GCD calculation We have seen that polynomial gcd calculation is possible in a fraction-free manner, at the price of intermediate expression swell, which, although reduced by the Bareiss algorithm, can still be horrific. A useful trick would be to bound the size of those intermediate expressions, and this is generally accomplished by the use of modular methods- working modulo a small value such that all expressions fall in a range 0... n or p...p say. Two (de)motivating Examples Sadly, the simplification power of modular mathematics can often erase interesting or important information. There is a further complication with gcds, which is that coefficients of the answer may be greater than any and all of the coefficients present in your original polynomials. Here are some examples that demonstrate these concerns directly. 3

Problem 1 Consider P = x 3 +x 2 x 1 and Q = x 4 +x 3 +x+1. Here all the coefficients are 0 or 1. However, when you factorise P and Q you observe P = (x+1) 2 (x 1) and Q = (x+1) 2 (x 2 x 1). So their gcd is (x+1) 2 which expands as x 2 +2x+1- we have obtained a larger coefficient. Examples of this form can be created to generate an arbitrarily large coefficient. Problem 2 Let P = x 3 and Q = x + 2. Then these are clearly coprime- so their gcd is 1. Yet working modulo 5, Q = P so we have a non-trivial gcd of x + 2, of greater degree than the true gcd. Solutions to these problems The first issue, of unexpectedly large coefficients, can be fairly easily resolved- there is an upper bound, the Landau-Mignotte bound, on the absolute value of coefficients in the gcd- generated by a fairly ugly formula involving the degrees of the polynomials and their coefficients. For a precise description, see the literature referenced at the end of this writeup; its existance is sufficient for the discussion that follows. The second problem needs more work to resolve, but is easy to describe. Given polynomials P and Q, we observe (without proof) that degree( gcd((p mod n),(q mod n)) ) degree( gcd(p,q) ) Equality holds in the above only if gcd((p mod n),(q mod n)) = (gcd(p,q)) mod n. That is, if the modular image of the gcd is the gcd of the modular images. The above will fail only if n divides the resultant of P G, Q, where G is the true gcd of P and G Q. The resultant is finite, so has only finitely many prime factors. Hence, if we chose n to be prime, then only finitely many choices of n will be bad. A bad gcd will be immediately apparent- it won t actually divide both P and Q! On the other hand, a gcd found by modular means has degree at least that of the true gcd, so if it turns out to be a divisor, it is the gcd. So if we just keep generating gcds working modulo a prime, eventually we ll exhaust the bad primes and uncover the true gcd. Note that in the two algorithms offered below, a gcd calculation is required! This seems circularbut we already have a working yet potentially unwieldy gcd algorithm from the Dodgson-Bareiss approach, which, modulo a prime, won t be so unwieldy after all. 4

Large prime modular GCD algorithm Pick p a prime greater than twice the LM (Landau-Mignotte) bound. Calculate Qp = gcd(ap,bp) (where Xp denotes X mod p) Rewrite Qp over the integers such that its coefficients fall in the range p subtract p from each coefficient that falls outside the LM bound) If Qp divides A and divides B, Qp is the true gcd. If not, repeat from start with the next largest prime. 2 to p 2 (that is, add or Many small primes modular GCD algorithm The above single-prime technique could still yield large coefficients if the LM bound is high; and we only determine if it was a good prime at the very end. Using the Chinese remainder theorem, it is possible to build up to the LM bound in stages, discarding any unlucky primes along the way. By using successive small primes, no modular gcd calculation will be very difficult, and there need not be many such iterations- knowing the answer mod m and n, the Chinese remainder theorem yields an answer mod mn. GCD(A,B) Define LM= Landau-Mignotte bound of A,B Define p=firstprime(a,b) // p should be chosen so as not to divide the leading coefficients of A and B. Define C=gcd(Ap,Bp) * If degree C = 0, return 1 //NB this assumes monic polynomials; the point is we have a trivial gcd Define Known=p Define Result=C While Known <= 2LM do p=nextprime(a,b,p) C=gcd(Ap,Bp) If degree C < degree Result, goto * //all previous primes bad! If degree C > degree Result, do nothing If degree C = degree Result Result=ChineseRemainder(C,Result,p,Known) Known=Known*p end while 5

Check Result divides A and B. If not, redo from start, avoiding all primes used so far. Note that an early-exit strategy is possible- if the result after applying the chinese remainder theorem is unchanged, the odds are very good that you have found the true gcd. So the algorithm can be refined by testing for Result dividing A and B whenever this occurs, and halting the loop if successful. References CM30070 Computer Algebra, University of Bath- lecture notes, revision notes, and the lecturer s book of the same title. More in-depth information on the specifics of algorithms and bounds can be found in the book, although it is currently out of print. Details at http://www.bath.ac.uk/ masjhd/dsteng.html 6