Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Module #9 – Number Theory Chapter 3 Algorithms, Integers and Matrices 5/3/2017 1 Module #9 – Number Theory Section 3.4: The Integers and Division • Division • Let a,bZ with a0. • a|b “a divides b” : “cZ: b=ac” So: a is a factor or a divisor of b, and b is a multiple of a. • a doesn’t divide b is denoted by a | b. – Example: 312 True, but 37 False. 5/3/2017 2 Module #9 – Number Theory Facts: the Divides Relation • a,b,c Z: 1. (a|b a|c) a | (b + c) 2. a|b a|bc 3. (a|b b|c) a|c Examples: 1. 312 39 3 (12+9) 321 (21÷3=7) 2. 26 2(6×3) 218 (18÷2= 9) 3. 48 8 64 464 (64÷ 4 = 16 ) 5/3/2017 3 Module #9 – Number Theory Composite and Prime Numbers • A positive integer p>1 is prime if the only positive factors of p are 1 and p ☻ Some primes: 2,3,5,7,11,13... • Non-prime integers greater than 1 are called composite, because they can be composed by multiplying two integers greater than 1. 5/3/2017 4 Module #9 – Number Theory Fundamental Theorem of Arithmetic • Every positive integer greater than 1 has a unique representation as a prime or as the product of two or more primes where the prime factors are written in order of nondecreasing size. (tree or division) 100 = 2·2·5·5= 2252 13 = 13 1024 = 2·2·2·2·2·2·2·2·2·2=210 5/3/2017 5 Module #9 – Number Theory Theorem • If n is composite integer, then n has prime divisor ≤ n • Ex: 49 prime numbers less than 49 are 2,3,5,7 16 prime numbers less than 16 are 2,3 • An integer n is prime if it is not divisible by any prime ≤ n • Ex: 13 where 13= 3.6 so the prime numbers are 2,3 but non of them divides 13 so 13 is prime 5/3/2017 6 Module #9 – Number Theory • To find the prime factor of an integer n: 1- find n 2- list all primes ≤ n 2, 3, 5, 7,…root of n 3- find all prime factors that divides n. 5/3/2017 7 Module #9 – Number Theory 5/3/2017 8 Module #9 – Number Theory The Division “Algorithm” • let a be an integer and d a positive integer, then there are unique integers q and r such that: a=d×q+r ,0r<d • d is called divisor, and a is called dividend • q is the quotient, and r is the remainder (positive integer) q = a div d , r =a mod d 5/3/2017 9 Module #9 – Number Theory Examples • What are the quotient and remainder when 101 is divided by 11? 101 = 11 × 9 + 2 , q = 9, r = 2 • What are the quotient and the remainder when -11 is divided by 3? • -11 = 3 × (-4) + 1 ,q = -4, r = 1 • Note: -11 3 × (-3) - 2 because r 0 (can't be negative) 5/3/2017 10 Module #9 – Number Theory Modular Congruence Let a, b Z, mZ+. Thm: a is congruent to b modulo m written “a b (mod m)”, iff 1) a mod m = b mod m 2) m | ab 5/3/2017 i.e. (ab) mod m = 0. 11 Module #9 – Number Theory Examples Is 17 congruent to 5 modulo 6 ? 17 mod 6 = 5 and 5 mod 6 = 5 and 6 | (17-5) 6 | 12 where 12÷ 6 = 2 Then 17 ≡ 5 (mod 6) Is 24 congruent to 14 modulo 6? Since, 24 mod 6 = 0 and 14 mod 6 = 2 Then, 24 ≡ 14 (mod 6) and 6 | (24-14) 6 | 10 5/3/2017 12 Module #9 – Number Theory Useful Congruence Theorems • Let a,b,c,dZ, mZ+. Then if ab (mod m) and cd (mod m), then: ▪ a+c b+d (mod m), and ▪ ac bd (mod m) 5/3/2017 13 Module #9 – Number Theory Ex. : let 7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5) Then: (7 + 11) ≡ (2 + 1) (mod 5) 18 ≡ 3 (mod 5) and ( 7 × 11) ≡ (2 × 1) (mod 5) 77 ≡ 2 (mod 5) 5/3/2017 14 Module #9 – Number Theory Greatest Common Divisor (GCD) • The greatest common divisor gcd(a,b) of integers a,b (not both 0) is the largest (most positive) integer d that is a divisor both of a and of b. 5/3/2017 15 Module #9 – Number Theory Ways to find GCD 1.find all positive common divisors of both a and b, then take the largest divisor Ex: find gcd (24, 36)? Divisors of 24: 1, 2, 3, 4, 6, 8, 12, 24 Divisors of 36: 1, 2, 3, 4, 6, 9, 12, 18,36 Common divisors: 1, 2, 3, 4, 6, 12 MAXIMUM = 12 gcd (24, 36) = 12 5/3/2017 16 Module #9 – Number Theory 2. use prime factorization: Take the min power of common factors Example Find gcd(24, 36) 24 = 2×2×2×3 = 23×3 36 = 2×2×3×3 = 22×32 gcd(24,36) = 22×3=12 5/3/2017 17 Module #9 – Number Theory Ex: find gcd (120, 500)? 120 = 23 × 3 × 5 500 = 22 × 53 gcd (120, 500) = 22 × 5 = 20 Ex: find gcd (17, 22)? No common divisors so gcd (17, 22) = 1 so, the numbers 17 and 22 are called relatively prime. 5/3/2017 18 Module #9 – Number Theory Least Common Multiple • lcm(a,b) of positive integers a, b, is the smallest positive integer that is a multiple both of a and of b. 5/3/2017 19 Module #9 – Number Theory Find lcm(6,10) ? Take the max power of all Factors 6 = 2×3 10 = 2×5 Lcm(6,10) = 2×3×5=30 Find Lcm (24, 36)? 24 = 23 × 31 36 = 22 × 32 Lcm (24, 36) = 23 × 32 = 72 5/3/2017 20 Module #9 – Number Theory Section 3.1: Algorithms • A finite set of instructions for performing a computation or for solving a problem Examples: - Finding the largest integer in a finite sequence of integers - Locating an element in a finite set - Sorting elements in a finite sequence 5/3/2017 21 Module #9 – Number Theory 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} 5/3/2017 22 Module #9 – Number Theory Linear Search Algorithm procedure linearsearch (x:integer, a1,a2,..,an:integers) i:=1 While(i≤n and x ≠ ai) i = i+1 If i ≤ n then location:= i else location:= 0 {location is the subscript of the term that equals x or the location is 0 if x is not found } 5/3/2017 23 Module #9 – Number Theory Bubble Sort Algorithm procedure bubblesort(a1,a2,…..,an) for i:=1 to n-1 for j:=1 to n-i if aj > aj+1 then interchange aj and aj+1 {a1,…..,an is in increasing order} 5/3/2017 24 Module #9 – Number Theory 3.6 Integers and Algorithms Introduction: The term algorithm originally referred to procedures for performing arithmetic operations using the decimal representations of integers. These algorithms, adapted for use with binary representations, are the basis for computer arithmetic. 5/3/2017 25 Module #9 – Number Theory There are many important algorithms involving integers besides those used in arithmetic, We will describe an algorithm for finding the base b expansion of a positive integer for any base b. 26 Module #9 – Number Theory Representations of Integers In everyday life we use decimal notation to express integers. For example, 965 is used to Denote 9·102 + 6·10 + 5 . However, it is often convenient to use bases other than 10. 27 Module #9 – Number Theory Computers usually use binary notation (with 2 as the base) when carrying out arithmetic, and octal (base 8) or hexadecimal (base 16) notation when expressing characters, such as letters or digits. In fact, we can use any positive integer greater than 1 as the base when expressing integers. 5/3/2017 28 Module #9 – Number Theory BINARY EXPANSIONS What is the decimal expansion of the integer that has (l 0101 1111)2 as its binary expansion? Solution: We have (l 0101 111l)2 = 1·28 + 0·27 + 1·26 + 0·25 + 1·24 + 1 ·2 3 + 1 · 2 2 + 1 ·2 1 + 1 ·2 0 = 351 . 5/3/2017 29 Module #9 – Number Theory HEXADECIMAL EXPANSIONS Sixteen is another base used in computer science. The base 16 expansion of an integer is called its hexadecimal expansion. hexadecimal digits used are 0, 1 , 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where the letters A through F represent the digits corresponding to the numbers 10 through 15 (in decimal notation). 5/3/2017 30 Module #9 – Number Theory EXAMPLE 2 What is the decimal expansion of the hexadecimal expansion of (2AE0B)16 ? Solution: We have (2AE0B)16 = 2.164 +10.163 +14.162 +0·16 +11 = ( 175627)10 . 5/3/2017 31 Module #9 – Number Theory BASE CONVERSION EXAMPLE 1: Find the binary expansion of (241)10 Solution: First divide 241 by 2 to obtain 241 = 2 . 120 + 1 . Successively dividing quotients by 2 gives 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 . Therefore, (241)10 = (1111 0001)2 5/3/2017 32 Module #9 – Number Theory EXAMPLE 2: Find the base 8 expansion of (12345)10 Solution: First, divide l2345 by 8 to obtain 12345 = 8 . 1543 + 1 . Successively dividing quotients by 8 gives: 1543 = 8 . 192 + 7, 192 = 8 · 24 + 0, 24 = 8 · 3 + 0, 3 = 8 · 0 + 3. Therefore, (12345)10 = (3 0071 )8 . 5/3/2017 33 Module #9 – Number Theory EXAMPLE 4 Find the hexadecimal expansion of (177130)10 Solution: 177130 = 16 · 11070 + 10 11070 = 16 · 691 + 14 , 691 = 16 · 43 + 3 , 43 = 16 · 2 + 11 , 2 = 16 · 0 + 2. Therefore, (177 130) 10 = (2B3EA) 16 5/3/2017 34 Module #9 – Number Theory Section 3.8: Matrices A matrix is a rectangular array of objects (usually numbers). 2 3 An m n matrix has exactly 5 1 m rows, and n columns. 7 An n n matrix is called a square matrix, whose order is n. 5/3/2017 0 2 1 3 1 32 2 2 35 Module #9 – Number Theory Matrix Equality • Two matrices A and B are equal iff they have the same number of rows, the same number of columns, and all corresponding elements are equal. 3 2 3 2 0 1 6 1 6 0 5/3/2017 36 Module #9 – Number Theory Row and Column Order • The rows in a matrix are usually indexed 1 to m from top to bottom. The columns are usually indexed 1 to n from left to right. Elements are indexed by row, then column. a1,1 a1, 2 a a 2 , 1 2, 2 A [ai , j ] am,1 am, 2 5/3/2017 a1,n a2 , n am , n 37 Module #9 – Number Theory Matrix Sums • The sum A+B of two matrices A, B (which must have the same number of rows, and the same number of columns) is the matrix given by adding corresponding elements. • A+B = [ai,j+bi,j] 9 2 6 9 3 11 0 8 11 3 11 5 1 2 2 0 3 2 5/3/2017 38 Module #9 – Number Theory Matrix Products For an mk matrix A and a kn matrix B, the product AB is the mn matrix: k AB C [ci , j ] ai ,b , j 1 I.e., element (i, j ) of AB is given by the vector dot product of the i th row of A and the j th column of B (considered as vectors). Note: Matrix multiplication is not commutative! 5/3/2017 39 Module #9 – Number Theory Matrix Product Example • An example matrix multiplication to practice in class: 0 1 1 0 0 1 1 1 0 5 1 2 0 3 2 0 2 0 3 2 11 3 1 0 3 1 2×3 3×4 2×4 5/3/2017 40 Module #9 – Number Theory Identity Matrices • The identity matrix of order n, In, is the order-n matrix with 1’s along the upper-left to lower-right diagonal and 0’s everywhere else. A In = A 1 1 if i j 0 I n 0 if i j 0 5/3/2017 0 1 0 0 0 1 41 Module #9 – Number Theory Powers of Matrices If A is an nn square matrix and p0, then: • Ap AAA···A (A0 In) p times • Example: 3 2 1 2 1 0 1 2 1 1 2 1 2 1 0 1 0 1 0 1 3 2 0 2 1 3 4 3 2 5/3/2017 42 Module #9 – Number Theory Matrix Transposition • If A is an mn matrix, the transpose of A is the nm matrix given by At 3 2 1 0 1 2 5/3/2017 t 2 0 1 1 3 2 43 Module #9 – Number Theory Symmetric Matrices • A square matrix A is symmetric iff A=At. • Which is symmetric? A B C 1 1 2 1 3 3 0 1 1 1 1 0 1 0 2 1 1 1 3 1 2 1 1 2 5/3/2017 44 Module #9 – Number Theory Zero-One Matrices All elements of a zero-one matrix are 0 or 1 – Representing False & True respectively. • The join of A, B (both mn zero-one matrices): A B : [aij bij] • The meet of A, B: A B : [aij bij] 5/3/2017 45 Module #9 – Number Theory Example A = 1 0 1 0 1 0 B= 0 1 0 1 1 0 1 1 1 We find the join between A B = 1 1 0 We find the meet between A B = 0 0 0 0 1 0 5/3/2017 46 Module #9 – Number Theory Boolean Products • Let A be an m k zero-one matrix, Let B be a k n zero-one matrix, • The Boolean product of A and B is like normal matrix product, But using instead + And using instead A⊙B 5/3/2017 47 Module #9 – Number Theory Boolean Powers • For a square zero-one matrix A, and any k0, the kth Boolean power of A is simply the Boolean product of k copies of A. • A[k] A⊙A⊙…⊙A k times 5/3/2017 48 Module #9 – Number Theory Example • A = 1 0 1 0 1 0 B= 1 1 0 0 1 1 A⊙B= (1 1) (0 0) (1 1) (0 1) (1 0) (0 1) (0 1) (1 0) (0 1) (1 1) (0 0) (1 1) (1 1) (0 0) (1 1) (0 1) (1 0) (0 1) 5/3/2017 49 Module #9 – Number Theory • Then 1 1 0 0 1 1 1 1 0 5/3/2017 50