university of hawaii ics141: discrete mathematics for...

31
13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) University of Hawaii ICS141: Discrete Mathematics for Computer Science I Dept. Information & Computer Sci., University of Hawaii Jan Stelovsky based on slides by Dr. Baek and Dr. Still Originals by Dr. M. P. Frank and Dr. J.L. Gross Provided by McGraw-Hill

Upload: ledien

Post on 29-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-1

University of Hawaii

ICS 141: Discrete Mathematics I (Spring 2011)

University of Hawaii

ICS141: Discrete Mathematics for Computer Science I

Dept. Information & Computer Sci., University of Hawaii

Jan Stelovsky based on slides by Dr. Baek and Dr. Still

Originals by Dr. M. P. Frank and Dr. J.L. Gross Provided by McGraw-Hill

Page 2: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-2 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Quiz

n  What is the big-theta of a polynomial?

n  What is the worst-case algorithmic complexity of: n  linear search n  binary search n  bubble sort n  insertion sort

n  Use pseudocode to define the "division algorithm"

Page 3: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-3

University of Hawaii

ICS 141: Discrete Mathematics I (Spring 2011)

University of Hawaii

Lecture 17

Chapter 3. The Fundamentals 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors

Page 4: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-4 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii The Division “Algorithm” n  It’s really just a theorem, not an algorithm…

n  Only called an “algorithm” for historical reasons.

n  Theorem: For any integer dividend a and divisor d∈Z+, there are unique integers quotient q and remainder r∈N such that a = dq + r and 0 ≤ r < d. Formally, the theorem is:

∀a∈Z, d∈Z+: ∃!q,r∈Z: 0 ≤ r < d, a = dq + r

n  We can find q and r by: q = ⎣a/d⎦, r = a - dq

Page 5: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-5 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii The mod Operator n  An integer “division remainder” operator.

n  Let a,d∈Z with d > 1. Then a mod d denotes the remainder r from the division “algorithm” with dividend a and divisor d; i.e. the remainder when a is divided by d. Also, a div d denotes the quotient q.

n  We can compute (a mod d) by: a - d·⎣a/d⎦ = a - dq.

n  In C/C++/Java languages, “%” = mod.

Page 6: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-6 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii The mod Operator: Examples

n  101 = 11·9 + 2 (dividend: 101, divisor: 11) n  101 div 11 = 9 101 mod 11 = 2

n  –11 = 3·(–4) + 1 or –11 = 3·(–3) – 2 ? (dividend: –11, divisor: 3) n  –11 div 3 = –4 –11 mod 3 = 1

(quotient: –4, remainder: 1) n  Note that the remainder must not be negative.

Page 7: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-7 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Modular Congruence n  Let a,b∈Z, m∈Z+, where Z+ = {n∈Z | n > 0} =

N − {0} (the positive integers).

n  Then a is congruent to b modulo m, written “a ≡ b (mod m)”, iff m|(a - b).

n  Note: this is a different use of “≡” than the meaning “equivalent” or “is defined as” used before.

n  It’s also equivalent to: (a - b) mod m = 0.

n  E.g. 17 ≡ 5 (mod 6), 24 ≢ 14 (mod 6)

Page 8: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-8 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Useful Congruence Theorems n  Theorem: Let a,b∈Z, m∈Z+. Then:

a ≡ b (mod m) ⇔ a mod m = b mod m.

n  Theorem: Let a,b∈Z, m∈Z+. Then: a ≡ b (mod m) ⇔ ∃k∈Z: a = b + km.

n  Theorem: Let a,b,c,d∈Z, m∈Z+. Then if a ≡ b (mod m) and c ≡ d (mod m), then: ▪ a + c ≡ b + d (mod m), and ▪ ac ≡ bd (mod m)

Page 9: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-9 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Congruence Theorem Example

n  7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5).

n  7 + 11 = 18 and 2 + 1 = 3 Therefore, 7 + 11 ≡ 2 + 1 (mod 5)

n  7 × 11 = 77 and 2 × 1 = 2 Therefore, 7 × 11 ≡ 2 × 1 (mod 5)

Page 10: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-10 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Applications of Congruence n  Hashing Functions (hashes)

n  Pseudorandom Numbers

n  Cryptology

n  Universal Product Codes

n  International Standard Book Numbers

Page 11: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-11 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Hashing Functions n  We want to quickly store and retrieve records in memory locations. n  A hashing function takes a data item to be stored or retrieved and

computes the first choice for a location for the item. n  h(k) = k mod m

n  A hashing function h assigns memory location h(k) to the record that has k as its key.

n  h(064212848) = 064212848 mod 111 = 14 n  h(037149212) = 037149212 mod 111 = 65 n  h(107405723) = 107405723 mod 111 = 14 ⇒ collision! n  Find the first unoccupied memory location after the occupied memory.

n  In this case, assign memory location 15. n  If collision occurs infrequently, and if when one does occur it is resolved

quickly, then hashing provides a very fast method of storing and retrieving data.

Page 12: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-12 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Cryptology (I) n  The study of secret messages

