이산수학 () 정수와알고리즘 (Integers and Algorithms) 2011년봄학기 강원대학교컴퓨터과학전공문양세
Introduction Base-b representations of integers. (b 진법표현 ) Especially: binary, hexadecimal, octal. Also, two s complement representation (2 의보수표현 ) Algorithms for computer arithmetic: Binary addition, multiplication, division. Euclidean algorithm for finding GCD s. Page 2
Base-b Representations of Integers If b is a positive integer greater than 1, then a given positive integer n can be uniquely represented as follows: n = a k-1 k b k + a k-1 b + + a 1 b 1 + a 0 b 0 where k is a natural number. and a 0, a 1,, and a k are a natural number less than b. a k 0. Example: 165 = 1 10 2 + 6 10 1 + 5 10 0 = (165) 10 165 = 2 8 2 + 4 8 1 + 5 8 0 = (245) 8 Page 3
Base-b Number Systems Ordinarily we write base-10 representations of numbers (using digits 0-9). However, 10 isn t special; any base b>1 will work. For any positive integers n, b, there is a unique sequence a k a k-1 a 1 a 0 of digits a i <b such that n k i 0 i a i b The base b expansion of n (n 의밑수b 전개, n의 b진법표현 ) Page 4
Particular Bases of Interest Base b=10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9. Base b=2 2 (binary): 2 digits: 0,1. ( Bits = binary digits. ) Base b=8 (octal): 8 digits: 0,1,2,3,4,5,6,7. Used only because we have 10 fingers Used internally in all modern computers Octal digits correspond to groups of 3 bits Base b=16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F Hex digits give groups of 4 bits Page 5
Converting to Base b (1/2) Informal Algorithm 1. To convert any integer n to any base b>1: 2. To find the value of the rightmost (lowest-order) digit, simply compute n mod b. (n%b 로가장끝자리 (digit) 를찾는다.) 3. Now replace n with the quotient n/b. ( 다음자리 (digit) 을구하기위하여, 몫을 n 으로삼는다.) 4. Repeat above two steps to find subsequent digits, until n is gone (=0). ( 단계 2/3 을 n 이 0 이될때까지반복한다.) (177130) 10 = (?) 16 (241) 10 = (?) 2 177130 = 16 11070 + 10 241 = 2 120 + 1, 120 = 2 60 + 0 11070 = 16 691691 + 14 60 = 2 30 + 0, 30 = 2 15 + 0 691 = 16 43 + 3 15 = 2 7 + 1, 7 = 2 3 + 1 43 = 16 2 + 11 3 = 2 1 + 1, 1 = 2 0 + 1 2 = 16 0 + 2 (241) 10 = (11110001) 2 (177130) 10 = (2B3EA) 16 Page 6
Converting to Base b (2/2) Formal Algorithm procedure base b expansion (n: positive integer) q := n k := 0 while q 0 begin a k := q mod b {remainder} q := q/b {quotient} k := k + 1 end {the base b expansion of n is (a k a k-1 a 1 a 0 ) b } Page 7
Addition of Binary Numbers Intuition (let a = (a n-1 a 1 a 0 ) 2, b = (b n-1 b 1 b 0 ) 2 ) c n-1 c n-2... c 1 c 0 c i = (a i-1 +b i-1 +c i-1 )/2 a = a n-1 a n-2... a 2 a 1 a 0 b = b n-1 b n-2... b 2 b 1 b 0 a+b = s n s n-1 s n-2... s 2 s 1 s 0 s i = (a i +b i +c i )%2 Algorithm procedure add(a n 1 a 0, b n 1 b 0 : binary expressions of a,b) c := 0 {c mean a carry} for i := 0 to n 1 {i means a bit index} begin sum := a i + b i + c {2-bit sum} O(n) s i := sum mod 2 {low bit of sum} c := sum/2 {high bit of sum} end s n := c {the binary expression of the sum is (s n s n-1 s 1 s 0 ) 2 } Page 8
2 s Complement (2의보수 ) (1/2) In binary, negative numbers can be conveniently represented using 2 s s complement notation. ( 실제로, 컴퓨터에서는음수를 2 의보수로표현한다.) In this scheme, a string of n bits can represent integers 2 n 1 ~ (2 n 1 1). 0 이상의정수만표현한다면 0 ~ 2 n 1 (unsigned int n) 음의정수까지표현한다면 2 n-1 ~ 2 n-1 1 (int n) The bit in the highest-order h bit-position iti (n 1) represents a coefficient multiplying 2 n 1 ; ( 왼쪽첫번째 bit 는 22 n 1 을나타내며, 흔히부호 (+ or ) ) 를의미한다.) The other positions i < n 1 just represent 2 i, as before. Page 9
2 s Complement (2의보수 ) (2/2) The negation of any n-bit 2 s complement number a(= a n 1 a 0 ) is given by a n 1 a 0 + 1. Bitwise logical complement of a Examples 1011 = (0100 + 1) = (0101) = (5) 10 0100 = +0100 = (4) 10 Page 10
Subtraction of Binary Numbers Theorem: For an integer a represented in 2 s complement notation, a = a + 1. (a 가 2 보수로표현된다면, ) Proof: Just try it by yourself! Algorithm procedure subtract (a n 1 a 0, b n 1 b 0 : binary 2 s complement expressions of a,b) return add(a, add(b, 1)) { a + ( b) } Page 11
Multiplication of Binary Numbers (1/2) Intuition (let a = (a n-1 a 1 a 0 ) 2, b = (b n-1 b 1 b 0 ) 2 ) a = a n-1 a n-2... a 2 a 1 a 0 b = b n-1 b n-2... b 2 b 1 b 0 c 0 = s (n-1,0) s (n-2,0)... s (2,0) s (1,0) s (0,0) c 1 = s (n-1,1) s (n-2,1)... s (2,1) s (1,1) s (0,1) 0 c 2 = s (n-1,2) s (n-2,2)... s (2,2) s (1,2) s (0,2) 0 0 +)...... a b = c n-1 + c n-2 +... + c 2 + c 1 + c 0 s (i,j) = (if b j = 1 then a i else 0) c j = (if b j = 1 then a << j else 0) Page 12
Multiplication of Binary Numbers (2/2) Algorithm procedure multiply(a n 1 a 0, b n 1 b 0 : binary expressions of a,b) for j := 0 to n 1 {a bit index for b} begin if b j = 1 then c j := a shifted j places {c j := a << j} else c j := 0 end {c 1, c 2,, c n-1 are the partial products.} p := 0 for j := 0 to n 1 p := add(p, c j ) {p is the value of ab} O(n 2 ) Can be reduced to O(n 1.585 ) Page 13
Division Algorithm Example: 23/4? r q Algorithm 23 4 = 19 1 19 4 = 15 2 15 4 = 11 3 11 4 = 7 4 7 4 = 3 5 procedure division(a, d: positive integer) q := 0 r := a while r d begin r := r d q := q + 1 end {q is the quotient(= a/d ), r is the remainder(=a%d)} Page 14
Euclid s Algorithm for GCD Finding GCDs by comparing prime factorizations can be difficult if the prime factors are unknown. ( 소인수분해로최대공약수를구하는것은어렵다. 특히, 큰수인경우 ) Euclid discovered: For all integers a, b, gcd(a, b) = gcd((a mod b), b). Sort a,b so that t a>b, and then (given b>1) (a mod b) < a, so problem is simplified. Examples gcd(372, 164) = gcd(372 mod 164, 164) [372%164 = 44] gcd(164, 44) = gcd(164 mod 44, 44) [164%44 = 32] gcd(44, 32) = gcd(44 mod 32, 32) = gcd(12, 32) = gcd(12, 8) gcd(8, 4) = gcd(4, 0) = 4. Page 15
Proof of Euclid s Algorithm Prove gcd(a,b) = gcd(b,r) where r = a bq a 와 b 의공약수는 b 와 r 의공약수가같음을보이면, 양쪽공약수의쌍이같으므로 ( 최대공약수가같아져 ) 증명이이루어진다. d 를 a 와 b 의공약수라하자. 그러면, a = dq a, b = dq b 가성립한다. 정의에의해, r = dq a dq b q= d(q a q b q) ) 이성립하므로, d 는 r 의약수이다. 따라서, d 는 b 와 r 의공약수이다. d 를 b 와 r 의공약수라하자. 그러면, b = dq b, r = dq r 이성립한다. 정의에의해, a = dq r + dq b q= d(q r + q b q) 이성립하므로, d 는 a 의약수이다. 따라서, d 는 a 와 b 의공약수이다. Page 16
Euclid s Algorithms Algorithm in Pseudocode procedure gcd(a, b: positive integer) while b 0 begin r := a mod b {r = a % b} a := b b := r end return a Algorithm in C (using recursive calls) int gcd(int a, int b) /* assume a > b */ { if(b==0) return a; else return gcd(b, a%b); } Page 17