<<

Outline Euclid’s for GCD The Least Common Multiple Prime numbers Prime Factorization

The

Alice E. Fischer

CSCI 1166 Discrete Mathematics for Computing March 20-21, 2018 Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization

1 Euclid’s Algorithm for GCD Mathematical Foundations The Algorithm

2 The Least Common Multiple

3 Prime numbers

4 Prime Factorization Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Greatest Common

The (gcd) of two integers, a, b, is the largest that divides both. Formally, the gcd(a, b) is the greatest integer d such that d | a and d | b.

Examples: gcd(1, x) = 1 gcd(10, 2) = 2 gcd(2, 10) = 2 gcd(2, 15) = 1 gcd(14, 10) = 2 gcd(13, 13) = 13 Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Greatest Common Divisor

Lemma 1: For x ∈ Z +, gcd(x, 0) = x

Proof: 0 divided by anything is 0 with no remainder. (.) Therefore, any integer, x, divides 0. (Definition of “divides”.) Any integer, x, divides itself. (x/x = 1, with remainder 0, and definition of divides.) x is the largest integer that divides x. (Definition of divides.) gcd(x, 0) = x. (Definition of gcd.) Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Greatest Common Divisor

Lemma 2: For a, b, q, r ∈ Z and either a 6= 0 or b 6= 0. if a = bq + r then gcd(a, b) = gcd(b, r)

Proof by into parts: Part 1a. Any common divisor of b and r also divides a. Part 1b. Any common divisor of a and b also divides r.

Part 2. Show that gcd(a, b) = gcd(b, r). Part 2a. Show that gcd(a, b) ≤ gcd(b, r). Part 2b. Show that gcd(b, r) ≤ gcd(a, b). Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Greatest Common Divisor

Part 1a. Show ANY common divisor of b and r also divides a. 1 Let c be any common divisor of a and b. 2 Then ∃m, n ∈ Z, a = cm and b = cn. (Def. of divides.) 3 a = bq + r, so cm = cnq + r. (Substitute equals.) 4 r = cm − cnq = c(m − nq) (Arithmetic.) 5 So c divides r, and c is a common divisor of a, b and r.

Part 1b. Show ANY common divisor of a and b also divides r. 1 Let c be any common divisor of b and r. 2 Then ∃m, n ∈ Z, b = cm and r = cn. (Def. of divides.) 3 a = bq + r, so a = cmq + cn. (Substitute equals.) 4 a = c(mq + n) (Arithmetic.) 5 So c divides a, and c is a common divisor of a, b and r. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Greatest Common Divisor, Part 2.

Show that the greatest common divisor of a and b is also the greatest common divisor of b and r. a. Show that gcd(a, b) ≤ gcd(b, r) gcd(a, b) is a common divisor of b and r. (Lemma 2.1a) Every element of a set is always ≤ the greatest element of that set. So gcd(a, b) ≤ gcd(b, r). b. Show that gcd(b, r) ≤ gcd(a, b) gcd(b, r) is also a common divisor of a and b. (Lemma 2.1b) Every element of a set is always ≤ the greatest element of that set. So gcd(b, r) ≤ gcd(a, b).

∴ gcd(a, b) = gcd(b, r) Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Euclid’s Algorithm

Find the gcd(a, b): First, find the remainder, r, of a divided by b. Then iterate, with b becoming the numerator, r the denominator, and finding a new remainder. a b r a b r 72 46 26 318 39 6 46 26 20 39 6 3 26 20 6 6 3 0 20 6 2 3 0 3 6 2 0 2 0 2 Why does this algorithm work? By Lemma 2, the gcd(a, b) on every line is equal to the gcd(a, b) on the following line. By Lemma 1, the gcd of any number and 0 is that number. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Greatest Common Divisor Algorithm

To calculate gcd(p, q) 1 If p < q, swap p and q. 2 Begin a loop: If r = 0, leave the loop and return q as the gcd. Else, let r = p mod q, then assign p = q and q = r. Repeat the loop from step 2. Example: gcd(10, 745) = gcd(745, 10) = gcd(10, 5) = gcd(5, 0) = 5 Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Greatest Common Divisor in C

int gcd (int p, int q) { int r; if (p*q == 0) return 0; if (q > p) { r=p, p=q, q=r; } for(;;){ r = p % q; if (r==0) break; p=q; q=r; } return q; } p q r return value 650 30 20 Compute gcd( 650, 30) 30 20 10 20 10 0 10 Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The Least Common Multiple

The Least Common Multiple of two non-zero integers a and b, denoted lcm(a, b) or LCM(a, b), is the positive integer c such that:

a | c and b | c, that is a and b are factors of c, and ∀m ∈ Z + , if a | m and b | m, then c ≤ m.

