Announcements CompSci 230 Discrete Math for Computer Science • Homework 4 out

October 22, 2013

Prof. Rodger

Slide ideas from Rosen and Benjamin 1 2

Chap. 4.3 - Greatest Common Chap. 4.3 - Greatest Common Divisor Definition: Let a and b be , not both zero. Definition: Let a and b be integers, not both zero. The largest d such that d | a and also d | b The largest integer d such that d | a and also d | b is called the greatest common divisor of a and b. is called the greatest common divisor of a and b. The greatest common divisor of a and b is The greatest common divisor of a and b is denoted by gcd(a,b). denoted by gcd(a,b).

Examples: Examples: gcd(12, 30) = 6 gcd(12, 30) = 6 gcd(12, -30) = 6 gcd(12, -30) = 6 gcd(16, 9) = 1 relatively prime gcd(16, 9) = 1 relatively prime gcd(7, 63) = 7 gcd(7, 63) = 7 gcd(7, 0) = 7 3 gcd(7, 0) = 7 4 Greatest Common Divisor Greatest Common Divisor Definition: The integers a and b are relatively prime if Definition: The integers a and b are relatively prime if their greatest common divisor is . their greatest common divisor is . Example: Example:

Definition: The integers a1, a2, …, an are pairwise Definition: The integers a1, a2, …, an are pairwise relatively prime if gcd(ai, aj)= whenever i j n. relatively prime if gcd(ai, aj)= whenever i j n.

Example: Are and Example: Are and Solution: Yes Yes

Example: Are Example: Are Solution: No, No,

5 6

What can be created using What numbers can be created using 12 and 30 as building blocks? 12 and 30 as building blocks?

• That means 12s + 30t • That means 12s + 30t • Can you create any positive integer using • Can you create any positive integer using this formula? this formula? • Can you create 4? • Can you create 4? – 4 = 12x + 30 y 4 = 12s + 30 t – = 6(2x + 5y) = 6(2s + 5t) – NO, must be a multiple of 6 NO, must be a multiple of 6

7 8 Can you create 6 using 12s + 30t? Can you create 6 using 12s + 30t?

6 = 12s + 30 t 6 = 12s + 30 t = 12(3) + 30(-1) = 36 – 30 = 6, yes! = 12(3) + 30(-1) = 36 – 30 = 6, yes! Can you create any multiple of 6 using 12 and 30? Can you create any multiple of 6 using 12 and 30? Yes Yes 12(3m) + 30(-m) = 6m 12(3m) + 30(-m) = 6m

9 10

Can you create any using Can you create any number using 16 and 9 as building blocks? 16 and 9 as building blocks? 16s + 9t 16s + 9t Can you create the number 1? Can you create the number 1? 16(4) + 9(-7) = 64 – 63 = 1 16(4) + 9(-7) = 64 – 63 = 1 Yes, can create any number with 16 and 9 Yes, can create any number with 16 and 9 To create any number m: To create any number m: 16(4m) + 9(-7m) = m 16(4m) + 9(-7m) = m

B zout showed back in 18th century B zout showed back in 18th century • If a and b are relatively prime, • If a and b are relatively prime,

then there exists integers x and y such that ax+by=1 11 then there exists integers s and t such that as+bt=1 12 gcds as Linear Étienne Bézout gcds as Linear Étienne Bézout Combinations (1730‐1783) Combinations (1730‐1783) B zout’s Theorem: If a and b are positive integers, B zout’s Theorem: If a and b are positive integers, then there exist integers s and t such that then there exist integers s and t such that gcd(a,b) = sa + tb. gcd(a,b) = sa + tb. (proof in exercises of Section ) (proof in exercises of Section ) Idea: Idea: gcd(a,b) = g gcd(a,b) = g a/g and b/g are relatively prime a/g and b/g are relatively prime g is the largest that divides both of them g is the largest that divides both of them Thus, there exists s and t such that (a/g)s + (b/g)t = 1 Thus, there exists s and t such that (a/g)s + (b/g)t = 1

Definition: If a and b are positive integers, then Definition: If a and b are positive integers, then integers s and t such that gcd(a,b) = sa + tb are called integers s and t such that gcd(a,b) = sa + tb are called B zout coefficients of a and b. The equation gcd(a,b) = B zout coefficients of a and b. The equation gcd(a,b) = sa + tb is called B zout’s identity. 13 sa + tb is called B zout’s identity. 14

