Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) PowerPoint Presentation
Download Presentation
이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms)

이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms)

227 Vues Download Presentation
Télécharger la présentation

이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

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

  2. Introduction 2.5 Integers and Algorithms • 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.

  3. Base-b Representations of Integers 2.5 Integers and Algorithms • If b is a positive integer greater than 1,then a given positive integer n can be uniquely represented as follows: n = akbk + ak-1bk-1 + … + a1b1 + a0b0 where • k is a natural number. • and a0, a1, …, and ak are a natural number less than b. • ak  0. • Example: • 165 = 1·102 + 6·101 + 5·100 = (165)10 • 165 = 2·82 + 4·81 + 5·80 = (245)8

  4. Base-b Number Systems 2.5 Integers and Algorithms 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 akak-1… a1a0of digitsai<b such that The “base b expansion of n” (n의 밑수 b전개, n의 b진법 표현)

  5. Particular Bases of Interest 2.5 Integers and Algorithms Used only because we have 10 fingers 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 Used internally in all modern computers Octal digits correspond to groups of 3 bits Hex digits give groups of 4 bits

  6. Converting to Base b (1/2) 2.5 Integers and Algorithms • 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 • 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. Converting to Base b (2/2) 2.5 Integers and Algorithms Formal Algorithm procedurebase b expansion (n: positive integer) q := n k := 0 whileq 0 begin ak := q mod b {remainder} q := q/b {quotient} k := k + 1 end {the base b expansion of n is (akak-1… a1a0)b}

  8. Addition of Binary Numbers 2.5 Integers and Algorithms Intuition (let a = (an-1… a1a0)2, b = (bn-1… b1b0)2) ci = (ai-1+bi-1)/2 cn-1 cn-2 . . . c2 c1 a = an-1 an-2 . . . a2 a1 a0 b = bn-1 bn-2 . . . b2 b1 b0 si = (ai+bi)%2 a+b = sn sn-1 sn-2 . . . s2 s1 s0 Algorithm procedureadd(an−1…a0, bn−1…b0: binary expressions of a,b) c := 0 {c mean a carry} fori := 0 to n−1 {i means a bit index} begin sum := ai+ bi + c {2-bit sum} si := summod 2 {low bit of sum} c := sum/2 {high bit of sum} end sn := c {the binary expression of the sum is (snsn-1… s1s0)2} O(n)

  9. 2’s Complement (2의 보수) (1/2) 2.5 Integers and Algorithms • 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 −2n−1 ~ (2n−1−1). • 0이상의 정수만 표현한다면… 0 ~ 2n−1 (unsigned int n) • 음의 정수까지 표현한다면… −2n ~ 2n−1 (int n) • The bit in the highest-order bit-position (n−1) represents a coefficient multiplying −2n−1;(왼쪽 첫 번째 bit는 “ −2n−1”을 나타내며, 흔히 부호(+ or −)를 의미한다.) • The other positions i < n−1 just represent 2i, as before.

  10. 2’s Complement (2의 보수) (2/2) 2.5 Integers and Algorithms • The negation of any n-bit 2’s complement numbera(= an−1…a0) is given by an−1…a0 + 1. • Examples • The negation of 1011 = −(0100 + 1) = −(0101) = −(5)10 • The negation of 0100 = (1011 + 1) = 10100 = (4)10 Bitwise logical complement of a

  11. Subtraction of Binary Numbers 2.5 Integers and Algorithms Theorem: For an integer a represented in 2’s complement notation, −a = a + 1. (a가 2보수로 표현된다면, …) Proof: Just try it by yourself! Algorithm proceduresubtract (an−1…a0, bn−1…b0: binary 2’s complement expressions of a,b) returnadd(a, add(b, 1)){ a + (−b) }

  12. Multiplication of Binary Numbers (1/2) 2.5 Integers and Algorithms Intuition (let a = (an-1… a1a0)2, b = (bn-1… b1b0)2) a = an-1 an-2 . . . a2 a1 a0 b = bn-1 bn-2 . . . b2 b1 b0 c0= s(n-1,0)s(n-2,0). . . s(2,0)s(1,0)s(0,0) c1= s(n-1,1)s(n-2,1). . . s(2,1)s(1,1)s(0,1) 0 c2= s(n-1,2)s(n-2,2). . . s(2,2)s(1,2)s(0,2) 00 . . . . . . a·b = cn-1 + cn-2 + ... + c2 + c1 + c0 +) s(i,j) = (ifbj = 1 then ai else 0)

  13. Multiplication of Binary Numbers (2/2) 2.5 Integers and Algorithms Algorithm proceduremultiply(an−1…a0, bn−1…b0: binary expressions of a,b) forj := 0 to n−1{a bit index for b} begin ifbi= 1 thencj:= a shifted j places {cj:= a << j} thencj:= a end {c1, c2, …,cn-1 are the partial products.} p := 0 forj := 0 ton−1 p := add(p, cj) {p is the value of ab} O(n2)  O(n1.585) in $6.3

  14. Division Algorithm 2.5 Integers and Algorithms Example: 23/4? r q 23 − 4 = 19 1 19 − 4 = 15 2 15 − 4 = 11 3 11 − 4 = 7 4 7 − 4 = 35 Algorithm proceduredivision(a, d: positive integer) q := 0 r := |a| whilerd begin r := r−d q := q + 1 end {q is the quotient(=a/d), r is the remainder(=a%d)}

  15. Euclid’s Algorithm for GCD 2.5 Integers and Algorithms • 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.

  16. Proof of Euclid’s Algorithm 2.5 Integers and Algorithms Prove gcd(a,b) = gcd(b,r) where r = a - bq • a와 b의 공약수는 b와 r의 공약수가 같음을 보이면, 양쪽 공약수의 쌍이 같으므로 (최대공약수가 같아져) 증명이 이루어진다. • d를 a와 b의 공약수라 하자. • 그러면, a = dqa, b = dqb가 성립한다. • 정의에 의해, r = dqa – dqbq = d(qa – qbq)이 성립하므로, d는 r의 약수이다. • 따라서, d는 b와 r의 공약수이다. • d를 b와 r의 공약수라 하자. • 그러면, b = dqb, r = dqr이 성립한다. • 정의에 의해, a = dqr + dqbq = d(qr + qbq)이 성립하므로, d는 a의 약수이다. • 따라서, d는 a와 b의 공약수이다.

  17. Euclid’s Algorithms 2.5 Integers and Algorithms Algorithm in Pseudocode proceduregcd(a, b: positive integer) whileb 0 begin r := amodb {r = a % b} a := b b := r 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); }