To compute the LCM, compute the GCD and divide:

lcm(a, b) = (a × b)/gcd(a, b) Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The GCD and LCM

For all positive integers, a and b, ab = lcm(a, b) × gcd(a, b).

Part 1 of proof: ab b a We can write gcd(a,b) = a × gcd(a,b) = b × gcd(a,b) ab So gcd(a,b) is a multiple of both a and b. This works because the gcd(a, b) is a factor of both a and b, so the divisions do not have any remainders. Because the LCM is the minimal multiple, it is true that ab gcd(a,b) ≥ lcm(a, b). Therefore, ab ≥ lcm(a, b) × gcd(a, b) But we need to prove that is it equal, not just ≥. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The GCD and LCM continued

Part 2 of proof: lcm(a, b) is divisible by both a and b, so we know lcm(a, b)/a and lcm(a, b)/b are both integers. ab a b We can write lcm(a,b) = lcm(a,b)/b = lcm(a,b)/a . (Arithmetic.) So lcm(a, b)/b is a common divisor of both a and b. Since the GCD is the greatest common divisor of a and b, lcm(a, b) ≤ gcd(a, b). Therefore, ab ≤ lcm(a, b) ∗ gcd(a, b). (Arithmetic) Combining parts 1 and 2, we see that ab = lcm(a, b) ∗ gcd(a, b). Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization

Prime Numbers

Definitions The set of prime numbers is infinite Prime factorization Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Prime numbers

A is an integer > 1 whose only factors are 1 and itself. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 Numbers that have other factors are called composite numbers. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The Sieve of Eratosthenes

The Sieve is an algorithm for enumerating the primes by eliminating all the composite numbers.

1 To begin, make a table of the odd numbers starting with 3. Make it as long as you can, to get the biggest primes you can. Also, put the first two primes, 2 and 3, on your list of primes. 2 Now go through your table and cross out all the multiples of 3 (that is, every third number in the table, starting with 3). 3 Then add the first un-crossed out number to your list of primes and cross out all its multiples. √ 4 Repeat from step 3 until the next prime >= n. Everything left is a prime because it has no factors smaller than itself. Computationally, this is very fast, but the size of the biggest prime it can compute is limited to the size of available memory. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The Sieve Illustrated

PRIMES, so far: 2 ODDS, 3 5 7 9 11 13 15 17 19 21 initially: 23 25 27 29 31 33 35 37 39 41

43 45 47 49 51 53 55 57 59 61

63 65 67 69 71 73 75 77 79 81

83 85 87 89 91 93 95 97 99 101

PRIMES, so far: 2, 3

ODDS, 0 5 7 0 11 13 0 17 19 0 after 23 25 0 29 31 0 35 37 0 41 crossing out the 43 0 47 49 0 53 55 0 59 61 3’s: 0 65 67 0 71 73 0 77 79 0

83 85 0 89 91 0 95 97 0 101 Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The Sieve – Continued

PRIMES, so far: 2, 3, 5

ODDS, 0 0 7 0 11 13 0 17 19 0 after crossing 23 0 0 29 31 0 0 37 0 41 out the 5’s: 43 0 47 49 0 53 0 0 59 61

0 0 67 0 71 73 0 77 79 0

83 0 0 89 91 0 0 97 0 101

PRIMES, so far: 2, 3, 5, 7

ODDS, 0 0 0 0 11 13 0 17 19 21 0 after crossing 23 0 0 29 31 0 350 37 0 41 out the 7’s: 43 0 47 490 0 53 0 0 59 61

0 0 67 0 71 73 0 770 79 0

83 0 0 89 910 0 0 97 0 101

The final chart shows all the primes up to 101. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Factorization vs. Prime Factorization.

There is more than one way to factor some composite integers. However, one or more of those factors will not be a prime number. Proof: Consider 12, which has two factorizations: 12 = 6 × 2 = 4 × 3. In both cases, one of the factors (6 or 4) is a . If these are factored into primes and re-ordered, the two prime factorizations are the same: 12 = 6 × 2 = 2 × 3 × 2 = 22 × 3 12 = 4 × 3 = 2 × 2 × 3 = 22 × 3 Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The Unique Prime Factorization Theorem

This theorem is also called the “fundamental theorem of arithmetic” Every integer greater than 1 is either a prime number or can be represented as the product of prime numbers. Moreover, this representation is unique. For example, 1200 = 24 × 31 × 52 = 5 × 2 × 5 × 2 × 3 × 2 × 2 = 52 × 3 × 24.

The theorem states that the factorization above is the only prime factorization. There will always be exactly four 2’s, one 3, two 5’s, and no other primes in the product.

