Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices...

54
Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大大大大 大大大大大 大大大 (Lingling Huang)

Transcript of Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices...

Page 1: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Discrete Mathematics

Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices

大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Page 2: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-2

3.1 Algorithms

Def 1. An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem.

Example 1. Describe an algorithm for finding the maximum value in a finite sequence of integers.( 假設給定的 sequence 是 a1,a2,…,an)

Page 3: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-3

Solution : ( English language)1. Set the temporary maximum equal to the first

integer in the sequence.2. Compare the next integer in the sequence to the

temporary maximum, and if it is larger than the temporary maximum, set the temporary maximum equal to this integer.

3. Repeat the previous step if there are more integers in the sequence.

4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence.

Page 4: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-4

Solution : (pseudo-code)

Algorithm 1. Finding the Maximum Element

procedure max(a1, a2, …, an : integers)max := a1

for i := 2 to n if max < ai then max := ai

{ max is the largest element}

Page 5: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-5

※ There are several properties that algorithms generally share :

Input Output Definiteness : The steps of an algorithm must be defined

precisely. Correctness : produce correct output values Finiteness : produce the desired output after a finite number of step. Effectiveness Generality : The procedure should be applicable for all problems of the desired form, not just for a particular set of input values.

Page 6: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-6

Problem : Locate an element x in a list of distinct elements a1,a2,…,an, or determine that it is not in the list. 做法 : linear search, binary search.

Algorithm 2. The linear search algorithm

procedure linear_search( x : integer, a1,a2,…,an: distinct integers)i := 1While ( i ≤ n and x≠ai ) i := i + 1if i ≤ n then location := i else location := 0 { location = j if x = aj; location = 0 if x≠ai, ∀i }

※ Searching Algorithms

Page 7: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-7

兩種 search 方式的概念 :

Linear Search : 從 a1 開始,逐一比對 x 是否等於 ai ,若找到則 location = i , 若到 an 比完後還找不到,則 location = 0 。

Binary Search : ( 必須具備 a1 < a2 < … < an 的性質才能用 )(1) 每次將 list 切成兩半, ( ai , … , am ) , (am+1 , … , aj ) 若 x > am 表示 x 應在右半,否則在左半。 (2) 重覆上一步驟至 list 只剩一個元素 ai , 若 x = ai 則 location = i ,否則 location = 0 。

Page 8: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-8

Example 3. Search 19 from

a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 a16

1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

12 13 15 16 18 19 20 22

18 19 20 22

18 19

191. ( 切兩半 ) ( 因 19 > 10 ,取右半 )2. ( 再切二半 ) ( 因 19 > 16 ,取右半 )3. ( 再切二半 ) ( 因 19 19≦ ,取左半 )4. ( 再切二半 ) ( 因 19 > 18 ,取右半 )5 此時只剩一個元素 a14 = 19 因 19 = 19 ,故 location =14

Note : ai, ai+1, …, aj 數列的切法 : 令 m =

2

ji

則 am 即切開紅線左邊那點。

Page 9: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-9

Algorithm 3. The Binary Search Algorithm

procedure binary_search( x : integer, a1,a2,…,an : increasing integers)i :=1 { i is left endpoint of search interval }j := n { j is right endpoint of search interval }while i < j begin m := if x > am then i := m+1 else j := m endif x = ai then location := i else location := 0 { location = i if x = ai , location = 0 if x≠ai , ∀i }