Euclidean • The Euclidean algorithm expressed in (325 B.C.E. – 265 B.C.E.) pseudocode is: • The Euclidian algorithm is an efficient method for computing the greatest common divisor of two integers. It is based on the idea that gcd(a,b) is equal to gcd(a,c) when procedure gcd(a, b: positive integers) a b and c is the remainder when a is divided by b. x := a Example: Find gcd( , ): y := b • 287 91 ∙ 3 14 Divide 287 by 91 while y • 91 14 ∙ 6 7 Divide 91 by 14 r := x mod y • 14 7 ∙ 2 0 Divide 14 by 7 Stopping x := y condition y := r gcd( , ) = gcd( , ) = gcd( , ) = return x {gcd(a,b) is x} 15 16 Correctness of Euclidean Algorithm Correctness of Euclidean Algorithm Lemma : Let a = bq + r, where a, b, q, and r Lemma : Let a = bq + r, where a, b, q, and r are integers. Then gcd(a,b) = gcd(b,r). are integers. Then gcd(a,b) = gcd(b,r). Proof: Proof: – Suppose that d divides both a and b. – Suppose that d divides both a and b. Then d also divides a bq = r (by Theorem of Then d also divides a bq = r (by Theorem of Section ). Hence, any common divisor of a and b Section ). Hence, any common divisor of a and b must also be any common divisor of b and r. must also be any common divisor of b and r. – Suppose that d divides both b and r. – Suppose that d divides both b and r. Then d also divides bq + r = a. Hence, any common Then d also divides bq + r = a. Hence, any common divisor of a and b must also be a common divisor of divisor of a and b must also be a common divisor of b and r. b and r. – Therefore, gcd(a,b) = gcd(b,r). – Therefore, gcd(a,b) = gcd(b,r).

17 18

Correctness of Euclidean Algorithm Correctness of Euclidean Algorithm

• Suppose that a and b are positive r0 = r1q1 + r2 0r2 < r1, • Suppose that a and b are positive r0 = r1q1 + r2 0r2 < r1, integers with a b. r = r q + r 0r < r , integers with a b. r = r q + r 0r < r , Let r = a and r = b. 1 2 2 3 3 2 Let r = a and r = b. 1 2 2 3 3 2 0 1 ∙ 0 1 ∙ Successive applications of the Successive applications of the yields: ∙ algorithm yields: ∙ ∙ ∙

rn‐2 = rn‐1qn‐1 + r2 0rn < rn‐1, rn‐2 = rn‐1qn‐1 + r2 0rn < rn‐1, rn‐1 = rnqn . rn‐1 = rnqn .

• Eventually, a remainder of zero occurs in the sequence of terms: a = r0 > r1 • Eventually, a remainder of zero occurs in the sequence of terms: a = r0 > r1 > r2 ∙ ∙ ∙ 0. The sequence can’t contain more than a terms. > r2 ∙ ∙ ∙ 0. The sequence can’t contain more than a terms. • By Lemma 1 • By Lemma 1

gcd(a,b) = gcd(r0,r1) = ∙ ∙ ∙ gcdrn‐1,rn gcdrn , 0 rn. gcd(a,b) = gcd(r0,r1) = ∙ ∙ ∙ gcdrn‐1,rn gcdrn , 0 rn. • Hence the greatest common divisor is the last nonzero remainder in • Hence the greatest common divisor is the last nonzero remainder in the sequence of divisions. the sequence of divisions.

