Sunteți pe pagina 1din 28

Chapter 3

The Fundamentals: Algorithms, the integers, and matrices


Section3.1: Algorithms
Definition1: an algorithm is a finite set of precise instructions for performing a
computation or for solving a problem.
Example1: page168: describe an algorithm for finding the maximum value in a finite
sequence of integers.
Answer: we can specify a procedure for solving this problem in several ways. One
method is simply to use the English language to describe the sequence of steps used.
1- Set the temporary maximum equal to first integer in the sequence
2- Compare the next integer in the sequence to the temporary maximum, if it is larger
than the temporary max., 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.
Remark: 1- An algorithm can also be described using a computer language.
2- Pseudo code: provides an intermediate step between an English language
description of an algorithm and an implementation of this algorithm in a programming
language.
3- The pseudo code used in this book is loosely used on programming language Pascal.
4- A pseudo code description of the algorithm for finding the maximum element in a finite
sequence follows:
Read searching Algorithms-sorting (The bubble sort- The intersection sort- Greedy
Algorithms).

_ a element est l the is
a then a if
n to i for
a
egers a a a procedure
i i
n
arg max
: max max
2 :
: max
int : ,........ , max
1
2 1
!
!
!
Section 3.4: The integers and division
Number theory: the part of mathematics involving integers and their properties belongs
to the branch of mathematics called number theory.
Definition: if a and b are integers with a0 we say that a divides b if there is an integer c
such that b = ac. (Note that a is a factor of b)
Remark: 1- We denote a divides b by ab
2- We denote a doesnt divide b by a b
Example1: determine whether 37 and whether 312
answer: 3 does not divide 7 and 312 since 12 = 3.4
Example2: let n and d be positive integers. How many positive integers not exceeding n
are divisible by d?
Answer: The positive integers divisible by d is of the form kd. So kd is not exceeding n.
So . Therefore there are n/d positive integers not exceeding
n that are divisible by d.
k
n
d n dk 0 0

Theorem 1:
Let a,b and c be integers. Then
i) If ab and ac, then a(b+c)
ii) If ab then abc for all integers c;
iii) If ab and bc, then ac
Proof:

c a so
a Lk c
ka L c lb c then c b If
ka b then b a If iii
cb a
c eger ka c cb
ka b then b a If ii
c b a a k k a k a k c b now
a k c then c a y
a k b then b a If i
/
) (
) ( /
/ )
/
int ) (
/ )
/
/
/ )
2 1 2 1
2
1
=
= =
=

V =
=
+ + = + = +
=
=
Corollary: If a,b, and c are integers such that ab and a/c, then a mb+nc whenever m
and n are integers.
Proof: Since ab by theorem(1) ii amb
Since ac by theorem (1) ii anc
So by theorem (1) I amb+nc
Theorem 2: The Division Algorithm
Let a, b, and c are integers and d a positive integer. Then there are unique q and r, with
o r < d, such that a = dq + r.
Example: let a = 17
d = 3 then 17 = 3.5 + 2
Definition: In the theorem 2, 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.
Example3: what are q and r when 101 is divided by 11?
Solution: 101 = 9(11)+2
q = 9 , r =2
So 9 = 101 div 11 2 = 101 mod 11
Example4: what are the quotient and remainder when -11 is divided by 3?
Answer: -11 = 3(-4) + 1
so -4= -11 div 3
1 = -11 mod 3
Note that r cant be negative. 0 r < 3
Remark: the integer a is divisible by the integer d if and only if the remainder is zero.
Modular Arithmetic:
Definition3: If a and b are integers and m is a positive integer, then a is congruent to b
modulo m if m divides a-b. We use notation a b (mod m) to indicate that a
congruent to b modulo m.
Theorem3: let a and b be integers, and let m be a positive integer. Then a b (mod m)
if and only if a mod m = b mod m.
Ex5: 1- determine whether 17 is congruent to 5 modulo 6
2- Determine whether 24 is congruent to 14 modulo 6
Solution: 17 5 = 12, 612 so 17 5 (mod 6)
24 14 = 10, 610 so 24 is not congruent to 14 (mod 6)
|
|
|
Theorem4: let m be a positive integer. The integers a and b are congruent modulo m iff
there is an integer k such that a = b + km
Proof: suppose a b mod (m)
m / a-b a-b=km
a=b+km
Conversely: suppose a = b + km k is an integer
a b = km
m / a b ( definition of divisibility)
a b ( mod m)
Theorem5: let m be a positive integer. If a b (mod m) and c d (mod m), then
a + c b + d (mod m) and ac bd (mod m).
Proof: since a b (mod m) m / a b a = b + m
Since c d (mod m) m / c- d c = d + m
a + c = (b + d) + m (a + c) (b + d) =
m / (a + c) (b + d) a + c b + d mod (m)
a . c =
|
|
| |
| |
|
1
k
|
2
k

