Top Banner
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
27

1 Discrete Structures CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

Jan 19, 2018

Download

Documents

Harriet Flowers

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)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

1

Discrete Structures – CNS2300Text

Discrete Mathematics and Its Applications

Kenneth H. Rosen (5th Edition)

Chapter 2The Fundamentals: Algorithms,

the Integers, and Matrices

Page 2: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

2

Section 2.5

Integers and

Algorithms

Page 3: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: 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)

Page 4: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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}

Page 5: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

5

Example

18 = (1)(12) + 6

12 = (2)(6) + 0

gcd(12,18)

Page 6: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

6

Example

18 = (1)(12) + 6

12 = (2)(6) + 0

gcd(12,18)

Page 7: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

7

Example

gcd(123,277)

277 = (2)(123) + 31

123 = (3)(31) + 30

31 = (1)(30) + 130 = (30)(1) + 0

Page 8: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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

Page 9: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

9

You can change bases

351 = (101011111)2

351 = ( 537)8

351 = (15F)16

351 = (253)12

Page 10: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

10

PeopleThere are only 10 types of people.

Those who understand binary numbers and those who don’t.

Page 11: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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

Page 12: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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

Page 13: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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

Page 14: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

14

Special Relationships

101011111

Page 15: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

15

Special Relationships

1010111117

Page 16: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

16

Special Relationships

10101111173

Page 17: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

17

Special Relationships

101011111735

Page 18: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

18

Special Relationships

101011111735

1010111112 = 5378

Page 19: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

19

Try it Again

101011111

Page 20: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

20

This time, group by 4 bits

101011111F

Page 21: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

21

Special Relationships

101011111F5

Page 22: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

22

Special Relationships

101011111F51

Page 23: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

23

Special Relationships

101011111F51

1010111112 = 15F16

Page 24: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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)[

Page 25: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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

Page 26: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

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

Page 27: 1 Discrete Structures  CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,

27

finished