19 20 Finding gcds as Linear Combinations Finding gcds as Linear Combinations Example: Express gcd(252,198) = 18 as a linear combination Example: Express gcd(252,198) = 18 as a linear combination of 252 and 198. of 252 and 198. Solution: First use the Euclidean algorithm to show Solution: First use the Euclidean algorithm to show gcd(252,198) = 18 gcd(252,198) = 18 i. 252 1∙198 54 i. 252 1∙198 54 ii. 198 3 ∙54 36 ii. 198 3 ∙54 36 iii. 54 1 ∙36 18 iii. 54 1 ∙36 18 iv. 36 2 ∙18 iv. 36 2 ∙18 – Now working backwards, from iii and i above – Now working backwards, from iii and i above • 18 54 1 ∙36 • 18 54 1 ∙36 • 36 198 3 ∙54 • 36 198 3 ∙54 – Substituting the 2nd equation into the 1st yields: – Substituting the 2nd equation into the 1st yields: • 18 54 1 ∙198 3 ∙54 4 ∙54 1 ∙198 • 18 54 1 ∙198 3 ∙54 4 ∙54 1 ∙198 – Substituting 54 252 1 ∙198 from i yields: – Substituting 54 252 1 ∙198 from i yields: • 18 4 ∙252 1 ∙198 1 ∙198 4 ∙252 5 ∙198 • 18 4 ∙252 1 ∙198 1 ∙198 4 ∙252 5 ∙198 • This method illustrated above is a two pass method. It first uses • This method illustrated above is a two pass method. It first uses the Euclidian algorithm to find the gcd and then works the Euclidian algorithm to find the gcd and then works backwards to express the gcd as a linear combination of the backwards to express the gcd as a linear combination of the original two integers. original two integers. 21 22

Euclid’s algorithm Euclid’s algorithm

• It is smart! • It is smart! – It not only finds the gcd, it also finds the s and t – It not only finds the gcd, it also finds the s and t that gives you the gcd! that gives you the gcd! • It is fast! • It is fast!

– If a>b, then finds gcd(a,b) in steps – If a>b, then finds gcd(a,b) in steps – Slightly less than the number of digits in b – Slightly less than the number of digits in b – If a and b are 100 digit numbers, finds the gcd – If a and b are 100 digit numbers, finds the gcd in under 500 steps in under 500 steps

23 24 What are the worse numbers to try? What are the worse numbers to try? • How about gcd(99, 98)? • How about gcd(99, 98)? gcd(99,98) = 1 gcd(99,98) = 1 • How about gcd(99,50)? • How about gcd(99,50)? gcd(99,50) = gcd(50,49) = gcd(49,1) = 1 gcd(99,50) = gcd(50,49) = gcd(49,1) = 1 • How about gcd(89,55)? • How about gcd(89,55)? gcd(89,55) = gcd(55,34) = gcd(34,21) = gcd(89,55) = gcd(55,34) = gcd(34,21) = gcd(21,13) = gcd(13,8) = gcd(8,5) = gcd(5,3) = gcd(21,13) = gcd(13,8) = gcd(8,5) = gcd(5,3) = gcd(3,2) = gcd(2,1) = 1 gcd(3,2) = gcd(2,1) = 1

Recognize Fibonnaci numbers? 25 Recognize Fibonnaci numbers? 26

Primes Primes Definition: A positive integer p greater than Definition: A positive integer p greater than is called prime if the only positive factors of p is called prime if the only positive factors of p are and p. A positive integer that is greater are and p. A positive integer that is greater than and is not prime is called composite. than and is not prime is called composite.

Example: is prime only factors are and , Example: is prime only factors are and , is composite because it is divisible by . is composite because it is divisible by . What about 1? What about 1? Neither prime or composite Neither prime or composite It is a unit! It divides everything It is a unit! It divides everything 27 28 The Fundamental Theorem of The Fundamental Theorem of Arithmetic Arithmetic Theorem: Every positive integer greater than Theorem: Every positive integer greater than can be written uniquely as a prime or as the can be written uniquely as a prime or as the product of two or more primes where the product of two or more primes where the prime factors are written in of prime factors are written in order of nondecreasing size. nondecreasing size. Examples: Examples: 2 2

2910 10

The Sieve of Erastosthenes Erastothenes (276‐194 B.C.)

• The Sieve of Erastosthenes can be used to find all primes not exceeding a specified positive integer. For example, begin with the list of integers between and . a. Delete all the integers, other than 2, divisible by 2. b. Delete all the integers, other than 3, divisible by 3. c. Next, delete all the integers, other than 5, divisible by 5. d. Next, delete all the integers, other than 7, divisible by 7. e. Since all the remaining integers are not divisible by any of the previous integers, other than 1, the primes are: continued → 31 32 Euclid Infinitude of Primes (325 B.C.E. – 265 B.C.E.) The Sieve of Erastosthenes Theorem: There are infinitely many primes. (Euclid)

If an integer n is a composite integer, then it has a prime Proof: Assume finitely many primes: p1, p2, ….., pn

divisor less than or equal to √n. –Let q = p1p2··· pn + 1 – Either q is prime or by the fundamental theorem of To see this, note that if n = ab, then a √n or b √n. arithmetic it is a product of primes.