n  Encryption is the process of making a message secret. Decryption is the process of determining the original message from the encrypted message.

n  Some simple early codes include Caesar’s cipher: n  Assign an integer from 0 to 25 to each letter based

on its position in the alphabet. n  Caesar's encryption method: f(p) = (p + 3) mod 26 n  Caesar's decryption method: f –1(p) = (p – 3) mod 26 n  MEET YOU IN THE PARK ⇒

PHHW BRX LQ WKH SDUN

Page 13: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-13 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Cryptology (II) n  Caesar's encryption method does not provide a high

level of security n  A slightly better approach: f(p) = (ap + b) mod 26

n  Example 10: What letter replaces the letter K when the function f(p) = (7p + 3) mod 26 is used for encryption?

n  10 represents K n  f(10) = (7×10 + 3) mod 26 = 73 mod 26 = 21 n  21 represents V n  Therefore, K is replaced by V in the encrypted

message

Page 14: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-14 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Prime Numbers

n  An integer p > 1 is prime iff the only positive factors of p are 1 and p itself.

n  Some primes: 2, 3, 5, 7, 11, 13,...

n  Non-prime integers greater than 1 are called composite, because they can be composed by multiplying two integers greater than 1.

Page 15: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-15 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii The Fundamental Theorem of Arithmetic

n  Every positive integer greater than 1 has a unique representation as a prime or as the product of a non-decreasing series of two or more primes. n  Some examples:

n  2 = 2 (a prime 2) n  4 = 2·2 = 22(product of series 2, 2) n  2000 = 2·2·2·2·5·5·5= 24·53 2001 = 3·23·29 2002 = 2·7·11·13 2003 = 2003 (no clear pattern!)

Page 16: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-16 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Prime Numbers: Theorems n  Theorem 2: If n is a composite integer, then n

has a prime divisor less than or equal to n  Proof: If n is composite then we have n = ab for

1 < a < n and a positive integer b greater than 1. Show that a ≤ or b ≤ . (Use proof by contradiction) If a > and b > , then ab > n (Contradiction!) Therefore, a ≤ or b ≤ , i.e. n has a positive divisor not exceeding (a or b). By the Fundamental Theorem of Arithmetic, this divisor is either a prime, or has a prime divisor less than itself. In either case, n has a prime divisor less than or equal to

n

nn

nnnn

n

n

Page 17: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-17 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Prime Numbers: Theorems n  Contrapositive of Theorem 2:

An integer is prime if it is not divisible by any prime less than or equal to its square root

n  Example: Show that 101 is prime n  Primes not exceeding : 2, 3, 5, 7 n  101 is not divisible by any of 2, 3, 5, or 7 n  Therefore, 101 is a prime

n

101

Page 18: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-18 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Prime Factorization n  Example 4: Fine the prime factorization of

7007 ( )

n  Perform division of 7007 by successive primes 7007 / 7 = 1001 (7007 = 7·1001)

n  Perform division of 1001 by successive primes beginning with 7 1001 / 7 = 143 (7007 = 7·7·143)

n  Perform division of 143 by successive primes beginning with 7 143 / 11 = 13 (7007 = 7·7·11·13 = 72·11·13)

83.77007 ≈

Page 19: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-19 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Greatest Common Divisor n  The greatest common divisor gcd(a,b) of

integers a, b (not both 0) is the largest integer d that is a divisor both of a and of b.

d = gcd(a,b) = max(d: d|a ∧ d|b) ⇔ d|a ∧ d|b ∧ ∀e∈Z, (e|a ∧ e|b) → (d ≥ e)

n  Example: gcd(24,36) = ? n  Positive divisors of 24: 1, 2, 3, 4, 6, 8, 12, 24 n  Positive divisors of 36: 1, 2, 3, 4, 6, 9, 12, 18, 36 n  Positive common divisors: 1, 2, 3, 4, 6, 12.

The largest one of these is 12.

Page 20: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-20 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Relative Primality n  Integers a and b are called relatively prime or

coprime iff their gcd = 1.

