university of hawaii ics141: discrete mathematics …janst/141/lecture/18-integeralgorithms.pdfics...

29
13-1 ICS 141: Discrete Mathematics I – Fall 2011 University of Hawaii 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: others

Post on 19-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

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

University of Hawaii 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

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

University of Hawaii University of Hawaii Quiz

1.  gcd(84,96) = 2.  lcm(84,96) = 3.  gcd(84,96) x lcm(84,96)=

n  Hints n  What's the prime factorization of 84? n  What's the prime factorization of 96?

n  Try the primes 2, 3 and 7

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

University of Hawaii University of Hawaii

Lecture 18

Chapter 3. The Fundamentals 3.6 Integers and Algorithms

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

University of Hawaii University of Hawaii Review: 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  If the prime factorizations are written as and , then the GCD is given by:

n  Example: 84 = 22·31·71 and 96 = 25·31·70

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

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

),min(1

2211 nn ban

baba pppba …=

nan

aa pppa …2121=

nbn

bb pppb …2121=

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

University of Hawaii University of Hawaii Review: 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  If the prime factorizations are written as and , then the LCM is given by:

n  Example: 84 = 22·31·71 and 96 = 25·31·70

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

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

),max(1

2211 nn ban

baba pppba …=

nan

aa pppa …2121=

nbn

bb pppb …2121=

13-6 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)

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

University of Hawaii University of Hawaii Integers and Algorithms

n  Topics: n  Base-b representations of integers.

n  Especially: binary, hexadecimal, octal.

n  Algorithms for computer arithmetic: n  Binary addition and multiplication.

n  Euclidean algorithm for finding GCD’s.

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

University of Hawaii University of Hawaii Base-b Number Systems n  Ordinarily, we write base-10 representations

of numbers, using digits 0-9. n  But, 10 isn’t special! Any base b > 1 will work. n  For any positive integers n and b, there is a

unique sequence akak-1… a1a0 of digits ai < b such that:

∑=

−−

−−

=

+++++=k

i

ii

kk

kk

kk

ba

ababababan

0

01

12

21

1

The “base-b expansion of n”

n  Notation: n = (akak-1… a1a0)b

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

University of Hawaii University of Hawaii Particular Bases of Interest

n  Base b = 10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9.

n  Base b = 2 (binary): 2 digits: 0,1. (“Bits”=“binary digits.”)

n  Base b = 8 (octal): 8 digits: 0,1,2,3,4,5,6,7.

n  Base b = 16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Used internally in all modern computers

Octal digits correspond to groups of 3 bits

Hex digits give groups of 4 bits

Used only because we have 10 fingers

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

University of Hawaii University of Hawaii Examples n  Example 1: Decimal expansion of the integer with

binary expansion (101011111)2? n  (101011111)2

= 1·28 + 0·27 + 1·26 + 0·25 + 1·24 + 1·23 + 1·22 + 1·2 + 1 = (351)10

n  Example 2: Decimal expansion of the integer with hexadecimal expansion (2AE0B)16? n  (2AE0B)16 = 2·164 + 10·163 + 14·162 + 0·16 + 11

= (175627)10

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

University of Hawaii University of Hawaii Converting to Base b (An algorithm, informally stated.) n  To convert any integer n to any base b > 1: n  To find the value of the rightmost (lowest-

order) digit, simply compute n mod b. n  Now, replace n with the quotient. n  Repeat above two steps to find subsequent

digits, until n is gone (= 0).

Exercise: Write this out in pseudocode…

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

University of Hawaii University of Hawaii Converting to Base b

baaaaabababa

abaababbabaabqbabaabqb

abaqbaabqb

