Upload
harriet-flowers
View
240
Download
1
Embed Size (px)
DESCRIPTION
3 Lemma (A short theorem not used for much except proving another theorem) Let a = bq + r, where a,b,q and r are integers. Then gcd(a,b) = gcd(b,r)
Citation preview
1
Discrete Structures – CNS2300Text
Discrete Mathematics and Its Applications
Kenneth H. Rosen (5th Edition)
Chapter 2The Fundamentals: Algorithms,
the Integers, and Matrices
2
Section 2.5
Integers and
Algorithms
3
Lemma (A short theorem not used for much except proving another theorem)
Let a = bq + r, where a,b,q and r are integers. Then gcd(a,b) = gcd(b,r)
4
Euclidean Algorithmprocedure gcd(a,b: positive integers)x := ay := bwhile y > 0begin r := x mod y x := y y := rend {gcd(a,b) is x}
5
Example
18 = (1)(12) + 6
12 = (2)(6) + 0
gcd(12,18)
6
Example
18 = (1)(12) + 6
12 = (2)(6) + 0
gcd(12,18)
7
Example
gcd(123,277)
277 = (2)(123) + 31
123 = (3)(31) + 30
31 = (1)(30) + 130 = (30)(1) + 0
8
Representation of IntegersLet b be a positive integer greater than 1. Then if n is a positive integer, it can be expressed uniquely in the form
n a b a b a b ak kk k
1 1 0
1 . . .where k is a nonnegative integer, a0,a1,…,ak are nonnegative integers less than b, and
Which means?????
a k 0
9
You can change bases
351 = (101011111)2
351 = ( 537)8
351 = (15F)16
351 = (253)12
10
PeopleThere are only 10 types of people.
Those who understand binary numbers and those who don’t.
11
How?
351 = (29)(12) + 3
29 = (2)(12) + 5
2 = (0)(12) + 2
The remainders tell us the number in base 12
351 = 25312
Convert 351 to base 12
12
Convert 351 to base 16 (hex)
351 = 21(16) + 15 (F in base 16)
21 = 1(16) +5
Therefore
351 = 15F16
1 = 0(16) +1
13
How About the Other Way?
5378 =
5 8 3 8 7 82 1 0
5 64 3 8 7 1
320 + 24 + 7 =
351
14
Special Relationships
101011111
15
Special Relationships
1010111117
16
Special Relationships
10101111173
17
Special Relationships
101011111735
18
Special Relationships
101011111735
1010111112 = 5378
19
Try it Again
101011111
20
This time, group by 4 bits
101011111F
21
Special Relationships
101011111F5
22
Special Relationships
101011111F51
23
Special Relationships
101011111F51
1010111112 = 15F16
24
0000
0001
0010
0011
0101
0100
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
11617256
912819256
56421
25611
12823
2563
3225
25613
12827256
7642925615
12831
256
1817128
96419128
53221
128116423
1283
1625
128136427
12873229
128156431
128
1417649
3219645
16216411322364382564133227647
16296415323164
1217329
161932582132111623323425321316273278293215163132
1
111611831
1611451
1631871
1611291
16518111163141311671815116
2
128124328122528324728
3
138134338132538334738
4
144142344
5
154152354
6
164162364
7
174172374
001 010 011 100 101 110 111E
M
range1 1, )
16 8[ 1 1, )
8 4[ 1 1, )
4 2[ 1 ,1)
2[ 1,2)[ 2,4)[ 4,8)[
25
Addition of Integersprocedure add(a,b: positive integers)c:= 0for j:= 0 to n-1begin d := (aj + bj + c)/2 sj := aj + bj + c - 2d c:= dendsn := c
26
Multiplying Integersprocedure multiply(a,b: positive integers)for j:= 0 to n-1begin if bj = 1 then cj := a shifted j places
else cj := 0endp := 0for j := 0 to n - 1 p := p + cj
27
finished