The proof follows, in two parts. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Prime Factorization: Part 1

∀n ∈ Z + either n is prime, or n can be represented as the product of two or more primes. An algorithm to compute the prime factorization: √ Calculate s = d n e. Then find or calculate a table of primes th p1 < p2 < . . . < pm ≤ s. Thus, pk is the k smallest prime. Initialize k = 1 and q = n. Repeat:

1 If pk | q, output pk and set q = q/pk . Repeat step 1. 2 If q = 1, stop. 3 If k = m, output q and stop. 4 Otherwise set k to k+1 and go back to step 1. The sequence of outputs is the prime factorization of n. If q is output in step 3, then q is prime. Why? Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Prime Factorization: Part 2

The prime factorization found in Part 1 is unique. Proof by contradiction. 1 Assume that n is the smallest integer that has two different prime factorizations: n = p1 × p2 × ... × pm = q1 × q2 × ... × qt

2 None of the p’s = any of the q’s. If any pj = any qk , we could divide n by pj and get a smaller n’ with two factorizations.

3 Consider p1, which divides n.

4 Since n = q1 × q2 × ... × qm, then p1 must also divide at least one of the q’s. (Euclid’s Lemma) 5 This is a contradiction, since, by step 2, all of the q’s are prime and different from all of the p’s. 6 ∴ the assumption in step 1 is wrong, and the prime factorization is unique. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Prime Factorization: Practice

What is the prime factorization of these numbers? 1 101

2 91 PRIMES, so far: 2, 3, 5, 7

3 ODDS, 0 0 0 0 11 13 0 17 19 21 0 55 after crossing 0 4 23 0 0 29 31 0 35 37 0 41 69 out the 7’s: 43 0 47 490 0 53 0 0 59 61 5 128 0 0 67 0 71 73 0 770 79 0 6 What two numbers, 83 0 0 89 910 0 0 97 0 101 above, are relatively prime? Definition: Two numbers are relatively prime if they have no common factors. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Relatively Prime: Practice

Definition: Two numbers are relatively prime if they have no common factors.

Is each of these pairs of numbers relatively prime? 1 101 and 202 2 93 and 9 3 90 and 75 4 4 and 5 5 69 and 64 6 128 and 125 Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Factoring n and (n+1)

For any integer, a, and any prime number, p, if p is a factor of a then p is not a factor of (a + 1).

Suppose there is an integer, a, and a prime number, p, such that p is a factor of both a and a + 1. p ≥ 2 because p is a prime. By the definition of divisibility, there are integers r and s such that a = r × p and a + 1 = s × p. (a + 1) − a = (s × p) − (r × p). (Algebra) 1 = (s − r) × p. (Substitute equals) But (s − r) ≥ 1 and p ≥ 2, so (s − r) × p 6= 1 This contradiction indicates the original theorem is true. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Infinitely many prime numbers.

The set of prime numbers is infinite.

1 Assume the set of prime numbers is finite. Therefore there is a largest prime, p. 2 List the primes in ascending : 2, 3, 5, 7, 11, .... p 3 Let J = (2 × 3 × 5 × 7 × 11 × .... × p) + 1 4 By step 1, J must be divisible by some prime number, q, which is a member of our finite set of prime numbers, 2...p. 5 q must also be a factor of the product of all primes, 2 × 3 × .... × p. 6 q therefore cannot be a factor of J, which is one more than this product, according to step 3. 7 But we already stated q was a factor of J. This contradiction indicates the original theorem is true. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Calculating the GCD Using the Prime Factorization

To compute the gcd(m, n), write out the prime factorization of m and n. The GCD is the number composed of all the prime factors in both factorizations.

Example - calculate gcd(45, 84). 45 = 3 * 3 * 5 84 = 2 * 2 * 3 * 7 gcd(45, 84) = 3 because only one factor of 3 is common to both numbers. Example - calculate gcd(140, 90). 140 = 2 * 2 * 5 * 7 90 = 2 * 3 * 3 * 5 gcd(140, 90) = 2*5 = 10 because one 2 and one 5 are common to both numbers. Outline Euclid’s Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization Calculating the LCM Using the Prime Factorization

To compute the lcm(m, n), write out the prime factorization of m and n. The LCM is the number composed of the greatest quantity of each prime factor in either factorization.

An example - calculate the lcm(45, 84). 45 = 3 * 3 * 5 84 = 2 * 2 * 3 * 7 lcm(45, 84) = 2 * 2 * 3 * 3 * 5 * 7 = 1260 (2 * 2 * 7) comes from 84, (3 * 3 * 5) comes from 45.

Try it yourself - calculate lcm(140, 90).