2 1
k k
2 1
k k
|

) (
) (
2 1 1 2
2
2 1 1 2 2 1
m k k d k bk m bd
m k k md k m bk bd m k d m k b
!
!
Example6: 1- Show that 18 3 (mod 5), by using theorem 5:
Solution: 18 = 7 + 11, 3 = 2 + 1 7 2 mod (5)
11 1 mod (5)
18 = 7 + 11 2 + 1 = 3 (mod 5)
so 18 3 (mod 5)
2- Show that 77 2 (mod 5) by using theorem 5
77 = (7)(11) (2)(1) (mod 5)
77 2 (mod 5)
Corollary 2: let m be a positive integer and let a and b be integers. Then
(a + b) mod m = ((a mod m) + (b mod m)) mod m
And ab (a mod m) (b mod m) (mod m)
Application of Congruences
Read example7. Hashing Functions
Read example8
Remark on example8:
1- Pseudorandom Numbers: numbers generated by systematic methods are not truly
random, they are called Pseudorandom Numbers.
|
|
|
|
|
|
|
|
|
2- we generate a sequence of pseudorandom numbers with for all n
by using the congruence
Cryptology: is the study of secret messages.
Caesar encryption: is the process of making a message secret by shifting each letter
forward. Eg. B sends to E, X sends to F
Caesars encryption method can be represented by the function f that assigns to the
nonnegative integer p, p 25, the integer f(p) is the set {0,1,,25} with
f(p) = (p+3) mod 26 note that in this process we replace each letter by an integer from 0
to 25, based on its position for example replace A by 0, K by 10 and so on.
Example9: what is the secret message produced from the message Meet you in the
park using the Caesar Cipher?
Solution: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
1) Replace the letters in the message with numbers
M e e t y o u I n t h e p a r k
12 4 4 19 24 14 20 8 13 19 7 4 15 0 17 10
_ a
n
x
m x
n
0
m x and m c m a
with seed x m c ax x
n n
e e e
+ =
+
0
0 1
0 0 , 2
, mod ) (
2) Replace each of these numbers ply f(p)=(p+3)mod 26
15 7 7 22 1 17 23 11 16 22 10 7 18 3 20 13
3) Translating this back to letters produces encrypted message
15 7 7 22 1 17 23 11 16 22 10 7 18 3 20 13
P H H W B R X L Q W K H S D U N
Decryption: the process of determining the original message from the encrypted
message is call decryption
Remark: instead of shifting each letter by 3, we can shift each letter by k so that
f(p)=(p+k) mod26 (called shift cipher)
Example: Decrypt the message using the Caesar Cipher:
E O X H M H D C V
4 14 23 7 12 7 3 16 21
1 11 20 4 9 4 0 13 18 ( f(p)=p-3 )
B L U E J E A N S
Do example10 page 208
Using f(p)=(p+3)(mod26)
Decrypt the message
G R Q R W S D V V
6 17 16 17 22 18 3 21 21
3 14 13 14 19 15 0 18 18
D O N O T P A S S
Encrypt the message how are you
H O W A R E Y O U
7 14 22 0 17 4 24 14 20
10 17 25 3 20 7 1 17 23
K R Z D M H B R X
Section 3.5
Primes and Greatest Common Divisors
A prime: is an integer greater than 1 that is divisible only by 1 and by itself.
Definition: a positive integer p greater than 1 is called prime if the only positive factors of
p are 1 and p.
Remark: 1- A positive integer that is greater than 1 and is not prime called composite.
2- The integer n is composite iff there exists an integer a such that an and 1 < a < n.
Examples of prime numbers:
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89 and 97
Theorem1: 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 non decreasing size.
Example2: Find the prime factorization of 100,641,999, and 1024.
Solution: 100 = 2.2.5.5
= 2.5
641 = 641 X 1 (prime)
999 = 3 . 37
1024 = 2
Theorem2: If n composite integer, then n has a prime divisor less than or equal to
Example: Show that 101 is prime
Solution: The only prime not exceeding 101 are 2,3,5 and 7. But 101 is not divisible by
2,3,5, or 7 it follows that 101 is prime.
Example4: Find the prime factorization of 7007.
Solution: start with 2 then 3 then 5 . None of them divides 7007
Try 7, we get 7007 = 7 X 11 X 13
Theorem3: There are infinitely many primes
Mersenne primes: The largest prime known has been an integer of special form 2 1
10
p
n
Example5: page 212
Theorem4: The prime number theorem. The ratio of the number of primes are not
exceeding x and x / lnx approaches 1 as x grows without bound.
Conjectures and open problems about primes.
1- Consider the polynomial f(n) = n - n + 41. This polynomial has the property that f(n) is
prime for all positive integers not exceeding 40.
Eg: f(1) = 41 f(3) = 47 f(2) = 43 f(4) = 53 and so on.
2- Every odd integer n, n > 5 is the sum of 3 primes
Every even integer n, n > 2 is the sum of 2 primes
Eg: 4 = 2 + 2, 6 = 3 + 3, 8 + 5 + 3, 10 = 7 + 3 and so on
3- There are infinitely many primes of the form n + 1 where n is a positive integer.
Eg: 5 = 2 + 1, 17 = 4 + 1, 37 = 6 + 1 and so on
4- The twin prime conjecture: Twin primes are primes that differ by 2 such as 3 and 5, 5
and 7, 11 and 13, 17 and 19.
P + 2 where p is prime and p + 2 is prime or the product of two primes .
89 . 23 2047 2047 1 2
31 1 2
7 1 2
3 1 2
11
5
3
2
! !