abqn

)(

)0(

)(

)(

0123

012

23

3

0122

33

0122

23

01222

0112

011

00

=

+++=

++++⋅=

+++=

+++=

++=

++=

+=

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

University of Hawaii University of Hawaii Examples n  Example 3: Find the base 8, i.e. octal,

expansion of (12345)10 n  12345 = 8·1543 + 1 n  1543 = 8·192 + 7 n  192 = 8·24 + 0 n  24 = 8·3 + 0 n  3 = 8·0 + 3 n  Therefore, (12345)10 = (30071)8

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

University of Hawaii University of Hawaii Binary ↔ Hexadecimal

n  Hexadecimal expansion of (11 1110 1011 1100)2

∴ (11 1110 1011 1100)2 = (3EBC)16

n  Binary expansion of (A8D)16 (A)16 = (1010)2, (8)16 = (1000)2, (D)16 = (1101)2

∴ (A8D)16 = (1010 1000 1101)2

E B C 0011 = 3

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

University of Hawaii University of Hawaii Addition of Binary Numbers Carry:

10111 + 11100 n  Carry = ⎣bitSum / 2⎦ n  sbitIndex = bitSum mod 2 = bitSum – 2⋅carry

1

0 0

1

0

0

1

0

1

1

1

1

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

University of Hawaii University of Hawaii Addition of Binary Numbers procedure add(an−1…a0, bn−1…b0: binary representations of

non-negative integers a, b) carry := 0 for bitIndex := 0 to n−1 begin {go through bits} bitSum := abitIndex+bbitIndex+carry {2-bit sum} carry := ⎣bitSum / 2⎦ {high bit of sum} sbitIndex := bitSum – 2⋅carry {low bit of sum} end sn := carry return sn…s0: binary representation of integer s

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

University of Hawaii University of Hawaii Multiplication of Binary Numbers

n  ab = a(b0·20 + b1·21 + ··· + bn-1·2n-1) = a(b0·20 ) + a(b1·21 ) + ··· + a(bn-1·2n-1)

110 a x 101 b

110 000

110 11110

shift 1 bit to the left, i.e. append 1 extra 0-bit 0 shift 2 bit to the left, i.e. append 2 extra 0-bits 00

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

University of Hawaii University of Hawaii Multiplication of Binary Numbers n  ab = a(b0·20 + b1·21 + ··· + bn-1·2n-1)

= a(b0·20 ) + a(b1·21 ) + ··· + a(bn-1·2n-1) procedure multiply(an−1…a0, bn−1…b0: binary

representations of positive integers a,b) product := 0 for i := 0 to n−1 if bi = 1 then product := add(an−1…a00···0, product) return product

i extra 0-bits appended after the digits of a

i times

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

University of Hawaii University of Hawaii Division with Remainder procedure div-mod(a∈Z,d∈Z+)

{quotient & remainder of a/d} q := 0 r := |a| while r ≥ d begin r := r – d q := q + 1 end if a < 0 and r > 0 then begin {a is a negative}

r := d – r q := –(q + 1) end {q = a div d (quotient), r = a mod d (remainder)}

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

University of Hawaii University of Hawaii Euclid’s Algorithm for GCD n  Finding GCDs by comparing prime

factorizations can be difficult when the prime factors are not known!

n  Euclid discovered: Let a = bq +r, where a, b, q, and r are integers. Then gcd(a,b) = gcd(b,r) (i.e. gcd(a,b) = gcd(b, (a mod b))) n  Example: gcd(36, 24) = gcd(24, 12)

n  Sort a, b so that a > b, and then (given b > 1) (a mod b) < b, so problem is simplified.

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

University of Hawaii University of Hawaii Euclid’s Algorithm Example n  gcd(372, 164) = gcd(164, 372 mod 164)

n  372 mod 164 = 372 - 164⎣372/164⎦ = 372 - 164·2 = 372 - 328 = 44

n  gcd(164, 44) = gcd(44, 164 mod 44) n  164 mod 44 = 164 - 44⎣164/44⎦

= 164 - 44·3 = 164 - 132 = 32

n  gcd(44, 32) = gcd(32, 44 mod 32) = gcd(32, 12) = gcd(12, 32 mod 12) = gcd(12, 8) = gcd(8, 12 mod 8) = gcd(8, 4) = gcd(4, 8 mod 4) = gcd(4, 0) = 4

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

University of Hawaii University of Hawaii Euclid’s Algorithm Pseudocode procedure gcd(a, b: positive integers)

x : = a y : = b while y ≠ 0 begin r := x mod y; x := y; y := r; end return x {x = gcd(a, b)}

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

University of Hawaii University of Hawaii Proof That Euclid’s Algorithm Works

n  Theorem 0: gcd(a,b) = gcd(b,c) if c = a mod b. Proof:

n  First, c = a mod b implies ∃t: a = bt + c. n  Let g = gcd(a,b), and gʹ′ = gcd(b,c). n  Since g|a and g|b (thus g|bt) we know g|(a−bt), i.e. g|c.

Since g|b ∧ g|c, it follows that g ≤ gcd(b,c) = gʹ′. n  Now, since gʹ′|b (thus gʹ′|bt) and gʹ′|c, we know gʹ′|(bt+c),

i.e., gʹ′|a. Since gʹ′|a ∧ gʹ′|b, it follows that gʹ′ ≤ gcd(a,b) = g.

n  Since we have shown that both g ≤ gʹ′ and gʹ′ ≤ g, it must be the case that g = gʹ′. ■

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

University of Hawaii University of Hawaii Two’s Complement n  In binary, negative numbers can be

conveniently represented using two’s complement notation.

n  In this scheme, a string of n bits can represent any integer i such that −2n−1 ≤ i < 2n−1.

n  The leftmost bit is used to represent the sign (0:positive, 1:negative integer)

n  The negation of any n-bit two’s complement number a = an−1…a0 is given by an−1…a0 + 1.

The bitwise logical complement of the n-bit string an−1…a0.

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

University of Hawaii University of Hawaii Two’s Complement Example

value 3-bit pattern 3 0 1 1 2 0 1 0 1 0 0 1 0 0 0 0

–1 1 1 1 –2 1 1 0 –3 1 0 1 –4 1 0 0

n  To obtain the results for –4 ≤ n ≤ –1, consider |n|, then n  In the binary representation of

|n|, replace each 0 by 1, and each 1 by 0, This is the one’s complement of n.

n  Add 1 (i.e. 001) to the result from the previous step. This is the two’s complement of n.

n  Example n  –3: 011 -> 100

+ 001 = 101

−22 ≤ i < 22

(−2n−1 ≤ i < 2n−1)

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

University of Hawaii University of Hawaii Subtraction of Binary Numbers

procedure subtract(an−1…a0, bn−1…b0: binary two’s complement reps. of integers a, b) return add(a, add(b,1)) { a + (−b) }

n  Note that this fails if either of the adds causes a carry into or out of the n−1 position, since 2n−2+2n−2 ≠ −2n−1, and −2n−1 + (−2n−1) = −2n isn’t representable! We call this an overflow.

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

University of Hawaii University of Hawaii Modular Exponentiation n  Problem: Given large integers b (base),

n (exponent), and m (modulus), efficiently compute bn mod m. n  Note that bn itself may be completely

infeasible to compute and store directly. n  E.g. if n is a 1,000-bit number, then bn itself

will have far more digits than there are atoms in the universe!

n  Yet, this is a type of calculation that is commonly required in modern cryptographic algorithms!

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

University of Hawaii University of Hawaii Algorithm Concept

00

22

11

00

22

11

)()()( 222

222

nnn

nnnn

bbb

bb

kk

kk

kk

kk

×××=

=

−−

−−

−−

−− ⋅++⋅+⋅

…n  Note that:

n  We can compute b to various powers of 2 by repeated squaring. n  Then multiply them into the partial product, or not,

depending on whether the corresponding ni bit is 1. n  Crucially, we can do the mod m operations as we go

along, because of the various identity laws of modular arithmetic.

n  All the numbers stay small.

The binary expansion of n

= b1 = b

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

University of Hawaii University of Hawaii Modular Exponentiation procedure modularExponentiation(b: integer,

n = (nk−1…n0)2, m: positive integers) x := 1 {accumulates the result} b2i := b mod m { mod m; i=0 initially} for i := 0 to k−1 begin {go thru all k bits of n} if ni = 1 then x := (x·b2i) mod m

b2i := (b2i·b2i) mod m end return x {x equals bn mod m}

i

b2

)()( 22222 1 iiii

bbbb ⋅== ⋅+