• But none of the primes pj divides q since if pj | q, then pj divides q p1p2··· pn = 1 . , a very inefficient method of determining if a • Hence, there is a prime not on the list p1, p2, ….., pn. It is number n is prime, is to try every integer i √n and see if either q, or if q is composite, it is a prime factor of q.

n is divisible by i. This contradicts the assumption that p1, p2, ….., pn are all the primes. – Consequently, there are infinitely many primes. In previous example, why did we use only 2, 3, 5 and 7? This proof was given by Euclid The Elements. The proof is considered to be one of the most beautiful in all . It is the first proof in The Book, inspired by the Paul Erdős 33 famous mathematician Paul Erdős’ imagined collection of perfect proofs maintained by (1913-1996)34 God.

Marin Mersenne Mersenne Primes (1588-1648) Distribution of Primes Definition: Prime numbers of the form 2p 1 , where p • Mathematicians have been interested in the is prime, are called Mersenne primes. distribution of prime numbers among the positive –22 1 3, 23 1 7, 25 1 37 , and 27 1 127 integers. In the nineteenth century, the prime are Mersenne primes. number theorem was proved which gives an –211 1 2047 is not a Mersenne prime since 2047 asymptotic estimate for the number of primes not 23∙89. exceeding x. – There is an efficient test for determining if 2p 1is Prime Number Theorem: The ratio of the number prime. of primes not exceeding x and x/ln x approaches as – The largest known prime numbers are Mersenne primes. x grows without bound. (ln x is the natural logarithm – As of mid 2011, 47 Mersenne primes were known, the of x) largest is 243,112,609 1, which has nearly 13 million decimal digits. – The theorem tells us that the number of primes not –The Great Internet Mersenne Prime Search GIMPS is a exceeding x, can be approximated by x/ln x. distributed computing project to search for new – The odds that a randomly selected positive integer less Mersenne Primes. than n is prime are approximately (n/ln n)/n = 1/ln n. http://www.mersenne.org/

35 36 Conjectures about Primes Generating Primes Many conjectures about them are unresolved, including:

• Goldbach’s Conjecture : Every even integer n, n 2, is the • Finding large primes with hundreds of digits is sum of two primes. It has been verified by computer for all important in cryptography. positive even integers up to 1.6 ∙1018. The conjecture is believed to be true by most mathematicians. • There is no simple function f(n) such that f(n) is • There are infinitely many primes of the form n2 1, where n prime for all positive integers n. is a positive integer. But it has been shown that there are infinitely many primes of the form n2 1, where n is a • Consider positive integer or the product of at most two primes. – f(n) = n2 n + is prime for all integers . 2 • The Twin Prime Conjecture: The twin prime conjecture is that f( ) = there are infinitely many pairs of twin primes. Twin primes are pairs of primes that differ by 2. Examples are 3 and 5, 5 • Fortunately, we can generate large integers which and 7, 11 and 13, etc. The current world’s record for twin primes as of mid 2011 consists of numbers are almost certainly primes. See Chapter . 65,516,468,355∙2333,333 1, which have 100,355 decimal 37 digits. 38

Finding the Greatest Common Finding the Greatest Common Divisor Using Prime Divisor Using Prime Factorizations • Suppose the prime factorizations of a and b are: • Suppose the prime factorizations of a and b are:

where each exponent is a nonnegative integer, and where all primes where each exponent is a nonnegative integer, and where all primes occurring in either prime are included in both. Then: occurring in either prime factorization are included in both. Then:

• This formula is valid since the integer on the right (of the equals • This formula is valid since the integer on the right (of the equals sign) divides both a and b. No larger integer can divide both a and b. sign) divides both a and b. No larger integer can divide both a and b. • • Example: 120 = 23 ∙3 ∙5 500 = 22 ∙53 Example: 120 = 23 ∙3 ∙5 500 = 22 ∙53 gcd(120,500) = 2min3,2 ∙3min1,0 ∙5min1,3 22 ∙30 ∙51 20 gcd(120,500) = 2min3,2 ∙3min1,0 ∙5min1,3 22 ∙30 ∙51 20 • Finding the gcd of two positive integers using their prime • Finding the gcd of two positive integers using their prime factorizations is not efficient because there is no efficient algorithm factorizations is not efficient because there is no efficient algorithm for finding the prime factorization of a positive integer. for finding the prime factorization of a positive integer.

