Module 9 Number Theory Chapter 3 Algorithms Integers
Module #9 – Number Theory Chapter 3 Algorithms, Integers and Matrices 9/15/2020 1
Module #9 – Number Theory Section 3. 4: The Integers and Division • • • Division Let a, b Z with a 0. a|b “a divides b” : “ c Z: 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: 3 12 True, but 3 7 False. 9/15/2020 2
Module #9 – Number Theory Facts: the Divides Relation • a, b, c Z: 1. (a|b a|c) a | (b + c) 2. a|bc 3. (a|b b|c) a|c Examples: 1. 3 12 3 9 3 (12+9) =3 21=7 2. 2 6 2 (6× 3) = 2 18 = 9 3. 4 8 8 64 4 64 = 16 9/15/2020 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. 9/15/2020 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=210 9/15/2020 5
Module #9 – Number Theory Theorem • If n is composite integer, then n has prime divisor ≤ • Ex: 49 prime numbers less than are 2, 3, 5, 7 16 prime numbers less than are 2, 3 • An integer n is prime if it is not divisible by any prime ≤ • Ex: 13 where = 3. 6 so the prime numbers are 2, 3 but non of them divides 13 so 13 is prime 9/15/2020 6
Module #9 – Number Theory • To find the prime factor of an integer n: 1 - find 2 - list all primes ≤ 2, 3, 5, 7, …root of n 3 - find all prime factors that divides n. 9/15/2020 7
Module #9 – Number Theory 9/15/2020 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 , 0 r<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 9/15/2020 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) 9/15/2020 10
Module #9 – Number Theory Modular Congruence Let a, b Z, m Z+. Thm: a is congruent to b modulo m written “a b (mod m)”, iff 1) a mod m= b mod m 2) m | a b i. e. (a b) mod m = 0. 9/15/2020 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 9/15/2020 12
Module #9 – Number Theory Useful Congruence Theorems • Let a, b, c, d Z, m Z+. Then if a b (mod m) and c d (mod m), then: ▪ a+c b+d (mod m), and ▪ ac bd (mod m) 9/15/2020 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) 9/15/2020 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. 9/15/2020 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, 8, 12, 18, 36 Common divisors: 1, 2, 3, 4, 6, 8, 12 MAXIMUM = 12 gcd (24, 36) = 12 9/15/2020 16
Module #9 – Number Theory 2. use prime factorization: Take the min Ex: Find gcd(24, 36) 24 = 2× 2× 2× 3 = 23× 3 36 = 2× 2× 3× 3 = 22× 32 gcd(24, 36) = 22× 3=12 9/15/2020 17
Module #9 – Number Theory Ex: find gcd (120, 500)? 120 = 23 × 31 × 5 36 = 22 × 53 × 30 gcd (120, 500) = 22 × 5 × 30 = 20 Ex: find gcd (17, 22)? No common divisors so gcd (17, 22)=1 so the numbers 17 and 22 are called relatively prime. 9/15/2020 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. 9/15/2020 19
Module #9 – Number Theory Find lcm(6, 10) ? Take the max 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 9/15/2020 20
Module #9 – Number Theory Section 3. 8: Matrices • A matrix is a rectangular array of objects (usually numbers). • An m n matrix has exactly m rows, and n columns. • An n n matrix is called a square matrix, whose order is n. 3 2 2 2 9/15/2020 21
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. 9/15/2020 22
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. 9/15/2020 23
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/15/2020 24
Module #9 – Number Theory Matrix Products • For an m k matrix A and a k n matrix B, the product AB is the m n matrix: • I. e. , element (i, j) of AB is given by the vector dot product of the ith row of A and the jth column of B (considered as vectors). • Note: Matrix multiplication is not commutative! 9/15/2020 25
Module #9 – Number Theory Matrix Product Example • An example matrix multiplication to practice in class: 2× 3 9/15/2020 3× 4 26
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 9/15/2020 27
Module #9 – Number Theory Powers of Matrices If A is an n n square matrix and p 0, then: • Ap AAA···A (A 0 In) p times • Example: 9/15/2020 28
Module #9 – Number Theory Matrix Transposition • If A is an m n matrix, the transpose of A is the n m matrix given by At 9/15/2020 29
Module #9 – Number Theory Symmetric Matrices • A square matrix A is symmetric iff A=At. • Which is symmetric? 9/15/2020 30
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 m n zero-one matrices): – A B : [aij bij] • The meet of A, B: – A B : [aij bij] 9/15/2020 31
Module #9 – Number Theory Example A= B= We find the join between A B = We find the meet between A B = 9/15/2020 32
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 , But using instead + • And using instead A⊙B 9/15/2020 33
Module #9 – Number Theory Boolean Powers • For a square zero-one matrix A, and any k 0, the kth Boolean power of A is simply the Boolean product of k copies of A. • A[k] A⊙A⊙…⊙A k times 9/15/2020 34
Module #9 – Number Theory Example • A= B= A⊙B= 9/15/2020 35
Module #9 – Number Theory • Then 9/15/2020 36
Module #9 – Number Theory 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 9/15/2020 37
Module #9 – Number Theory Finding the maximum element procedure max(a 1, a 2, ……. . , an: integers) max: =a 1 for i: =2 to n if max<ai then max: =ai {max is the largest element} 9/15/2020 38
Module #9 – Number Theory Linear Search Algorithm procedure linearsearch (x: integer, a 1, a 2, . . , 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 } 9/15/2020 39
Module #9 – Number Theory Bubble Sort Algorithm procedure bubblesort(a 1, a 2, …. . , an) for i: =1 to n-1 for j: =1 to n-i if aj > aj+1 then interchange aj and aj+1 {a 1, …. . , an is in increasing order} 9/15/2020 40
- Slides: 40