The Integers
Total Page:16
File Type:pdf, Size:1020Kb
Outline Euclid's Algorithm for GCD The Least Common Multiple Prime numbers Prime Factorization The Integers 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 Divisor The greatest common divisor (gcd) of two integers, a; b, is the largest integer that divides both. Formally, the gcd(a; b) is the greatest integer d such that d j a and d j 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 2 Z +, gcd(x; 0) = x Proof: 0 divided by anything is 0 with no remainder. (Arithmetic.) 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 2 Z and either a 6= 0 or b 6= 0. if a = bq + r then gcd(a; b) = gcd(b; r) Proof by division 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 9m; n 2 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 9m; n 2 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 j c and b j c, that is a and b are factors of c, and 8m 2 Z + , if a j m and b j 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 prime number 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. p 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.