!
!
!
because prime Mersenne a not is hile
primes Mersenne are numbers The
Greatest Common Divisors and Least Common Multiples
Definition2: let a and b be integers, not both zero. The largest integer d such that da
and db is called the greatest common divisor of a and b denoted by gcd (a,b).
Remark: One way to find the greatest common divisor of two integers is to find all the
positive common divisors of both integers and then take the largest divisior.
Example10 page 215
What is the greatest common divisor of 24 and 36 ?
Solution: 24:1,2,3,4,6,8,12,24 (factors of 24)
36: 1,2,3,4,6,9,12,16,36 (factors of 36)
g.c.d(24,36) = 12
Example11: Find g.c.d (17,22)
Solution: 17: 1, 17
22: 1, 2,11,22
g.c.d (17, 22) = 1
Definition3: The integers a and b are relatively prime if their greatest common divisor is1.
Example12: Since g.c.d (17,22) = 1 it follows that the integers 17 and 22 are relatively
prime.
Definition4:
n j i whenever a a if
prime relatively pairwise are a a a egers he
j i
n
e e = 1 1 ) , ( gcd
,......., , int
2 1
Example13: Determine whether the following integers are pair wise relatively prime
1- 10, 17, 21
2- 10, 19, and 24
Solution: 1- g.c.d(10,17)=1
g.c.d(17,21)=1
g.c.d(10,21)=1
We calculate that 10,17,21 are pair wise relatively prime.
2- g.c.d(10,19)=1
g.c.d(19,24)=1
g.c.d(10,24)=2>1
So 10,19 and 24 are not pair wise relatively prime.
Remark: Another way to find the prime g.c.d if 2 integers is to use the prime
factorizations of these integers suppose
) , min( ) , min(
1
2 1
2 1
......... ) , ( . .
.........
........
1 1
2 1
2 1
n n
n
n
b a
n
b a
b
n
b b
a
n
a a
p p b a d c g
p p p b
p p p a
!
!
!
Example14: find the g.c.d (120,500) (using method 2, prime factorization)
120 = 2.3.5
} g.c.d(120,500)
= 2.5
= 20
500 = 2.5
10 10
5 2 5 2
least common multiple
Definition5: The least common multiple of the positive integers a and b is the smallest
Positive integer that is divisible by both a and b it is denoted by Lcm(a,b)
Remark: lcm(a,b) =
120
12 10
3 2 2 2 5
500
5 100
) , max( ) , max(
2
) , max(
1
......... .
2 2 1 1 n n
b a
n
b a b a
p p p
Example: What is the least common multiple of
Answer:
Theorem5: let a and b be positive integers then ab = gcd (a,b) . Lcm (a,b)
? 3 2 7 3 2
3 4 2 5 3
and

2 5 4
) 0 , 2 max( ) 3 , 5 max( ) 4 , 3 max( 3 4 2 5 3
7 . 3 . 2
7 . 3 . 2 3 2 , 7 3 2
!
! Lcm
Section 3.6: Integers and Algorithms
Representations of Integers:
Theorem(1): (The base b expansion of n).
Let b be a positive integer greater than1. Then if n is a positive integer, it can be
expressed uniquely in the form
Where k is a non negative integer, are non negative integers less than
b and
Example: 965 = 9 X 10 + 6 X 10 + 5 X 10 the base here is 10
Remark: (1) In theorem 1: The base b expansion of n is denoted by
Example: (245) represents 2.8+4.8+5=165 the base here is 8
(2) Choosing 2 as the base gives binary expansions of integers.
(3) In binary notation each digit is either a0 or a1. So the binary expansions of
an integer is just a bit string.
Example1: What is the decimal expansion of the integer that has (101011111) as its
binary expansion?
Solution:
0 1
1
1
...... a b a b a b a n
k
k
k
k
+ + + + =

k
a a a , ,......... ,
1 0
0 {
k
a

b
k k
a a a a
0 1 1
....

8
2
351 2 1 2 1 2 1
2 1 2 1 2 . 1 2 . 0 2 . 1 2 . 0 2 . 1 ) 101011111 (
0 1 2
3 3 4 5 6 7 8
2
!
!
X X X
X X
Hexadecimal Expansions.
Sixteen is another base used in computer science. The base 16 expansion of an integer
is called its hexadecimal expansion
Remark: The hexadecimal digits used are 0,1,2,,9,A,B,C,D,E and F where the letters
A through F represent the digits corresponding to the numbers 19 through 15 (in
decimal notation)
Example: What is the decimal expansion of the hexadecimal expansion of(2AE0B) ?
Solution:
Remark: Each hexadecimal digit can be represented using four bits for example
Base Conversion:
Constructing the base b expansion of an integer n
16

10
2 3 4
16
) 175627 ( 175627
11 16 . 0 16 . 14 16 . 10 16 . 2 0 2
! !
! B AE
5 ) 0101 ( 1110 5 11100101
2 2 16 2
! ! ! Eand because E
....
0
0
1
1 0 1 0 0
0 0 0
so and b by q divide
b a a bq q by q divide
b a a bq n by n divide first
!
!
This process terminates when we obtain a quotient equal to zero
Example3 page 220: Find the base 8, or octal, expansion of (12345)
Solution: Divide 12345 by 8 12345 = 8.1543 + 1
Divide 1543 by 8 1543 = 8.192 + 7
192 = 8.24 + 0
24 = 8.3 + 0
3 = 8.0 + 3
Because the remainders are the digits of the base 8 expansion of 12345, it follows that
Example4: Find the hexadecimal expansion of (177130)
Solution: 177130 = 16.11070 +10
11070 = 16.691 + 14
691 = 16.43 + 3
43 = 16.2 + 11
2 = 16.0 + 2
10

8 10
30071 12345 =
10
16 10
) 3 2 ( ) 177130 ( EA B so !
Example: Find the binary expansion of (241)
Solution: 241 = 2.120 + 1
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
S0
10
2 10
) 11110001 ( ) 241 ( =
Section 3.8: Matrices
Definition1: A matrices is a rectangular array of numbers. A matrix with rows m and n
column is called an mXn matrix.
Remarks: 1- A matrix with the same number of rows as column is called square.
2- The matrices are equal if they have the same number of rows and the same number
of columns and the corresponding entries in every position are equal.
Example: The matrix is a 3X2 matrix
The matrix is a square matrix 2X2 matrix
Definition: Addition of matrices
Let be mXn matrices. The sum of A and B, denoted by
A+B is the mXn matrix that has as its (i,j) the element. A+B=
Remark: Addition of matrices id defined if the matrices are of same size
Example: Add

0 1
4 2
3 1
2 0
1 1
? A ? A
ij ij
b andB a A = =
ij ij
b a +
] [
ij ij
b a
Definition: multiplication of matrices
Let A be an mXk matrix and B be a kXn matrix. The product of A and B, denoted by AB
is the mXn matrix with its (i,j) the entry equal to the sum of the product of the
corresponding elements from the i th row of A and the j th column of B.
Remark: Multiplication of matrices is not commutative.
Example:
Find AB if it is defined
A is 4X3 B is 3X2 so AB is defined and is 4X2 matrix.
3 3 2 3
2 1 1
0 3 1
1 4 3
4 3
2 2
0 1
2 5 2
3 1 3
2 4 4
2 0 1 4 1 3
0 3 3 2 1 2
1 1 4 0 3 1
2 1 1
0 3 1
1 4 3
0 4 3
3 2 2
1 0 1
X X
defined not is
Add

+ +
+ +
+ +
=

? A
kj ik j j i ij ij
b a b a b a c where c AB + + + = = .....
2 12 1 1

!
0 3
1 1
4 2
2 2 0
1 1 2
4 0 1
andB letA
example:

2 8
13 7
9 8
4 14
) 0 ( 2 ) 1 ( 2 ) 4 ( 0 ) 3 ( 2 ) 1 ( 2 ) 2 ( 0
) 0 ( 0 ) 1 )( 1 ( ) 4 ( 3 ) 3 ( 0 ) 1 )( 1 ( ) 2 ( 3
) 0 )( 1 ( ) 1 )( 1 ( ) 4 ( 2 ) 2 )( 1 ( ) 1 )( 1 ( ) 2 ( 2
) 0 ( 4 1 0 4 1 3 4 1 0 2 1
0 3
1 1
4 2
2 2 0
0 1 3
1 1 2
4 0 1
AB
BA soAB
BA AB Solution
BA AB does
andB letA
{

!
!

!
2 3
3 4
,
3 5
2 3
:
?
1 1
1 2
1 2
1 1
Transpose of a matrix
The transpose of the matrix is the matrix
Definition: A square matrix A is called symmetric if A=A
Example: The matrix is symmetric.
Definition: Zero-one Matrix
A matrix with entries that are either 0 or 1 is called a zero- one matrix
A A A A A
matrix identity the called is I matrix The In
r
nXn
.... . .
1 ....... 00
0 ... 0001
0 ..... 001
0 ..... 010
0 ..... 100
!

6 5 4
3 2 1

6 3
5 2
4 1
T
A


!
2 5 2
3 1 3
2 3 4
2 3 2
5 1 4
2 3 4
for A
T
T

0 1 0
1 0 1
0 1 1
Read definition8 from the book page 252
Example: Find the join and meet of the zero matrices
Solution: joint of A and B is
The meet of A and B is
Example: Find the Boolean product of A and B where

= =
= v

= =
=
otherwise
b or b if
b b
otherwise
b b if
b b
0
1 1 1
0
1 1
2 1
2 1
2 1
2 1

=
0 1 1
0 1 0
,
0 1 0
1 0 1
B A

v v v
v v v
= v
0 1 1
1 1 1
0 0 1 1 1 0
0 1 1 0 0 1
B A



!
0 1 0
0 0 0
0 0 1 1 1 0
0 1 1 0 0 1
B A

!
1 1 0
0 1 1
0 1
1 0
0 1
andB A
Solution: The Boolean product of A and B denoted by is
Remark:1- let A be a square zero- one matrix then
2- note that
Example:
B A


v v v
v v v
v v v
=

v v v
v v v
v v v
= O
0 1 1
1 1 0
0 1 1
0 0 0 1 0 1
1 0 1 0 0 0
0 0 0 1 0 1
1 0 0 1 1 0 1 1 0 0 1 1
1 1 0 0 1 1 1 0 0 1 1 0
1 0 0 1 1 0 1 1 0 0 1 1
B A
? A
A A A A A
r
! .....
? A
In A =
0
? A
? A

! !

!
1 0 1
1 0 0
0 1 1
0 1 1
0 0 1
1 0 0
2
2
A A A
A find letA

S-ar putea să vă placă și