39 40 Least Common Multiple Definition: The least common multiple of the positive integers a and b is the smallest positive integer that is Example: lcm(30, 35) = divisible by both a and b. It is denoted by lcm(a,b). • The least common multiple can also be computed 5*2*3, 7*5 from the prime factorizations. 5*2*3*7 = 210

This number is divided by both a and b and no smaller number is divided by a and b. Example: lcm( 3 5 2, 4 3) = max3,4 max5,3 max2,0 4 5 2

41 42

LCM and GCD relation Least Common Multiple Theorem : Let a and b be positive integers. Then Example: lcm(30, 35) = ab = gcd(a,b) a,b 5*2*3, 7*5

5*2*3*7 = 210

Example: lcm( 3 5 2, 4 3) = Proof: max3,4 max5,3 max2,0 4 5 2 Note that minx,y maxx,y x y one uses the larger exponent and the other one the smaller exponent, but you get all factors back.

43 44 LCM and GCD relation Consequences of Bézout’s Theorem Lemma 2: If a, b, and c are positive integers such that gcd(a, b) Theorem : Let a and b be positive integers. Then = 1 and a | bc, then a | c. ab = gcd(a,b) a,b Proof: Assume gcd(a, b) = 1 and a | bc – Since gcd(a, b) = 1, by Bézout’s Theorem there are integers s and t such that sa + tb = 1. – Multiplying both sides of the equation by c, yields sac + tbc = c. – From Theorem 1 of Section 4.1: a | tbc part ii) and a divides sac + tbc since a | sac and a|tbc (part i) – We conclude a | c, since sac + tbc = c.

Lemma 3: If p is prime and p | a1a2∙∙∙an, then p | ai for some i. Proof: (proof uses mathematical induction; see Exercise 64 of Section Note that minx,y maxx,y x y 5.1) one uses the larger exponent and the other one the smaller exponent, but you get all factors back. • Lemma 3 is crucial in the proof of the uniqueness of prime factorizations.

45 46

Consequences of Bézout’s Theorem Uniqueness of Prime Factorization Lemma 2: If a, b, and c are positive integers such that gcd(a, b) • We will prove that a prime factorization of a positive integer = 1 and a | bc, then a | c. where the primes are in nondecreasing order is unique. (This is Proof: Assume gcd(a, b) = 1 and a | bc part of the fundamental theorem of arithmetic. The other part, – Since gcd(a, b) = 1, by Bézout’s Theorem there are integers s and which asserts that every positive integer has a prime t such that factorization into primes, will be proved in Section 5.2.) sa + tb = 1. Proof: (by contradiction) Suppose that the positive integer n can – Multiplying both sides of the equation by c, yields sac + tbc = c. be written as a product of primes in two distinct ways: – From Theorem 1 of Section 4.1: n = p1p2 ∙∙∙ ps and n = q1q2 ∙∙∙ pt. a | tbc part ii) and a divides sac + tbc since a | sac and a|tbc (part i) – Remove all common primes from the factorizations to get – We conclude a | c, since sac + tbc = c.

Lemma 3: If p is prime and p | a a ∙∙∙a , then p | a for some i. – By Lemma 3, it follows that divides , for some k, 1 2 n i contradicting the assumption that and are distinct (proof uses mathematical induction; see Exercise 64 of Section primes. 5.1)

• Lemma 3 is crucial in the proof of the uniqueness of prime – Hence, there can be at most one factorization of n into primes in factorizations. nondecreasing order. 47 48 Dividing Congruences by an Integer Dividing Congruences by an Integer

• Dividing both sides of a valid congruence by an integer • Dividing both sides of a valid congruence by an integer does not always produce a valid congruence (see does not always produce a valid congruence (see Section ). Section ).

• But • But

Theorem 7 a b Theorem 7 a b c ac bc (mod m) and gcd(c,m) = c ac bc (mod m) and gcd(c,m) =

Proof: Since ac bc (mod m), m | ac bc = c(a b) Proof: Since ac bc (mod m), m | ac bc = c(a b) by Lemma and the fact that gcd(c,m) = , it follows by Lemma and the fact that gcd(c,m) = , it follows that m | a b. that m | a b. Hence, a b m Hence, a b m

49 50