ji 2/)(

Page 10: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-10

Problem : Suppose that we have a list of elements, a sorting is putting these elements into a list in which the elements are in increasing order.

eg. 7, 2, 1, 4, 5, 9 => 1, 2, 4, 5, 7, 9 d, t, c, a, f => a, c, d, f, t

解法有很多,此處僅介紹 : bubble sort ( 氣泡排序法 ) ,及 insertion sort ( 插入排序法 ) 。

Bubble Sort 概念 : 設原 list 為 a1,…,an 。 從 a1,a2 開始,向後兩兩比較,若 ai > ai+1 則交換,當檢查

完 an 時, an 必定是最大數。 再從 a1,a2 開始向後比較,若 ai > ai+1 則交換,此時只需

檢查到 an-1 即可。 依此類推。

※ Sorting Algorithms

Page 11: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-11

Example 4. Use the bubble sort to put 3, 2, 4, 1, 5 into increasing order.

Sol :

32415

First pass (i=1) :

23415

23415

23145

Second pass (i=2) :

Third pass (i=3) : Fourth pass (i=4) :

12345

23145

23145

21345

21345

23145

21345

12345

12345

12345

Page 12: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-12

Algorithm 4 The Bubble Sort procedure bubble_sort (a1,…,an )

for i := 1 to n1

for j := 1 to ni if aj > aj+1 then interchange aj and aj+1

{ a1,a2,…,an is in increasing order }

Page 13: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-13

Insertion Sort 的概念 : 從 j = 2 開始,將 aj 插入已排序好的 a1,…,aj1 間

的位置,使得 a1,…,aj 都由小 → 大排好。 j 逐次遞增,重複上一步驟至做完。

Page 14: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-14

Example 5. Use insertion sort to sort 3, 2, 4, 1, 5 Sol :

(j=2 時, a1=3 可看成已經排序好的數列,此時要插入 a2) :

3 < 2 2, 3 交換 2, 3, 4, 1, 5(j=3 時, a1,a2 已經排序好,此時要插入 a3) : 4 > 2, 4 > 3 4 的位置不變 2, 3, 4, 1, 5 (j=4 時, a1,a2 ,a3 已經排序好,此時要插入 a4) : 1 < 2 將 1 插在最前面 1, 2, 3, 4, 5(j=5 時, a1,a2 ,a3 ,a4 已經排序好,此時要插入 a5) : 5 > 1, 5 > 2, 5 > 3, 5 > 4 5 不變 1, 2, 3, 4, 5

a1 a2 a3 a4 a5

Page 15: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-15

Algorithm 5 The Insertion Sort

procedure insertion_sort ( a1,…,an : real numbers with n ≥ 2 )for j := 2 to n begin i := 1 while aj > ai

i := i + 1 m := aj for k := 0 to j – i – 1

ajk := ajk1

ai := mend{ a1,a2,…,an are sorted }

( Exercise : 3, 9, 13, 23, 35, 39 )

找出 aj 應插入的位置最後 ai1 < aj <= ai

將 ai, ai+1, …, aj1

全部往右移一格

Page 16: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-16

3.2 The Growth of Functions To analyze the practicality of the program, we need

to understand how quickly the function (number of operations used by this algorithm) grows as n (number of input elements) grows.

eg. sort n objects Alg. 1 : n2 次計算 Alg. 2 : 8n 次計算

n 1 2 3 … 8 9 10# ofop.

Alg.1 1 4 9 … 64 81 100Alg.2 8 16 24 … 64 72 80 better!

Page 17: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-17

Def 1. ( Big-O notation )Let f and g be functions from the set of integers to the set of real numbers. We say that f (x) is O(g(x)) if there are constants C and k such that

| f (x) | ≤ C | g(x) |

whenever x > k . ( read as “f (x) is big-oh of g(x)” )

Page 18: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-18

Example 1. Show that f (x) = x2+2x+1 is O(x2)

Sol : Since

x2+2x+1 ≤ x2+2x2+x2 = 4x2

whenever x > 1 , it follows that f (x) is O(x2)

(take C = 4 and k =1 )

另法: If x > 2, we see that

x2+2x+1 ≤ x2+x2+x2 = 3x2

( take C = 3 and k = 2 )

Page 19: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-19

Figure 2. The function f (x) is O(g(x))

Example 1( 補充 ). Show that f (n)= n 2+2n +2 is O(n3)Sol : Since

n2+2n+2 ≤ n3+n3+n3 = 3n3

whenever n > 1, we see that f (n) is O(n3) ( take C = 3 and k = 1 )

k

Cg(x)

f (x)g(x)

f (x) < C g(x) for x > k

Note. The function g is chosen to be as small as possible.

Page 20: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-20

Example 5. How can big-O notation be used to estimate the sum of the first n positive integers?

( i.e., )

Sol :

1 + 2 + 3 + … + n ≤ n + n + … + n = n2

∴ is O(n2), taking C =1 and k =1.

Theorem 1. Let f (x) = anxn+an1xn1+…+a1x+a0

where a0, a1, …, an are real numbers. Then f (x) is O(xn).

n

i

i1

n

i

i1

Page 21: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-21

Example 6. Give big-O estimates for f (n) = n!

Sol :

n! = 12 3 … n ≤ n n … n = nn

∴ n! is O(nn) , taking C =1 and k =1.

Theorem 2,3 Suppose that f1(x) is O(g1(x)) and f2(x) is O(g2(x)), then (f1+f2)(x) is O(max(|g1(x)|, |g2(x)|)), (f1 f2)(x) is O(g1(x) g2(x)).

Example 7. (see Figure 3) 常見 function 的成長速度由小至大排列: 1 < log n < n < n log n < n2 < 2n < n!

Page 22: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-22

Exercise 7,11,19

Exercise 19(c) : f (n) = (n!+2n)(n3+log(n2+1)) (n!+n!)(n3+n3) = 4n3n!

∴ f (n) is O(n3n!) 取 C = 4, k = 3

Page 23: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-23

3.3 Complexity of AlgorithmsQ : How can the efficiency of an algorithm be

analyzed ?

Ans : (1) time (2) memory

Def : Time complexity : an analysis of the time required to solve a

problem of a particular size.

( 評量方式 : 計算 # of operations ,如 “ comparison” 次數,

“加法” 或 “乘法” 次數等 ) Space complexity : an analysis of the computer memory

required to solve a problem of a particular size. ( 通常是資料結構探討的範圍 )

Page 24: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-24

Example 1. Describe the time complexity of Algorithm 1.

Algorithm 1. ( Find Max )procedure max(a1,…,an : integers)max := a1

for i := 2 to n if max < ai then max := ai

{ max is the largest element }

Sol : ( 計算 # of comparisons)

i 值一開始 = 2逐次加一,並比較是否 >n.當 i 變成 n+1 時因比 n 大,故結束 for 迴圈。∴ 共有 n 次 comparison

共有 n1 次 comparison

故整個演算法共做 2n1 次 comparison其 time complexity 為 O(n).

Page 25: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-25

Example 2. Describe the time complexity of the linear search algorithm.

Algorithm 2 ( Linear Search )procedure ls ( x : integer , a1,…,an : distinct integers )i := 1While ( i n and x ≠ai ) i := i +1if i n then location := i else location := 0 location = i x = ai

= 0 x ai i

Sol : ( 計算 # of comparisons ) (Case 1) 當 x = ai for some i n 時 此行只執行 i 次,故此行共 2i 次比較 加上 if ,共計 2i +1 次 comparisons. (Case 2) 當 x ≠ ai for all i 時 此行執行 n 次後 第 n + 1 次時 i = n + 1 > n 即跳出 ∴ 共計 2n+2 次 comparisons 由 (1) 、 (2) 取 worst-case 演算法的 time complexity 為 O(n)

Page 26: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-26

Example 4. Describe the average-case performance of the linear search algorithm, assuming that x is in the list.

Sol : ( 計算 “平均比較次數” )

已知當 x = ai 時,共需 2i + 1 次比較 .( by Example 2 )x = a1,a2, …, 或 an 的機率都是 1/n.∴平均比較次數 ( 即期望值 )= ( x = a1 的比較次數 ) × ( x = a1 的機率 )+ ( x = a2 的比較次數 ) × ( x = a2 的機率 )+ …+ ( x = an 的比較次數 ) × ( x = an 的機率 )= 3 × 1/n + 5 × 1/n + … + ( 2n+1) × 1/n= ( 3+5+…+(2n+1)) / n

= / n = n + 2

2

)42( nn

∴average-case 的 time complexity 為 O(n)

Alg. 2 ( Linear Search )procedure ls ( x,a1,…,an)i := 1While ( i n and x ≠ai ) i := i +1if i n then location := i else location := 0

Page 27: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-27

Example 3. Describe the time complexity of the binary search algorithm.

Sol : 設 n = 2k 以簡化計算( 若 n < 2k ,其比較次數必小於等 於 n = 2k 的情況 )因 while 迴圈每次執行後整個 list 會切成兩半故最多只能切 k 次就會因 i = j 而跳出迴圈∴ 共比較 2k+2 次

time complexity 為 O(k) = O(log n)

Alg. 3 ( Binary Search )procedure bs ( x : integer, a1,…,an : increasing integers )i := 1 { left endpoint }j := n { right endpoint }while i < j /* ( k+1 次 )begin m := ( i + j ) / 2 if x > am then i := m+1 /* ( k 次 ) else j := mendif x = ai then location := i /* ( 1 次 )else location := 0

Page 28: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-28

Example 5. What is the worst-case complexity of the bubble sort in terms of the number of comparisons made ?

procedure bubble_sort ( a1,…,an )for i := 1 to n 1 for j := 1 to n – i if aj > aj+1 then interchange aj and ai+1 { a1,…,an is in increasing order }

Sol : 共 n1 個 pass 第 i 個 pass 需 n – i 次比較 ∴ 共計 (n1)+(n2)+…+1 = 次比較 ∴ O(n2)

Note 1. 不管何種 case 都需做 次比較。

Note 2. For 迴圈所需比較次數通常會省略,因此 Example 5,6 不再考慮。2

)1( nn

2

)1( nn

Page 29: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-29

Example 6. What is the worst-case complexity of the insertion sort in terms of the number of comparisons made ?

procedure insertion_sort ( a1,…,an )for j := 2 to n begin i := 1 while aj > ai i := i +1 m := aj

for k := 0 to j i 1 ajk := ajk1

ai := mend{ a1,…,an are sorted }

Sol : 做最多次比較的情況如下: 在考慮 aj 時 a1 < a2 < … < aj1 < aj

此時共做 j 次比較

故共計 2+3+…+n = 1 次比較

O(n2)( 即 worst case 是 a1 < a2 < … < an)

2

)1( nn

Page 30: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-30

Table 1. Commonly Used Terminology

Complexity Terminology

O(1) constant complexity

O(log n) Logarithmic complexity

O(n) Linear complexity

O(n log n) n log n complexity

O(nb) Polynomial complexity

O(bn) , b >1 Exponential complexity

O(n!) Factorial complexity

Exercise : 7,8,13

Page 31: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-31

3.4 The integers and division※探討一些 Number Theory 的基本觀念Def 1. a,b : integers, a≠0.

a divides b (denote a | b) if cZ , b=ac . (a : a factor of b, b : a multiple of a)

(a b if a does not divide b) Corollary 1. If a,b,c Z and a | b , a | c. then a | mb+nc whenever m,nZDef 2. In the quality a = dq + r with 0 r < d, d is called

the divisor ( 除數 ), a is called the dividend ( 被除數 ), q is called the quotient ( 商數 ), and r is called the remainder ( 餘數 ). q = a div d, r = a mod d

Page 32: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-32

Def 3. If a,bZ, mZ+, then

a is congruent ( 同餘 ) to b modulo m if m | (ab).

(denote a≡b (mod m)).

Thm 4. Let mZ+, a,bZ. a≡b (mod m) iff kZ, s.t. a=b+km.

Thm 5. Let mZ+, a,bZ.

If a≡b (mod m) and c≡d (mod m), then a+c≡b+d (mod m) and ac≡bd (mod m).

Page 33: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-33

3.5 Primes and Greatest Common Divisors

Def 1. pZ+ {1} is called prime ( 質數 )

if a p, 1<a< p, aZ+. p is called composite ( 合成數 ) otherwise.

Thm 1. (The fundamental theorem of arithmetic)

Every positive integer greater than 1 can be written uniquely as a prime or as the product of two or more primes where the prime factors are written in order of nondecreasing size.

Example 2.

The prime factorization ( 因數分解 ) of 100 is 2252.

Page 34: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-34

Thm 3. There are infinitely many primes.Pf. 假設質數只有 n 個: p1, p2, …, 及 pn , 令 Q = p1p2…pn+1, 因 p1, …, pn 都不整除 Q ,得證。

※目前為止所知最大的質數是 2p 1 的形式 , where p is prime. 稱為 Mersenne primes ( 梅森質數).

Example 5. 221=3, 231=7, 251=31 are primes, but 2111=2047=2389 is not a prime.

Def 2. gcd ( greatest common divisor )

Def 3. relatively prime ( 互質 )

Def 5. lcm ( least common multiple )

Thm 2. If n is a composite integer, then n has a prime divisor less than or equal to .n

Page 35: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-35

Exercise 14. How many zeros are there at the end of 100! ?

Sol : 計算 12 3 … 100=10k m, where 10 m

∵ 10=2 5 ,又 2 的次數必定比 5 多 ∴ 計算 1 2 3 … 100=5k n, where 5 n

∵ 5,10,15,20,…,100 才有因數 5, 而 25,50,75,100 有因數 25

∴ k=24 共有 24 個 0

Homework : 試寫一 alg.求出 n 的所有質數

Page 36: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-36

3.6 Integers and Algorithms※The Euclidean Algorithm ( 輾轉相除法求 gcd )

Example : Find gcd(91,287)

Sol:

287 = 91 3 + 14

91 = 14 6 + 7

14 = 7 2

∴ gcd(91,287) = 7

Lemma 1 Let a = bq + r, where a, b, q, and r Z.Then gcd(a, b) = gcd (b, r).

if x|91 & x|287 x|14∴gcd (91,287) = gcd(91,14)

gcd (91,14) = gcd (14,7) gcd (14,7) = 7

Page 37: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-37

Algorithm 6. ( The Euclidean Algorithm)procedure gcd ( a, b : positive integers)x := ay := bwhile y≠0begin r := x mod y ( if y > x then r = x) x := y y := r end { gcd (a, b) = x }

eg. 求 gcd (6,12)

x = 6 y = 12while y≠0 r = 6 mod 12 =6 x = 12 y = 6while y≠0 r = 12 mod 6 = 0 x = 6 y = 0 while y = 0 , end.

∴ gcd (6,12) = 6Exercise : 23,25

Page 38: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-38

3.7 Applications of Number Theory

Theorem 1. If a and b are positive integers, then there exist integers s and t such that gcd(a,b) = sa+tb.

gcd(a,b) can be expressed as a linear combination with integer coefficients of a and b.

※將 gcd(a,b) 寫成 a 跟 b的線性組合: The extended Euclidean Algorithm

Page 39: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-39

Example 1 Express gcd(252, 198) =18 as a linear combination of 252 and 198.

Sol:252 = 1 198 + 54

198 = 3 54 + 36 54 = 1 36 + 18 36 = 2 18 ∴ gcd(252, 198) = 18

18 = 54 – 1 36

36 =198 – 3 54

54 =252 – 1 198

18 = 54 – 1 36 = 54 – 1 (198 – 3 54 )

= 4 54 – 1 198 = 4 (252 – 1 198) – 1 198

= 4 252 – 5 198Exercise : 1(g)

Page 40: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-40

Lemma 1. If a, b and c are positive integers such that gcd(a,b) = 1 and a | bc, then a | c.

Lemma 2. If p is a prime and p | a1a2…an, where each ai is an integer, then p | ai for some i.

Example 2 14 8 (mod 6), 但的左右兩邊同除以 2 後不成立because 14/2=7, 8/2=4, but 7 4(mod 6).

Q: 何時可以讓 的左右同除以一數後還成立呢?

另 , 14 8 (mod 3), 同除以 2 後 , 7 4 (mod 3) 成立

Page 41: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-41

Theorem 2. Let m be a positive integer and let a, b, and c be integers. If ac bc (mod m) and gcd(c, m) = 1, then a b (mod m).

※ Linear Congruences

A congruence ( 同餘式 ) of the form ax b (mod m), wherem is a positive integer, a and b are integers, and x is a variable, is called a linear congruence.

How can we solve the linear congruence ax b (mod m)?Def: If ax 1 (mod m), and let a be an answer of x, a is called an inverse ( 反元素 ) of a modulo m

Page 42: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-42

Theorem 3. If a and m are relatively prime integers and m>1, then an inverse of a modulo m exists. Furthermore, this inverse is unique modulo m.

Proof. (existence) (unique 的部分是 exercise)

By Thm 1, because gcd(a, m) = 1, there exist integers s and t such that sa + tm =1.

sa + tm 1 (mod m).

Because tm 0 (mod m), sa 1 (mod m),

s is an inverse of a modulo m.

Page 43: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-43

Example 3 Find an inverse of 3 modulo 7.

Sol. Because gcd(3, 7) = 1, find s, t such that 3s + 7t =1.

2 is an inverse of 3 modulo 7.

7 = 2 3 + 1

1 = 2 3 + 1 7

(Note that every integer congruent to 2 modulo 7 is also an inverse of 3, such as 5, 9, 12, and so on. )

Exercise : 5

Page 44: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-44

Example 4 What are the solutions of the linear congruence 3x 4 (mod 7)?

Sol. By Example 3 2 is an inverse of 3 modulo 7

If x is a solution, then x 8 6 (mod 7).

2 3x 2 4 (mod 7)

Because 6 1 (mod 7), and 8 6 (mod 7),

We need to determine whether every x with x 6 (mod 7)is a solution. Assume x 6 (mod 7), then 3x 6 = 18 4 (mod 7).

Therefore every such x is a solution: x = 6, 13, 20, …, and 1, 8, 15, …. Exercise : 11

將 3x 4 (mod 7) 左右同乘 2

6x (mod 7)

3 (2) 1 (mod 7)

Page 45: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-45

Example 5. 孫子算經 : 「某物不知其數,三三數之餘二,五五數之餘三,七七數之餘二,問物幾何 ? 」 ( 又稱為「韓信點兵」問題 )

i.e. x ≡ 2 (mod 3) x ≡ 3 (mod 5) x = ? x ≡ 2 (mod 7)

Theorem 4. (The Chinese Remainder Theorem)Let m1,m2,…,mn be pairwise relatively prime positive integers and a1, a2, …, an arbitrary integers. Then the system

x ≡ a1 (mod m1)x ≡ a2 (mod m2)

:x ≡ an (mod mn)

has a unique solution modulo m = m1m2…mn.( 即有一解 x, where 0 x < m , 且所有其他解 mod m 都等於 x)

The Chinese Remainder Theorem ( 中國餘數定理 )

Page 46: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-46

Proof of Thm 4:

Let Mk = m / mk 1 k n

∵ m1, m2,…, mn are pairwise relatively prime

∴ gcd (Mk , mk) = 1

integer yk s.t. Mk yk ≡ 1 (mod mk)

( by Thm. 3)

ak Mk yk ≡ ak (mod mk) , 1 k n

Let x = a1 M1 y1+a2 M2 y2+…+an Mn yn

∵ mi | Mj , i ≠ j

∴ x ≡ ak Mk yk ≡ ak (mod mk) 1 k n

x is a solution.

All other solution y satisfies y ≡ x (mod mk).

x ≡ a1 (mod m1)x ≡ a2 (mod m2):x ≡ an (mod mn)

m = m1m2…mn

Page 47: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-47

Example 6. (Solve the system in Example 5)Let m = m1m2m3 = 357 = 105 M1 = m / m1 = 105 / 3 = 35 ( 也就是 m2m3) M2 = m / m2 = 105 / 5 = 21 M3 = m / m3 = 105 / 7 = 15

35 ≡ 2 (mod 3) 35 2 ≡ 2 2 ≡ 1 (mod 3)

21 ≡ 1 (mod 5) 21 1 ≡ 1 (mod 5)

15 ≡ 1 (mod 7) 15 1 ≡ 1 (mod 7)

∴ x = a1M1y1 + a2M2y2 + a3M3y3

= 2 35 2 + 3 21 1 + 2 15 1 = 233 ≡ 23 (mod 105)∴ 最小的解為 23 ,其餘解都等於 23+105t for some tZ+

M1 y1

M2 y2

M3 y3

x ≡ 2 (mod 3)x ≡ 3 (mod 5) x = ?x ≡ 2 (mod 7)

找 y1 使得 M1y1 = 1 (mod 3)

Page 48: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-48

Exercise 18. Find all solutions to the system of congruences x ≡ 2 (mod 3)

x ≡ 1 (mod 4) x ≡ 3 (mod 5)Sol : a1=2 , a2=1 , a3=3, m1=3 , m2=4 , m3=5 m=345=60 M1=20 , M2=15 , M3=12 20≡2 (mod 3) 202≡1 (mod 3) 15≡3 (mod 4) 153≡1 (mod 4) 12≡2 (mod 5) 123≡1 (mod 5) ∴ x = 2202+1153+3123 = 80+45+108=233≡53 (mod 60)

Page 49: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-49

※ 補充: (when mi is not prime)

Ex 20. Find all solutions, if any, to the system of congruences.

x≡5 (mod 6)x≡3 (mod 10)x≡8 (mod 15)

Sol. Rewrite the system as the following:x ≡ 1 (mod 2) x≡2 (mod 3)

i.e., x≡1 (mod 2)x≡2 (mod 3) …x≡3 (mod 5) Exercise : 做完此題

x ≡ 1 (mod 2)x ≡ 2 (mod 3)

x≡3 (mod 5)x≡3 (mod 5)

Page 50: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-50

※ 補充: (when mi is a prime power)

Ex 21. Find all solutions, if any, to the system of congruences.

x≡7 (mod 9)x≡4 (mod 12)x≡16 (mod 21)

Sol. Rewrite the system as the following:x≡7 (mod 9) ( 不能拆! )

x≡0 (mod 4)

i.e., x≡7 (mod 9) ( 此式取代 x≡1 (mod 3) 式子 )x≡0 (mod 4) …x≡2 (mod 7)

x≡1 (mod 3)x≡1 (mod 3) x≡2 (mod 7)

Page 51: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-51

Computer Arithmetic with Large Integers

Suppose that m1,m2,…,mn be pairwise relatively prime integers greater than or equal to 2 and let m = m1m2 …mn.By the Chinese Remainder Theorem, we can show that an integer a with 0 a < m can be uniquely represented by then-tuple (a mod m1, a mod m2, …, a mod mn).

Example 7 What are the pairs used to represent the nonnegativeintegers x<12 when they are represented by the order pair(x mod 3, x mod 4)?

Sol 0=(0, 0), 1=(1, 1), 2=(2, 2), 3=(0, 3), 4=(1, 0), 5=(2, 1), 6=(0, 2), 7=(1, 3), 8=(2, 0), 9=(0, 1), 10=(1, 2), 11=(2, 3).

Exercise : 37

Page 52: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-52

To perform arithmetic with larger integers, we selectmoduli (modulus 的複數 ) m1,m2,…,mn, whereeach mi is an integer greater than 2, gcd(mi, mj)=1 whenever i j, and m=m1m2…mn is greater than the result of the arithmetic operations we want to carry out.

Page 53: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-53

Example 8 Suppose that performing arithmetic with integers less than 100 on a certain processor is much quicker than doing arithmetic with larger integers. We can restrict almost all our computations to integers less than 100 if we represent integersusing their remainders modulo pairwise relatively prime integers less than 100.

For example, 99, 98, 97, and 95 are pairwise relatively prime.every nonnegative integer less than 99989795 = 89403930can be represented uniquely by its remainders when divided bythese four moduli.

E.g., 123684 = (33, 8, 9, 89), and 413456 = (32, 92, 42, 16)

123684 + 413456 = (33, 8, 9, 89) + (32, 92, 42, 16) = (65 mod 99, 100 mod 98, 51 mod 97, 105 mod 95) = (65, 2, 51, 10)

Use Chinese Remainder Thm to find this sum 537140.

Page 54: Discrete Mathematics Chapter 3 The Fundamentals : Algorithms, the Integers, and Matrices 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)

Ch3-54

Theorem 5 (Fermat’s Little Theorem)If p is prime and a is an integer not divisible by p, then a p1 1 (mod p)Furthermore, for every integer a we have a p a (mod p)

Exercise 27(a) Show that 2340 1 (mod 11) by Fermat’s Little Theorem and noting that 2340 = (210)34.

Proof 11 is prime and 2 is an integer not divisible by 11.

210 1 (mod 11)

2340 1 (mod 11) by Thm 5 of Sec. 3.4 (a b (mod m) and c d (mod m) ac bd (mod m) )

Exercise : Compute 52003 (mod 7)