CSE 54 Discrete Mathematics & Chapter 2 (Part 3): The Fundamentals: Algorithms, the Integers & Matrices Integers & Algorithms (Section 2.5) by Kenneth H. Rosen, Discrete Mathematics & its Applications, Fifth Edition, Mc Graw-Hill, 23 Integers & Algorithms (2.5) Introduction Algorithm is a set a procedures that handle arithmetic operations Use binary representations to enter the world of computer arithmetic Good illustration of the complexity of an algorithm Role of the Euclidean algorithm Base b expansion and modular exponentiation, important in cryptography Ch. 2 (Part 3) Section 2.5
CSE 54 Discrete Mathematics & Representations of integers Theorem Let b be a positive integer grater than. Then if n is a positive integer, it can be expressed uniquely in the form n = a k b k + a k- b k- + + a b + a, where k is a nonnegative integer, a, a,,a k are nonnegative integers less than b, and a k. The representation of n given in theorem is known as the base b expansion of n. Example: What is the decimal expansion of the integer that has ( ) 2 as its binary expansion? ( ) 2 = *2 8 + *2 7 + *2 6 + *2 5 + *2 4 + *2 3 + *2 2 + *2 + *2 = 35 Ch. 2 (Part 3) Section 2.5 2
CSE 54 Discrete Mathematics & Hexadecimal expansions = base 6 expansion,,2,3,4,5,6,7,8,9,a,b,c,d,e,f The letters A through F represent the digits corresponding to the number through 5. Example: What is the decimal expansion of the hexadecimal expansion of (2AEB) 6? (2AEB) 6 = 2*6 4 + *6 3 + 4*6 2 + *6 + *6 = (75627) Each hexadecimal digit can be represented using 4 bits. ( ) 2 = (E5) 6 (E 6 = () 2 ; 5 6 = () 2 ) Bytes: bit strings of length 8 represented by two hexadecimal digits. Representations of integers (cont.) Base conversion Example: Find the base 8 (or octal) expansion of (2345) First, divide 2345 by 8. 2345 = 8 * 543 + 543 = 8 * 92 + 7 92 = 8 * 24 + 24 = 8 * 3 + 3 = 8 * + 3 (2345) = (37) 8 Ch. 2 (Part 3) Section 2.5 3
CSE 54 Discrete Mathematics & Example: Find the hexadecimal expansion of ( ) 2 and the binary expansion of (A8D) 6.. Form blocks of 4 digits and add zeros at the start of the leftmost block if necessary. They are:,, and. 2. Convert each binary block into decimal () 2 = 3 6 ; () 2 = E 6 ; () 2 = B 6 ; () 2 = C 6 Therefore, ( ) 2 = (3EBC) 6. The conversion of (A8D) 6 into binary notation requires replacing each hexadecimal digit by a block of 4 binary digits. They are: A 6 = () 2, 8 6 = () 2, D 6 = () 2 Algorithms for integer operations Goal : Compute the sum of two integers expressed with their binary expansions. The complexity will be based on the number of bits used. a = (a n- a n-2 a a ) 2, b = (b n- b n-2 b b ) 2 a + b? (sum of two n-bits numbers) First: a + b = c * 2 + s where s = rightmost bit in the binary expansion of a + b c = carry = or. Second: a + b + c = c * 2 + s where s = next bit of the sum expansion c = carry a + b = (s n s n- s n-2 s s ) 2 Ch. 2 (Part 3) Section 2.5 4
CSE 54 Discrete Mathematics & Example: Add a = () 2 and b = () 2 a + b = + = * 2 + c =, s = a + b + c = + + = * 2 + c =, s = a 2 + b 2 + c = + + = * 2 + c 2 =, s 2 = a 3 + b 3 + c 2 = + + = * 2 + c 3 =, s 3 = s 4 = c 3 = a + b = ( ) 2 = a = b = c Goal 2: Compute the product since b = b n- b n-2 b b then: ab = a (b 2 + b 2 + + b n- 2 n- ) = a (b 2 ) + a(b 2 ) + + a(b n- 2 n- ) Using this latter equation, we can compute a * b. ab j = a if b j = and otherwise. Each time we multiply a term by 2, we shift its binary expansion one place to the left and add a zero at the tail end of the expansion. Ch. 2 (Part 3) Section 2.5 5
CSE 54 Discrete Mathematics & Example: Find the product of a = () 2 and b = () 2 ab 2 = () 2 * * 2 = () 2 ab 2 = () 2 * * 2 = () 2 (4 zeros-shift) ab 2 2 2 = () 2 * * 2 2 = () 2 add 2-zero bits To find the product, we need to perform: () 2 + () 2 + () 2 ab = () 2 Other exercise: check for example: 7 * 5 = 35 is true in binary expansion. Ch. 2 (Part 3) Section 2.5 6
CSE 54 Discrete Mathematics & The Euclidean algorithm Goal: Compute the great common divisor (gcd) of two integers avoiding the prime factorization of each number which is time-consuming. Illustration: Find gcd(9, 287). Divide 287 (the larger) by 9 (the smaller) 287 = 9 * 3 + 4 Any divisor of 9 and 287 must be a divisor of 287 9 * 3 =4 (Theorem, part & 2) Any divisor of 9 and 4 divides 287 = 9 * 3 + 4 gcd(9,287) = gcd(9,4) 2. Next divide 9 by 4 to obtain: 9 = 4 * 6 + 7 Using the same reasoning, our next step is to: 3. Divide 4 by 7: 4 = 7 * 2 since 7/4 gcd(4,7) = 7. But since gcd(287,9) = gcd(9,4) = gcd (4,7) = 7, we have solved the problem Ch. 2 (Part 3) Section 2.5 7
CSE 54 Discrete Mathematics & Lemma : Let a = bq + r, where a, b, q and r are integers. Then we can write: gcd(a,b) = gcd(b,r) Example: Determine the gcd of 44 and 662 using the Euclidean algorithm. 662 = 44 * + 248 44 = 248 * + 66 248 = 66 * + 82 66 = 82 * 2 + 2 82 = 2 * 4 Therefore: gcd(44,662) = 2 (since 2 is the last nonzero remainder!) Ch. 2 (Part 3) Section 2.5 8