Upload
prosper-golden
View
224
Download
2
Embed Size (px)
Citation preview
이산수학 이산수학 (Discrete Mathematics)(Discrete Mathematics)
2.5 2.5 정수와 알고리즘정수와 알고리즘(Integers and Algorithms)(Integers and Algorithms)
20062006 년 봄학기년 봄학기
문양세문양세강원대학교 컴퓨터과학과강원대학교 컴퓨터과학과
Page 2Discrete Mathematicsby Yang-Sae Moon
IntroductionIntroduction
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
Page 3Discrete Mathematicsby Yang-Sae Moon
Base-Base-bb Representations of Integers 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 = 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
2.5 Integers and Algorithms
Page 4Discrete Mathematicsby Yang-Sae Moon
Base-b Number SystemsBase-b Number Systems
Ordinarily we write base-10 representations of numbers (usi
ng 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… a1a0 of digits ai<b such that
2.5 Integers and Algorithms
k
i
iiban
0
The “base b expansion of n”
(n 의 밑수 b 전개 , n 의 b 진법 표현 )
Page 5Discrete Mathematicsby Yang-Sae Moon
Particular Bases of InterestParticular 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 inall modern computers
Octal digits correspond to
groups of 3 bits
Hex digits givegroups of 4
bits
Used only because we have 10 fingers
Page 6Discrete Mathematicsby Yang-Sae Moon
Converting to Base Converting to Base b b (1/2)(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
Page 7Discrete Mathematicsby Yang-Sae Moon
Converting to Base Converting to Base b b (2/2)(2/2)
Formal Algorithm
2.5 Integers and Algorithms
procedure base b expansion (n: positive integer)
q := n
k := 0
while q 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}
Page 8Discrete Mathematicsby Yang-Sae Moon
Addition of Binary NumbersAddition of Binary Numbers
Intuition (let a = (an-1… a1a0)2, b = (bn-1… b1b0)2)
2.5 Integers and Algorithms
a = an-1 an-2 . . . a2 a1 a0
b = bn-1 bn-2 . . . b2 b1 b0
a+b = sn sn-1 sn-2 . . . s2 s1 s0
cn-1 cn-2 . . . c2 c1 ci = (ai-1+bi-1)/2
si = (ai+bi)%2
Algorithmprocedure add(an 1− …a0, bn−1…b0: binary expressions of a,b)
c := 0 {c mean a carry}for i := 0 to n 1− {i means a bit index}begin
sum := ai + bi + c {2-bit sum}si := sum mod 2 {low bit of sum}c := sum/2 {high bit of sum}
endsn := c{the binary expression of the sum is (snsn-1… s1s0)2}
O(n)
Page 9Discrete Mathematicsby Yang-Sae Moon
2’s Complement (22’s Complement (2 의 보수의 보수 ) (1/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− ~ (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 c−oefficient multiplying 2− n 1− ;( 왼쪽 첫 번째 bit 는 “ − 2n 1− ” 을 나타내며 , 흔히 부호 (+ or )− 를 의미한다 .)
• The other positions i < n 1 just represent 2− i, as before.
2.5 Integers and Algorithms
Page 10Discrete Mathematicsby Yang-Sae Moon
2’s Complement (22’s Complement (2 의 보수의 보수 ) (2/2)) (2/2)
The negation of any n-bit 2’s complement number
a(= 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
2.5 Integers and Algorithms
Bitwise logical complement of a
Page 11Discrete Mathematicsby Yang-Sae Moon
Subtraction of Binary NumbersSubtraction of Binary Numbers2.5 Integers and Algorithms
procedure subtract (an 1− …a0, bn−1…b0:
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
Page 12Discrete Mathematicsby Yang-Sae Moon
Multiplication of Binary Numbers (1/2)Multiplication of Binary Numbers (1/2)
Intuition (let a = (an-1… a1a0)2, b = (bn-1… b1b0)2)
2.5 Integers and Algorithms
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) 0 0
. . . . . .a·b = cn-1 + cn-2 + ... + c2 + c1 + c0
s(i,j) = (if bj = 1 then ai else 0)
+)
Page 13Discrete Mathematicsby Yang-Sae Moon
Multiplication of Binary Numbers (Multiplication of Binary Numbers (22/2)/2)2.5 Integers and Algorithms
Algorithmprocedure multiply(an 1− …a0, bn−1…b0: binary expressions of a,b)
for j := 0 to n 1− {a bit index for b}begin
if bi = 1 then cj := a shifted j places {cj := a << j}then cj := a
end{c1, c2, …, cn-1 are the partial products.}p := 0for j := 0 to n−1
p := add(p, cj){p is the value of ab}
O(n2) O(n1.585) in $6.3
Page 14Discrete Mathematicsby Yang-Sae Moon
Division AlgorithmDivision Algorithm2.5 Integers and Algorithms
Example: 23/4?
procedure division(a, d: positive integer)q := 0r := |a|while r dbegin
r := r − dq := q + 1
end{q is the quotient(=a/d), r is the remainder(=a%d) }
Algorithm
r q23 − 4 = 19 119 − 4 = 15 215 − 4 = 11 311 − 4 = 7 47 − 4 = 3 5
Page 15Discrete Mathematicsby Yang-Sae Moon
Euclid’s Algorithm for GCDEuclid’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
Page 16Discrete Mathematicsby Yang-Sae Moon
Proof of Proof of Euclid’s AlgorithmEuclid’s Algorithm
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 의 공약수이다 .
2.5 Integers and Algorithms
Page 17Discrete Mathematicsby Yang-Sae Moon
Euclid’s AlgorithmEuclid’s Algorithmss2.5 Integers and Algorithms
procedure gcd(a, b: positive integer)while b 0begin
r := a mod b {r = a % b}a := bb := 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)