n  Example: Neither 21 nor 10 is prime, but they are relatively prime. (divisors of 21: 1, 3, 7, 21; divisors of 10: 1, 2, 5, 10; so they have no common factors > 1, so their gcd = 1.

n  A set of integers {a1, a2, a3,…} is pairwise relatively prime if all pairs (ai, aj), for i ≠ j, are relatively prime.

Page 21: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-21 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii GCD Shortcut n  If the prime factorizations are written as

and , then the GCD is given by:

n  Example of using the shortcut: n  a = 84 = 2·2·3·7 = 22·31·71 n  b = 96 = 2·2·2·2·2·3 = 25·31·70

n  gcd(84,96) = 22·31·70 = 2·2·3 = 12.

nan

aa pppa …2121= nb

nbb pppb …2121=

.),gcd( ),min(),min(2

),min(1

2211 nn ban

baba pppba …=

Page 22: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-22 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Least Common Multiple n  lcm(a,b) of positive integers a, b, is the smallest

positive integer that is a multiple both of a and of b. E.g. lcm(6,10) = 30

m = lcm(a,b) = min(m: a|m ∧ b|m) ⇔ a|m ∧ b|m ∧ ∀n∈Z: (a|n ∧ b|n) → (m ≤ n)

n  Example: lcm(24,36) = ? n  Positive multiples of 24: 24, 48, 72, 96, 120, 144,… n  Positive multiples of 36: 36, 72, 108, 144,… n  Positive common multiples: 72, 144,…

The smallest one of these is 72.

Page 23: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-23 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii LCM Shortcut

.),(lcm ),max(),max(2

),max(1

2211 nn ban

baba pppba …=

n  If the prime factorizations are written as and ,

then the LCM is given by

n  Example of using the shortcut: n  a = 84 = 2·2·3·7 = 22·31·71 n  b = 96 = 2·2·2·2·2·3 = 25·31·70

n  lcm(84,96) = 25·31·71 = 32·3·7 = 672

nan

aa pppa …2121= nb

nbb pppb …2121=

Page 24: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-24 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii LCM: Another Example

n  Example 15: What is the least common multiple of

23·35·72 and 24·33?

n  Solution:

lcm(23·35·72, 24·33)

= 2max(3,4) ·3max(5,3) ·7max(2,0)

= 24·35·72

Page 25: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-25 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii GCD and LCM n  Theorem: Let a and b be positive integers. Then

ab = gcd(a,b) × lcm(a,b)

n  Example n  a = 84 = 2·2·3·7 = 22·31·71 n  b = 96 = 2·2·2·2·2·3 = 25·31·70

n  ab = (22·31·71)·(25·31·70) = 22·31·70·25·31·71 = 2min(2,5)·3min(1,1)·7min(1,0)·2max(2,5)·3max(1,1)·7max(1,0)

= gcd(a,b) × lcm(a,b)

Page 26: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-26 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Spiral Visualization of mod

≡ 3 (mod 5)

≡ 2 (mod 5)

≡ 1 (mod 5)

≡ 0 (mod 5)

≡ 4 (mod 5)

0 1

2 3

4

5

6

7 8

9

10

11

12 13

14

15

16

17 18

19

20

21

22

n  Example shown: modulo-5 arithmetic

Page 27: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-27 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Pseudorandom Numbers n  Numbers that are generated deterministically,

but that appear random for all practical purposes. n  We need to repeat the same sequence when testing!

n  Used in many applications, such as: n  Hash functions n  Simulations, games, graphics n  Cryptographic algorithms

n  One simple common pseudo-random number generating procedure: n  The linear congruential method

n  Uses the mod operator

Page 28: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-28 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Linear Congruential Method n  Requires four natural numbers:

n  The modulus m, the multiplier a, the increment c, and the seed x0.

n  where 2 ≤ a < m, 0 ≤ c < m, 0 ≤ x0 < m. n  Generates the pseudo-random sequence {xn}

with 0 ≤ xn < m, via the following: xn+1 = (axn + c) mod m

n  Tends to work best when a, c, m are prime, or at least relatively prime.

n  If c = 0, the method is called a pure multiplicative generator.

Page 29: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-29 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Example n  Let modulus m = 1,000 = 23·53.

n  To generate outputs in the range 0-999.

n  Pick increment c = 467 (prime), multiplier a = 293 (also prime), seed x0 = 426.

n  Then we get the pseudo-random sequence:

x1 = (ax0 + c) mod m = 285

x2 = (ax1 + c) mod m = 972

x3 = (ax2 + c) mod m = 263 and so on…

Page 30: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-30 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Prime Numbers: Theorems n  Theorem 3: There are infinitely many primes. (Euclide)

n  Assume: there are only finite many primes p1, p2,…, pn n  Let Q = p1p2···pn + 1 n  Then, Q is prime or it can be written as the product of two

or more primes (by Fundamental Theorem of Arithmetic) n  None of the primes pi divides Q

(if pi|Q then pi|(Q – p1p2···pn), i.e. pi|1) n  Hence there is a prime not in the list p1, p2,…, pn, which

is either Q itself or a prime factor of Q (CONTRADICTION!!)

Page 31: University of Hawaii ICS141: Discrete Mathematics for ...janst/141/lecture/17-IntegersDivision.pdf · 13-1 University of Hawaii ICS 141: Discrete Mathematics I (Spring 2011) ICS141:

13-31 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Mersenne Primes n  Definition: A Mersenne prime is a prime number

of the form 2p – 1, where p is prime. prime p 2p – 1 Mersenne?

2 22 – 1 = 3 yes 3 23 – 1 = 7 yes 5 25 – 1 = 31 yes 7 27 – 1 = 127 yes

11 211 – 1 = 2047 = 23·89 no 11,213 211,213 – 1 yes 19,937 219,937 – 1 yes

3,021,377 23,021,377 – 1 Yes (late 1998) 43,112,609 243,112,609 – 1 Yes (MID 2008)

largest Mersenne prime known (with almost 13 million digits)