Presentation is loading. Please wait.

Presentation is loading. Please wait.

이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.

Similar presentations


Presentation on theme: "이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과."— Presentation transcript:

1 이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과

2 Discrete Mathematics by Yang-Sae Moon Page 2 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. 2.5 Integers and Algorithms

3 Discrete Mathematics by Yang-Sae Moon Page 3 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 b k + a k-1 b k-1 + … + 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 2.5 Integers and Algorithms

4 Discrete Mathematics by Yang-Sae Moon Page 4 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 2.5 Integers and Algorithms The “base b expansion of n” (n 의 밑수 b 전개, n 의 b 진법 표현 )

5 Discrete Mathematics by Yang-Sae Moon Page 5 Particular Bases of Interest Base b=10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9. Base b=2 (binary): 2 digits: 0,1. (“Bits”=“binary digits.”) Base b=8 (octal): 8 digits: 0,1,2,3,4,5,6,7. Base b=16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F 2.5 Integers and Algorithms 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

6 Discrete Mathematics by Yang-Sae Moon Page 6 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 이 될 때 까지 반복한다.) 2.5 Integers and Algorithms (177130) 10 = (?) 16 −177130 = 16·11070 + 10 −11070 = 16·691 + 14 −691 = 16·43 + 3 −43 = 16·2 + 11 −2 = 16·0 + 2  (177130) 10 = (2B3EA) 16 (242) 10 = (?) 2 −241 = 2·120 + 1,120 = 2·60 + 0 −60 = 2·30 + 0,30 = 2·15 + 0 −15 = 2·7 + 1,7 = 2·3 + 1 −3 = 2·1 + 1,1 = 2·0 + 1  (242) 10 = (11110001) 2

7 Discrete Mathematics by Yang-Sae Moon Page 7 Converting to Base b (2/2) Formal Algorithm 2.5 Integers and Algorithms 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 }

8 Discrete Mathematics by Yang-Sae Moon Page 8 Addition of Binary Numbers Intuition ( let a = (a n-1 … a 1 a 0 ) 2, b = (b n-1 … b 1 b 0 ) 2 ) 2.5 Integers and Algorithms 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 c n-1 c n-2...c 2 c 1 c i =  (a i-1 +b i-1 )/2  s i = (a i +b 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} 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 } O(n)

9 Discrete Mathematics by Yang-Sae Moon Page 9 2’s Complement (2 의 보수 ) (1/2) In binary, negative numbers can be conveniently represented using 2’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 ~ 2 n −1(int n) The bit in the highest-order bit-position (n−1) represents a coefficient multiplying −2 n−1 ; ( 왼쪽 첫 번째 bit 는 “  −2 n−1 ” 을 나타내며, 흔히 부호 (+ or −) 를 의미한다.) The other positions i < n−1 just represent 2 i, as before. 2.5 Integers and Algorithms

10 Discrete Mathematics by Yang-Sae Moon Page 10 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. Examples The negation of 1011 = −(0100 + 1) = −(0101) = −(5) 10 The negation of 0100 = (1011 + 1) = 10100 = (4) 10 2.5 Integers and Algorithms Bitwise logical complement of a

11 Discrete Mathematics by Yang-Sae Moon Page 11 Subtraction of Binary Numbers 2.5 Integers and Algorithms 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) } Theorem: For an integer a represented in 2’s complement notation, −a = a + 1. (a 가 2 보수로 표현된다면, …) Proof: Just try it by yourself! Algorithm

12 Discrete Mathematics by Yang-Sae Moon Page 12 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 ) 2.5 Integers and Algorithms 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) 00...... 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) +)

13 Discrete Mathematics by Yang-Sae Moon Page 13 Multiplication of Binary Numbers (2/2) 2.5 Integers and Algorithms 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 i = 1 then c j := a shifted j places{c j := a << j} then c j := a 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 )  O(n 1.585 ) in $6.3

14 Discrete Mathematics by Yang-Sae Moon Page 14 Division Algorithm 2.5 Integers and Algorithms Example: 23/4? 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)} Algorithm rq 23 − 4=191 19 − 4=152 15 − 4=113 11 − 4=74 7 − 4=35

15 Discrete Mathematics by Yang-Sae Moon Page 15 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 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) = 4. 2.5 Integers and Algorithms

16 Discrete Mathematics by Yang-Sae Moon Page 16 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 의 공약수이다. 2.5 Integers and Algorithms

17 Discrete Mathematics by Yang-Sae Moon Page 17 Euclid’s Algorithms 2.5 Integers and Algorithms procedure gcd(a, b: positive integer) while b  0 begin r := a mod b{r = a % b} a := b b := r return a int gcd(int a, int b)/* assume a > b */ { if(b==0) return a; else return gcd(b, a%b); } Algorithm in Pseudocode Algorithm in C (using recursive calls)


Download ppt "이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과."

Similar presentations


Ads by Google