Scilab Textbook Companion for Discrete Mathematics by

Scilab Textbook Companion for
Discrete Mathematics
by S. Lipschutz, M. Lipson And V. H. Patil1
Created by
Neerja Bahuguna
B.Tech. (pursuing)
Computer Engineering
Echelon Institute of Technology, Faridabad
College Teacher
Prashant Gupta, E. I. T. ,Faridabad
Cross-Checked by
Prashant Dave, IIT Bombay
May 17, 2016
1 Funded
by a grant from the National Mission on Education through ICT,
http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab
codes written in it can be downloaded from the ”Textbook Companion Project”
section at the website http://scilab.in
Book Description
Title: Discrete Mathematics
Author: S. Lipschutz, M. Lipson And V. H. Patil
Publisher: Tata McGraw - Hill Education
Edition: 3
Year: 2009
ISBN: 9780070669123
1
Scilab numbering policy used in this document and the relation to the
above book.
Exa Example (Solved example)
Eqn Equation (Particular equation of the above book)
AP Appendix to Example(Scilab Code that is an Appednix to a particular
Example of the above book)
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.
2
Contents
List of Scilab Codes
4
1 Set Theory
5
3 Functions and Algorithms
9
5 Vectors and Matrices
12
6 Counting
16
7 Probability Theory
23
8 Graph Theory
40
9 Directed graphs
43
11 Properties of the integers
45
12 Algebraic Systems
50
15 Boolean Algebra
52
16 Recurrence relations
54
3
List of Scilab Codes
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
1.8
1.9
1.13
1.14
1.18
3.8
3.9
3.10
3.11
5.2
5.3
5.5
5.6
5.7
5.8
5.9
5.10
5.13
5.14
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
inclusion exclusion principle . . . . . . . . . . .
Inclusion exclusion principle . . . . . . . . . . .
Power sets . . . . . . . . . . . . . . . . . . . .
Power sets . . . . . . . . . . . . . . . . . . . .
Mathematical induction . . . . . . . . . . . . .
Recursively defined functions . . . . . . . . . .
Cardinality . . . . . . . . . . . . . . . . . . . .
Polynomial evaluation . . . . . . . . . . . . . .
Greatest Common Divisor . . . . . . . . . . . .
Vector operations . . . . . . . . . . . . . . . .
Column vectors . . . . . . . . . . . . . . . . . .
Matrix addition and Scalar multiplication . . .
Matrix multiplication . . . . . . . . . . . . . .
Matrix multiplication . . . . . . . . . . . . . .
Algebra of square matrices . . . . . . . . . . .
Invertible matrices . . . . . . . . . . . . . . . .
Determinants . . . . . . . . . . . . . . . . . . .
Matrix solution of a system of linear equations
Inverse of a square matrix . . . . . . . . . . . .
Sum rule principle . . . . . . . . . . . . . . . .
Product rule principle . . . . . . . . . . . . . .
Factorial notation . . . . . . . . . . . . . . . .
Binomial coefficients . . . . . . . . . . . . . . .
Permutations . . . . . . . . . . . . . . . . . . .
Permutations with repetitions . . . . . . . . . .
Combinations . . . . . . . . . . . . . . . . . . .
Combinations . . . . . . . . . . . . . . . . . . .
Combinations with repetitions . . . . . . . . .
4
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5
6
7
7
7
9
9
10
10
12
12
13
13
13
14
14
14
15
15
16
17
17
18
18
19
19
20
20
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
6.14
6.15
6.16
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
7.10
7.12
7.13
7.14
7.15
7.16
7.17
7.18
7.19
8.1
8.2
9.6
9.8
11.2
11.4
11.5
11.6
11.9
11.12
11.19
12.4
12.17
12.18
15.1
15.2
Ordered partitions . . . . . . . . . . . . . . . . . . . .
Unordered partitions . . . . . . . . . . . . . . . . . . .
Inclusion exclusion principle revisited . . . . . . . . .
Sample space and events . . . . . . . . . . . . . . . . .
Finite probability spaces . . . . . . . . . . . . . . . . .
Equiprobable spaces . . . . . . . . . . . . . . . . . . .
Addition principle . . . . . . . . . . . . . . . . . . . .
Conditional probability . . . . . . . . . . . . . . . . .
Multiplication theorem for conditional probability . .
Independent events . . . . . . . . . . . . . . . . . . . .
Independent events . . . . . . . . . . . . . . . . . . . .
Independent repeated trials . . . . . . . . . . . . . . .
Repeated trials with two outcomes . . . . . . . . . . .
Random variables . . . . . . . . . . . . . . . . . . . .
Probability distribution of a random variable . . . . .
Probability distribution of a random variable . . . . .
Expectation of a random variable . . . . . . . . . . . .
Variance and standard deviation of a random variable
Binomial diatribution . . . . . . . . . . . . . . . . . .
Chebyshev inequality . . . . . . . . . . . . . . . . . .
Sample mean and Law of large numbers . . . . . . . .
Paths and connectivity . . . . . . . . . . . . . . . . .
Minimum spanning tree . . . . . . . . . . . . . . . . .
Adjacency matrix . . . . . . . . . . . . . . . . . . . .
Path matrix . . . . . . . . . . . . . . . . . . . . . . .
Division algorithm . . . . . . . . . . . . . . . . . . . .
Primes . . . . . . . . . . . . . . . . . . . . . . . . . .
Greatest Common Divisor . . . . . . . . . . . . . . . .
Euclidean algorithm . . . . . . . . . . . . . . . . . . .
Fundamental theorem of Arithmetic . . . . . . . . . .
Congruence relation . . . . . . . . . . . . . . . . . . .
Linear congruence equation . . . . . . . . . . . . . . .
Properties of operations . . . . . . . . . . . . . . . . .
Roots of polynomial . . . . . . . . . . . . . . . . . . .
Roots of polynomial . . . . . . . . . . . . . . . . . . .
Basic definitions in boolean algebra . . . . . . . . . . .
Boolean algebra as lattices . . . . . . . . . . . . . . .
5
21
21
22
23
24
25
25
26
28
29
30
30
31
32
33
34
35
36
37
38
38
40
41
43
43
45
45
46
46
47
48
48
50
50
51
52
53
Exa 16.14 Linear homogenous recurrence relations with constant
coefficients . . . . . . . . . . . . . . . . . . . . . . . .
Exa 16.15 Solving linear homogenous recurrence relations with constant coefficients . . . . . . . . . . . . . . . . . . . . .
Exa 16.16 Solving linear homogenous recurrence relations with constant coefficients . . . . . . . . . . . . . . . . . . . . .
Exa 16.17 Solving linear homogenous recurrence relations with constant coefficients . . . . . . . . . . . . . . . . . . . . .
Exa 16.18 Solving linear homogenous recurrence relations with constant coefficients . . . . . . . . . . . . . . . . . . . . .
Exa 16.19 Solving general homogenous linear recurrence relations
6
54
55
56
57
58
59
Chapter 1
Set Theory
Scilab code Exa 1.8 inclusion exclusion principle
1
2
3
4
5
6
7
8
9
10
11
disp ( ’ To f i n d : number o f m a t h e m a t i c s s t u d e n t s t a k i n g
a t l e a s t one o f t h e l a n g u a g e s F r e n c h ( F ) , German (G)
and R u s s i a n (R) ’ )
F =65; // number o f s t u d e n t s s t u d y i n g F r e n c h
G =45; // number o f s t u d e n t s s t u d y i n g German
R =42; // number o f s t u d e n t s s t u d y i n g R u s s i a n
FandG =20; // number o f s t u d e n t s s t u d y i n g F r e n c h and
German
FandR =25; // number o f s t u d e n t s s t u d y i n g F r e n c h and
Russian
GandR =15;
// number o f s t u d e n t s s t u d y i n g German and
Russian
FandGandR =8; // number o f s t u d e n t s s t u d y i n g French ,
German and R u s s i a n
//By i n c l u s i o n −e x c l u s i o n p r i n c i p l e
ForGorR = F + G +R - FandG - FandR - GandR + FandGandR ;
disp ( ForGorR , ’ t h e number o f s t u d e n t s s t u d y i n g
a t l e a s t one o f t h e l a n g u a g e s : ’ )
7
Scilab code Exa 1.9 Inclusion exclusion principle
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
26
disp ( ’ I n a c o l l e g e , 1 2 0 m a t h e m a t i c s s t u d e n t s can o p t
f o r e i t h e r F r e n c h ( F ) , German (G) o r R u s s i a n (R) ’ )
n =120; // t o t a l number o f s t u d e n t s
F =65; // number o f s t u d e n t s s t u d y i n g F r e n c h
G =45; // number o f s t u d e n t s s t u d y i n g German
R =42; // number o f s t u d e n t s s t u d y i n g R u s s i a n
FandG =20; // number o f s t u d e n t s s t u d y i n g F r e n c h and
German
FandR =25; // number o f s t u d e n t s s t u d y i n g F r e n c h and
Russian
GandR =15; // number o f s t u d e n t s s t u d y i n g German and
Russian
FandGandR =8; // number o f s t u d e n t s s t u d y i n g French ,
German and R u s s i a n
disp ( ’ u s i n g i n c l u s i o n −e x c l u s i o n p r i n c i p l e : ’ )
ForGorR = F + G +R - FandG - FandR - GandR + FandGandR ;
disp ( ForGorR , ’ number o f s t u d e n t s s t u d y i n g F r e n c h o r
German o r R u s s i a n ’ )
FGnR = FandG - FandGandR ;
disp ( FGnR , ’ number o f s t u d e n t s s t u d y i n g F r e n c h and
German but n o t R u s s i a n ’ )
FRnG = FandR - FandGandR ;
disp ( FRnG , ’ number o f s t u d e n t s s t u d y i n g F r e n c h and
R u s s i a n but n o t German ’ )
GRnF = GandR - FandGandR ;
disp ( GRnF , ’ number o f s t u d e n t s s t u d y i n g German and
R u s s i a n but n o t F r e n c h ’ )
OF =F - FGnR - FandGandR - FRnG ;
disp ( OF , ’ number o f s t u d e n t s s t u d y i n g Only F r e n c h ’ )
OG =G - FGnR - FandGandR - GRnF ;
disp ( OG , ’ number o f s t u d e n t s s t u d y i n g Only German ’ )
OR =R - FRnG - FandGandR - GRnF ;
disp ( OR , ’ number o f s t u d e n t s s t u d y i n g Only R u s s i a n ’ )
k =n - ForGorR ;
disp (k , ’ number o f s t u d e n t s n o t s t u d y i n g any o f t h e
languages ’)
8
Scilab code Exa 1.13 Power sets
1 x =10; // number o f members o f s e t X
2 P =2^ x // number o f members o f t h e power s e t o f X
3 q =P -1; // x i t s e l f i s n o t t h e p r o p e r s u b s e t . Hence i t
isn ’ t counted
4 disp (q , ’ number o f members o f p o w e r s e t P which a r e
proper subsets of x are : ’)
Scilab code Exa 1.14 Power sets
// e a t a b l e s f o r s a l a d p r e p a r a t i o n 1=
o n i o n ,2= tomato ,3= c a r r o t ,4= cabbage ,5= cucumber
2 p = length ( A ) ;
// t o t a l number o f e a t a b l e s a v a i l a b l e
3 n =2^ p -1;
// no s a l a d can be made w i t h o u t a t l e a s t one
o f t h e e a t a b l e s . Hence n u l l s e t i s n ’ t c o u n t e d
4 disp (n , ’ number o f d i f f e r e n t s a l a d s t h a t can be
prepared using the given e a t a b l e s ’ )
1 A =[1 ,2 ,3 ,4 ,5];
Scilab code Exa 1.18 Mathematical induction
1
2
3
4
5
6
7
8
U1 =1; // g i v e n
U2 =5; // g i v e n
P =[];
for i =1:2
P ( i ) =3^ i -2^ i ;
disp ( P ( i ) )
end
disp ( ’P ( 1 )=U( 1 ) and P ( 2 )=U( 2 ) ’ ) ;
9
9
disp ( ’ h e n c e Un=3ˆn−2ˆn f o r a l l n b e l o n g i n g t o N ’ ) ;
10
Chapter 3
Functions and Algorithms
Scilab code Exa 3.8 Recursively defined functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function [ k ]= fact ( a )
k = -1;
if (a <0| a >200)
disp ( ” I n v a l i d ” ) ;
break ;
else
if ( a ==1| a ==0)
k =1;
else
k = a * fact (a -1) ;
end
end
endfunction
a =4;
p = fact ( a ) ;
disp (p , ’ t h e v a l u e o f 4 ! i s ’ )
Scilab code Exa 3.9 Cardinality
11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
x =1;
y =2;
z =3;
A =[ x ,y , z ];
disp ( ’ c a r d i n a l i t y o f s e t A i s : ’ )
length ( A )
B =[1 ,3 ,5 ,7 ,9]
disp ( ’ c a r d i n a l i t y o f s e t B i s : ’ )
length ( B )
// 3 . 9 ( b )
disp ( ’ t h e s e t E h a s t h e f o l l o w i n g e l e m e n t s )
E = [ 2 , 4 , 6 % i n f ] // s e t E i s t h e s e t o f a l l p o s i t i v e
e v e n numbers and N i s t h e s e t o f a l l n a t u r a l
numbers
d i s p ( ’ function f : N to E is defined . So , E has the same
cardinality as N ’)
disp ( ’ s e t E i s c o u n t a b l y i n f i n i t e : ’ )
for x =2:2: %inf
y =2* x ;
disp ( y )
end
Scilab code Exa 3.10 Polynomial evaluation
1 x = poly (0 , ’ x ’ ) ;
2 p = 2* x ^3 -7* x ^2+4* x -15;
3 disp (p , ’ t h e p o l y n o m i a l i s ’ )
4 k = horner (p ,5) ;
5 disp (k , ’ v a l u e o f t h e p o l y n o m i a l a t x=5 i s ’ )
Scilab code Exa 3.11 Greatest Common Divisor
12
1 V = int32 ([258 ,60]) ;
2 thegcd = gcd ( V ) ;
3 disp ( thegcd , ’ t h e gcd o f t h e two numbers 258 and 60
is ’)
13
Chapter 5
Vectors and Matrices
Scilab code Exa 5.2 Vector operations
1
2
3
4
5
6
7
8
9
10
11
u =[2 ,3 , -4];
v =[1 , -5 ,8];
u+v
5* u
-v
2* u -3* v
u .* v ;
k = sum ( u .* v ) ;
disp (k , ’ d o t p r o d u c t o f t h e two v e c t o r s ’ )
l = norm ( u ) ;
disp (l , ’ norm o r l e n g t h o f t h e v e c t o r u ’ )
Scilab code Exa 5.3 Column vectors
1
2
3
4
u =[5 ,3 , -4] ’
v =[3 , -1 , -2] ’
2* u -3* v
k = sum ( u .* v ) ;
14
disp (k , ’ The d o t p r o d u c t o f t h e two v e c t o r s u and v
is : ’)
6 l = norm ( u ) ;
7 disp (l , ’ The l e n g t h o r norm o f t h e v e c t o r u i s : ’ )
5
Scilab code Exa 5.5 Matrix addition and Scalar multiplication
1 A =[1 , -2 ,3;0 ,4 ,5];
2 B =[4 ,6 ,8;1 , -3 , -7];
3 k=A+B;
4 disp (k , ’ The a d d i t i o n
o f t h e two m a t r i c e s A and B i s :
’)
5 m =3* A ;
6 disp (m , ’ The m u l t i p l i c a t i o n o f a v e c t o r w i t h a s c a l a r
is : ’)
7 p =2* A -3* B
Scilab code Exa 5.6 Matrix multiplication
1
2
3
4
5
6
7
8
a =[7 , -4 ,5];
b =[3 ,2 , -1] ’;
k=a*b;
disp (k , ’ p r o d u c t o f a and b i s ; ’ )
p =[6 , -1 ,8 ,3];
q =[4 , -9 , -2 ,5] ’;
l=p*q;
disp (l , ’ p r o d u c t o f p and q i s : ’ )
Scilab code Exa 5.7 Matrix multiplication
15
1
2
3
4
5
6
7
8
A =[1 3;2 -1];
B =[2 0 -4;5 -2 6];
A*B
A =[1 2;3 4]
B =[5 6;0 -2];
A*B
B*A
disp ( ’ m a t r i x m u l i t p l i c a t i o n i s n o t c o m m u t a t i v e s i n c e
AB may n o t be e q u a l t o BA ’ )
Scilab code Exa 5.8 Algebra of square matrices
1
2
3
4
5
6
7
A =[1 2;3 -4];
A2 = A * A // m u l t i p l y i n g A by i t s e l f
A3 = A2 * A
f =2* A2 -3* A +5;
disp (f , ’ f o r t h e f u n c t i o n f ( x ) =2xˆ2−3x +5 , f (A) i s : ’ )
g = A2 +3* A -10;
disp (g , ’ f o r t h e f u n c t i o n g ( x )=xˆ2+3x −10 , g (A) i s ’ )
Scilab code Exa 5.9 Invertible matrices
1 A =[1 0 2;2 -1 3;4 1 8];
2 B =[ -11 2 2; -4 0 1;6 -1 -1];
3 A*B
4 disp ( ’ s i n c e A∗B i s i d e n t i t y m a t r i x , A and B a r e
i n v e r t i b l e and i n v e r s e o f e a c h o t h e r ’ )
Scilab code Exa 5.10 Determinants
16
1
2
3
4
5
6
7
8
A =[5 4;2 3];
det ( A ) ;
disp ( det ( A ) , ’ d e t e r m i n a n t o f A ’ )
B =[2 1; -4 6]
det ( B ) ;
disp ( det ( B ) , ’ d e t e r m i n a n t o f B ’ )
C =[2 1 3;4 6 -1;5 1 0]
disp ( det ( C ) , ’ d e t e r m i n a n t o f C ’ )
Scilab code Exa 5.13 Matrix solution of a system of linear equations
// l e f t hand s i d e o f
1 A =[1 2 1;2 5 -1;3 -2 -1];
the system o f e q u a t i o n s
// r i g h t hand s i d e o r
the constants in the equations
X =[];
X=A\B ;
// u n i q u e s o l u t i o n f o r t h e s y s t e m o f
equations
x = X (1)
y = X (2)
z = X (3)
2 B =[3 -4 5] ’;
3
4
5
6
7
Scilab code Exa 5.14 Inverse of a square matrix
1 A =[1 0 2;2 -1 3;4 1 8];
2 P = rref ([ A , eye (3 ,3) ]) ;
3 disp (P , ’ c a n o n i c a l form o f m a t r i x A : ’ )
4 disp ( ’ l e f t s i d e o f t h e m a t r i x P i s t h e i d e n t i t y
matrix so the r i g h t s i d e i s the i n v e r s e of A ’ )
5 inverseA = P (: ,4:6)
17
Chapter 6
Counting
Scilab code Exa 6.1 Sum rule principle
// number o f male p r o f e s s o r s t e a c h i n g
calculus
2 F =5;
// number o f f e m a l e p r o f e s s o r s t e a c h i n g
calculus
3 T=M+F ;
4 disp (T , ’ number o f ways a s t u d e n t can c h o o s e a
calculus professor ’)
1 M =8;
5
6 E =[2 ,3 ,5 ,7];
// e v e n t o f c h o o s i n g a p r i m e number
l e s s t h a n 10
7 F =[2 ,4 ,6 ,8];
// e v e n t o f c h o o s i n g an e v e n number
l e s s t h a n 10
8 G = intersect (E , F ) ;
// e v e n t o f g e t t i n g an e v e n and
p r i m e number
9 H = length ( E ) + length ( F ) - length ( G ) ;
10 disp (H , ’ e v e n t o f g e t t i n g an e v e n o r a p r i m e number ’ )
11
12 E =[11 ,13 ,17 ,19]; // e v e n t o f c h o o s i n g a p r i m e number
b e t w e e n 10 and 20
13 F =[12 ,14 ,16 ,18];
// e v e n t o f c h o o s i n g an e v e n number
b e t w e e n 10 and 20
18
// e v e n t o f c h o o s i n g a number which
i s prime or even
15 k = length ( G ) ;
16 disp (k , ’ number o f ways o f c h o o s i n g a number which i s
prime or even ’ )
14 G = union (E , F ) ;
Scilab code Exa 6.2 Product rule principle
1
2
3
4
5
6
7
disp ( ’ a l i c e n s e p l a t e c o n t a i n s two l e t t e r s f o l l o w e d
by t h r e e d i g i t s where f i r s t d i g i t can n o t be z e r o
’)
n =26; // number o f e n g l i s h l e t t e r s
n * n ; // number o f ways o f c h o o s i n g two l e t t e r s i n
the l i c e n s e p l a t e
p =10; // number o f d i g i t s (0 −9)
(p -1) * p * p ; // number o f ways t o s e l e c t t h e t h r e e
d i g i t s with the f i r s t d i g i t not being z e r o
k = n * n *( p -1) * p * p ;
disp (k , ’ t o t a l number o f l i c e n s e p l a t e s t h a t can be
printed ’)
8
9
disp ( ’ a p r e s i d e n t , a s e c r e t a r y and a t r e a s u r e r h a s
t o be e l e c t e d i n an o r g a −n i s a t i o n o f 26 members .
No p e r s o n i s e l e c t e d t o more t h a n one p o s t i o n ’ )
10 t =26;
// t o t a l number o f members i n t h e o r g a n i s a t i o n
11 j = t *( t -1) *( t -2) ;
12 disp (j , ’ number o f ways t o e l e c t t h e t h r e e o f f i c e r s (
president , secretary , treasurer ’)
Scilab code Exa 6.3 Factorial notation
1
2
disp ( ’ To f i n d : f a c t o r i a l o f a 6 ’ )
facto2 =2*1;
19
3 facto3 =3* facto2
4 facto4 =3* facto3
5 facto4 =4* facto3
6 facto5 =5* facto4
7 facto6 =6* facto5
8
9 k =8*7* factorial (6) / factorial (6) ;
10 disp (k , ’ v a l u e o f 8 ! / 6 ! i s : ’ )
11 j =12*11*10* factorial (9) / factorial (9) ;
12 disp (j , ’ v a l u e o f 1 2 ! / 9 ! i s : ’ )
Scilab code Exa 6.4 Binomial coefficients
1
function [ k ]= func1 (n , r ) // c a l c u l a t i n g b i n o m i a l
coefficient
k = factorial ( n ) /( factorial ( r ) * factorial (n - r ) ) ;
endfunction
func1 (8 ,2)
func1 (9 ,4)
func1 (12 ,5)
func1 (10 ,3)
func1 (13 ,1)
2
3
4
5
6
7
8
9
10 p = factorial (10) /( factorial (10 -7) * factorial (7) ) ;
// c a l c u l a t i n g 10C7
11 q = factorial (10) /( factorial (10 -3) * factorial (3) )
12
13
14
c a l c u l a t i n g 10C3
disp (p , ’ v a l u e o f 10C7 i s ’ )
// 10−7=3 s o 10C7 can a l s o be computed a s 10C3
// b o t h p and q have same v a l u e s but s e c o n d method
s a v e s t i m e and s p a c e
Scilab code Exa 6.5 Permutations
20
//
1
2
3
4
5
6
7
disp ( ’ f i n d i n g t h e number o f t h r e e − l e t t e r words u s i n g
o n l y t h e g i v e n s i x l e t t e r s (A, B , C , D, E , F ) w i t h o u t
repetition ’)
n =6; // t o t a l number o f l e t t e r s
l1 = n ; // number o f ways i n which f i r s t l e t t e r o f t h e
word can be c h o s e n
l2 =n -1; // number o f ways i n which s e c o n d l e t t e r o f
t h e word can be c h o s e n
l3 = n -2; // number o f ways i n which t h i r d l e t t e r can
be c h o s e n
k = l1 * l2 * l3 ;
disp (k , ’ number o f t h r e e − l e t t e r words
possible ’)
Scilab code Exa 6.6 Permutations with repetitions
1 function [ k ]= funct1 (n ,p , q )
2 k = factorial ( n ) /( factorial ( p ) * factorial ( q ) ) ;
3 endfunction
4 k = funct1 (7 ,3 ,2)
// i n ”BENZENE” t h r e e l e t t e r s
are
a l i k e ( t h e t h r e e Es ) and two a r e a l i k e ( t h e two Ns
)
5 disp (k , ’ The number o f s e v e n − l e t t e r words t h a t can be
f o r m e d u s i n g l e t t e r s o f t h e word BENZENE ’ )
6
7
disp ( ’ a s e t o f 4 i n d i s t i n g u i s h a b l e r e d c o l o u r e d
f l a g s , 3 i n d i s t i n g u i s h a b l e w h i t e f l a g s and a b l u e
f l a g i s given ’)
8 j = funct1 (8 ,4 ,3) ;
9 disp (j , ’ number o f d i f f e r e n t s i g n a l s , e a c h c o n s i s t i n g
of eight flags ’)
Scilab code Exa 6.7 Combinations
21
1
2
3
4
5
6
disp ( ’ f o u r o b j e c t s a r e g i v e n ( a , b , c , d ) and t h r e e a r e
taken at a time ’ )
combinations = factorial (4) /( factorial (4 -3) *
factorial (3) ) ;
disp ( combinations , ’ number o f c o m b i n a t i o n s o f t h e
four objects given ’)
k = factorial (3) ; // number o f p e r m u t a t i o n s o f o b j e c t s
in a combination
permutations = combinations * k ;
disp ( permutations , ’ t o t a l number o f p e r m u a t a t i o n s f o r
the problem ’ )
Scilab code Exa 6.8 Combinations
1 function [ k ]= myfunc (n , r )
2 k = factorial ( n ) /( factorial (n - r ) * factorial ( r ) ) ;
3 endfunction
4 k = myfunc (8 ,3) ;
5 disp (k , ’ t h e number o f c o m m i t t e e s o f t h r e e t h a t can
be f o r m e d o u t o f e i g h t p e o p l e ’ )
6
7
8
9
10
11
cows = myfunc (6 ,3) // number o f ways t h a t a f a r m e r can
c h o o s e 3 cows o u t o f 6 cows
pigs = myfunc (5 ,2) // number o f ways t h a t a f a r m e r can
choose 2 p i g s out o f 5 p i g s
hens = myfunc (8 ,4) // number o f ways t h a t a f a r m e r can
choose 4 hens out o f 8 hens
p = cows * pigs * hens ;
disp (p , ’ t o t a l number o f ways t h a t a f a r m e r can
choose a l l these animals ’)
Scilab code Exa 6.9 Combinations with repetitions
22
1
2
3
4
5
// e a c h s o l u t i o n
t o t h e e q u a t i o n can be v i e w e d a s a
combination of o b j e c t s
r =18; // number o f o b j e c t s
M =3;
// k i n d s o f o b j e c t
m = factorial ( r +( M -1) ) /( factorial ( r +( M -1) -(M -1) ) *
factorial (M -1) ) ;
disp (m , ’ number o f non n e g a t i v e i n t e g e r s o l u t i o n s o f
t h e g i v e n e q u a t i o n x+y+z =18 ’ )
Scilab code Exa 6.14 Ordered partitions
1 c1 =3; // number o f t o y s t h a t t h e y o u n g e s t
2
3
4
5
6
child
should get
c2 =2; // number o f t o y s t h a t t h e t h i r d c h i l d s h o u l d
get
c3 =2; // number o f t o y s t h a t t h e s e c o n d c h i l d s h o u l d
get
c4 =2;
// number o f t o y s t h a t t h e e l d e s t s o n s h o u l d
get
m = factorial (9) /( factorial (3) * factorial (2) * factorial
(2) * factorial (2) ) ;
disp (m , ’ number o f ways n i n e t o y s can be d i v i d e d
between f o u r c h i l d r e n with the youngest son
g e t t i n g 3 t o y s and o t h e r s g e t t i n g 2 e a c h ’ )
Scilab code Exa 6.15 Unordered partitions
1 p =12;
// t o t a l number o f s t u d e n t s
2 t =3;
// number o f teams o r p a r t i t i o n
3 disp ( ’ e a c h p a r t i t i o n o f t h e s t u d e n t s can be a r r a n g e d
i n 3 ! ways a s an o r d e r e d p a r t i t i o n ’ )
4 r = factorial (12) /( factorial (4) * factorial (4) * factorial
(4) ) // number o f o r d e r e d p a r t i t i o n s
23
5 m = r / factorial ( t ) ;
// number o f u n o r d e r e d p a r t i t i o n s
6 disp (m , ’ number o f ways t h a t 12 s t u d e n t s can be
p a r t i t i o n e d i n t o t h r e e teams s o t h a t e a c h team
consists of 4 students ’)
Scilab code Exa 6.16 Inclusion exclusion principle revisited
// number o f e l e m e n t s i n t h e s e t o f p o s i t i v e
i n t e g e r s not e x c e e d i n g 1000
A = U /3;
// number o f e l e m e n t s i n t h e s u b s e t o f
i n t e g e r s d i v i s i b l e by 3
B = U /5;
// number o f e l e m e n t s i n t h e s u b s e t o f
i n t e g e r s d i v i s i b l e by 5
C = U /7;
// number o f e l e m e n t s i n t h e s u b s e t o f
i n t e g e r s d i v i s i b l e by 7
AandB = floor ( U /(3*5) )
// number o f e l e m e n t s i n t h e
s u b s e t c o n t a i n i n g numbers d i v i s i b l e by b o t h 3 and
5
AandC = floor ( U /(3*7) )
// number o f e l e m e n t s i n t h e
s u b s e t c o n t a i n i n g numbers d i v i s i b l e by b o t h 3 and
7
BandC = floor ( U /(5*7) )
// number o f e l e m e n t s i n t h e
s u b s e t c o n t a i n i n g numbers d i v i s i b l e by b o t h 5 and
7
AandBandC = floor ( U /(3*5*7) ) // number o f e l e m e n t s i n
t h e s u b s e t c o n t a i n i n g numbers d i v i s i b l e by 3 , 5
and 7
s =U -( A + B + C ) +( AandB + AandC + BandC ) -( AandBandC ) ; // By
i n c l u s i o n −e x c l u s i o n p r i n c i p l e
S = round ( s ) ;
disp (S , ’ The number o f i n t e g e r s i n t h e s e t U, which
a r e n o t d i v i s i b l e by 3 , 5 and 7 i s ’ )
1 U =1000;
2
3
4
5
6
7
8
9
10
11
24
Chapter 7
Probability Theory
Scilab code Exa 7.1 Sample space and events
// s a m p l e s p a c e f o r t h e
1 S =[1 ,2 ,3 ,4 ,5 ,6];
r o l l i n g of a die
2 A =[2 ,4 ,6];
3
4
5
6
7
8
// e v e n t t h a t an e v e n number
occurs
B =[1 ,3 ,5];
// e v e n t t h a t an odd number
occurs
C =[2 ,3 ,5];
// e v e n t t h a t a p r i m e number
occurs
disp ( union (A , C ) , ’ s a m p l e s p a c e f o r t h e e v e n t t h a t an
e v e n o r a p r i m e number o c c u r s ’ )
disp ( intersect (B , C ) , ’ s a m p l e s p a c e f o r t h e e v e n t t h a t
an odd p r i m e number o c c u r s ’ )
disp ( setdiff (S , C ) , ’ s a m p l e s p a c e f o r t h e e v e n t t h a t a
p r i m e number d o e s n o t o c c u r ’ )
// I t i s t h e
complement o f t h e s e t C .
intersect (A , B )
// I t i s a n u l l s e t o r n u l l v e c t o r
s i n c e t h e r e can ’ t o c c u r an e v e n and an odd
number s i m u l t a n e o u s l y
9
10 H =0;
11 T =1;
// ” head ” f a c e o f a c o i n
// ” t a i l ” f a c e o f a c o i n
25
12 S =[ ” 000 ” ,” 001 ” ,” 010 ” ,” 011 ” ,” 100 ” ,” 101 ” ,” 110 ” ,” 111 ” ]
;
// s a m p l e s p a c e f o r t h e t o s s o f a c o i n t h r e e
times
13 A =[ ” 000 ” ,” 001 ” ,” 100 ” ];
// e v e n t t h a t two more o r
more h e a d s a p p e a r c o n s e c u t i v e l y
14 B =[ ” 000 ” ,” 111 ” ];
// e v e n t t h a t a l l t o s s e s
a r e t h e same
15 disp ( intersect (A , B ) , ’ s a m p l e s p a c e f o r t h e e v e n t i n
which o n l y h e a d s a p p e a r ’ )
16
17
disp ( ’ E x p e r i m e n t : t o s s i n g a c o i n u n t i l a head a p p e a r s
and t h e n c o u n t i n g t h e number o f t i m e s t h e c o i n
is tossed ’)
18 S =[1 ,2 ,3 ,4 ,5 , %inf ]
// The s a m p l e s p a c e h a s
i n f i n i t e elements in i t
19 disp ( ” S i n c e e v e r y p o s i t i v e i n t e g e r i s an e l e m e n t o f
S , the sample space i s i n f i n i t e ”)
Scilab code Exa 7.2 Finite probability spaces
1
2
3
4
5
6
7
disp ( ’ E x p e r i m e n t : t h r e e c o i n s a r e t o s s e d and t h e
number o f h e a d s a r e o b s e r v e d ’ )
S =[0 ,1 ,2 ,3];
// t h e s a m p l e s p a c e f o r t h e e x p e r i m e n t
where 0 i m p l i e s no heads , 1 i m p l i e s o n l y one head
o u t o f t h e t h r e e c o i n s and s o on
disp ( ” t h e p r o b a b i l i t y s p a c e i s a s f o l l o w s ” )
P0 =1/8; // p r o b a b i l i t y o f g e t t i n g no head on any o f
t h e c o i n s i . e TTT
P1 =3/8; // p r o b a b i l i t y o f g e t t i n g o n l y one head on
any o f t h e c o i n s , o u t o f t h e t h r e e c o i n s i . e HTT,
THT,TTH
P2 =3/8; // p r o b a b i l i t y o f g e t t i n g two heads , o u t o f
t h e t h r e e c o i n s i . e THH, HTH,HHT
P3 =1/8;
// p r o b a b i l i t y o f g e t t i n g a l l t h e t h r e e
h e a d s i . e HHH
26
8
9
10
11
12
13
14
disp ( ”A i s t h e e v e n t t h a t a t l e a s t one head a p p e a r s
and B i s t h e e v e n t t h a t a l l h e a d s o r a l l t a i l s
appear ”)
A =[1 ,2 ,3];
// HHH, HHT, HTH, HTT, THH, THT,TTH
B =[0 ,3];
//HHH, TTT
PA = P1 + P2 + P3 ;
disp ( PA , ’ p r o b a b i l i t y o f o c c u r r e n c e o f e v e n t A ’ )
PB = P0 + P3 ;
disp ( PB , ’ p r o b a b i l i t y o f o c c u r r e n c e o f e v e n t B ’ )
Scilab code Exa 7.3 Equiprobable spaces
1
2
3
4
5
6
7
8
9
10
11
12
13
disp ( ” E x p e r i m e n t : a c a r d i s s e l e c t e d from a d e c k o f
52 c a r d s ” )
disp ( ”A i s t h e e v e n t o f t h e s e l e c t e d c a r d b e i n g a
spade ”)
disp ( ”B i s t h e e v e n t o f t h e s e l e c t e d c a r d b e i n g a
f a c e card ”)
t =52 ;
// t h e t o t a l number o f c a r d s
s =13;
// number o f s p a d e s
PA = s / t ;
disp ( PA , ’ p r o b a b i l i t y o f s e l e c t i n g a s p a d e ’ )
f =12;
// number o f f a c e c a r d s ( j a c k , queen , k i n g )
PB = f / t ;
disp ( PB , ’ p r o b a b i l i t y o f s e l e c t i n g a f a c e c a r d ’ )
sf =3;
// number o f s p a d e f a c e c a r d s
Psf = sf / t ;
disp ( Psf , ” p r o b a b i l i t y o f s e l e c t i n g a s p a d e f a c e c a r d
i s : ”)
Scilab code Exa 7.4 Addition principle
27
1
2
3
4
5
6
7
8
9
10
disp ( ” E x p e r i m e n t : s e l e c t i o n o f a s t u d e n t o u t o f 100
students ”)
M =30;
// no o f s t u d e n t s t a k i n g m a t h e m a t i c s
C =20;
// no o f s t u d e n t s t a k i n g c h e m i s t r y
T =100; // t o t a l no . o f s t u d e n t s
PM = M / T // p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
taking mathematics
PC = C / T // p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
taking chemistry
MnC =10; // no o f s t u d e n t s t a k i n g m a t h e m a t i c s and
chemistry
PMnC = MnC / T
// p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
t a k i n g m a t h e m a t i c s and c h e m i s t r y b o t h
PMorC = PM + PC - PMnC ;
disp ( PMorC , ’ p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
taking mathematics or chemistry ’ )
Scilab code Exa 7.5 Conditional probability
1
2
3
4
5
6
7
8
9
//EXAMPLE 7 . 5 ( a )
disp ( ” E x p e r i m e n t : A d i e i s t o s s e d and t h e o u t c o m e s
a r e o b s e r v e d ”);
disp ( ”To f i n d : p r o b a b i l i t y (PM) o f an e v e n t t h a t
one o f t h e d i c e i s 2 i f t h e sum i s 6 ” ) ;
10
11
12 E =[ ” ( 1 , 5 ) ” ,” ( 2 , 4 ) ” ,” ( 3 , 3 ) ” ,” ( 4 , 2 ) ” ,” ( 5 , 1 ) ” ]
//
e v e n t t h a t t h e sum o f t h e two numbers on t h e two
dice is 6
28
13
14
15 A =[ ” ( 2 , 1 ) ” ,” ( 2 , 2 ) ” ,” ( 2 , 3 ) ” ,” ( 2 , 4 ) ” ,” ( 2 , 5 ) ” ,” ( 2 , 6 ) ” ,”
( 1 , 2 ) ” ,” ( 3 , 2 ) ” ,” ( 4 , 2 ) ” ,” ( 5 , 2 ) ” ,” ( 6 , 2 ) ” ]
t h a t 2 a p p e a r s on a t l e a s t one d i e
// e v e n t
16
17
18 B = intersect (A , E )
// p o s s i b l e c o m b i n a t i o n o f
numbers on two d i e s u c h t h a t t h e i r sum i s 6 and 2
a p p e a r s a t l e a s t on one d i e
19
20
21 PM =2/5
// s i n c e E h a s 5 e l e m e n t s and B h a s 2
elements
22
23
24
25
26
27 //EXAMPLE 7 . 5 ( b )
28
29 disp ( ”A c o u p l e h a s two c h i l d r e n ” ) ;
30
31
32 b =1;
// boy c h i l d
33
34 g =2;
// g i r l c h i l d
35
36 S =[11 ,12 ,21 ,22] ;
// s a m p l e s p a c e where 11 i m p l i e s
b o t h c h i l d r e n b e i n g boys , 1 2 i m p l i e s f i r s t c h i l d
b e i n g a boy and t h e s e c o n d c h i l d b e i n g a g i r l
and s o on
37
38
disp ( ”To f i n d : p r o b a b i l i t y (PM) t h a t b o t h c h i l d r e n
a r e boys ”);
39
40
41
29
42 // 7 . 5 ( b ) . i
43
44 L = S (: ,1:3)
// r e d u c e d s a m p l e s p a c e i f
t h a t one o f t h e c h i l d r e n i s a boy
45
46
47 PM =1/ length ( L )
48
49
50 // 7 . 5 ( b ) . i i
51
52 R = S (: ,1:2)
// r e d u c e d s a m p l e s p a c e
if
it
it
i s known
i s known
t h a t t h e o l d e r c h i l d i s a boy
53
54
55 PM =1/ length ( R )
Scilab code Exa 7.6 Multiplication theorem for conditional probability
1
2
3
4
5
6
7
disp ( ”A bag c o n t a i n s 12 i t e m s o f which f o u r a r e
d e f e c t i v e . Three i t e m s a r e drawn a t random , one
a f t e r the o t h e r ”);
s =12;
// t o t a l i t m e s i n t h e bag
d =4; // d e f e c t i v e i t e m s i n t h e bag
Pf =( s - d ) / s ; // p r o b a b i l i t y t h a t t h e f i r s t i t e m
drawn i s non d e f e c t i v e
Pe = Pf *[( s -d -1) /( s -1) ]*[( s -d -2) /( s -2) ];
disp ( Pe , ’ p r o b a b i l i t y t h a t a l l t h r e e i t e m s a r e non
defective ’)
// a f t e r t h e f i r s t i t e m i s c h o s e n , t h e s e c o n d i t e m i s
t o be c h o s e n from 1 l e s s t h a n t h e o r i g i n a l number
o f i t e m s i n t h e box and s i m i l a r l y t h e number o f
non d e f e c t i v e i t e m s g e t s d e c r e a s e d by 1 . S i m i l a r l y
, f o r t h e t h i r d draw o f i t e m from t h e box
30
Scilab code Exa 7.7 Independent events
1 H =1;
// h e a d s o f a c o i n
2 T =2;
// t a i l s o f t h e c o i n
3 S =[111 ,112 ,121 ,122 ,211 ,212 ,221 ,222]
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// s a m p l e s p a c e
f o r t h e t o s s o f a c o i n t h r e e t i m e s . 111 i m p l i e s
h e a d s a l l t h r e e t i m e s , 1 1 2 i m p l i e s h e a d s on f i r s t
two t o s s e s and t a i l s on t h e t h i r d t o s s
A =[111 ,112 ,121 ,122]; // e v e n t t h a t f i r s t t o s s i s
heads
B =[111 ,112 ,211 ,212]; // e v e n t t h a t s e c o n d t o s s i s
heads
C =[112 ,211];
// e v e n t t h a t e x a c t l y two h e a d s
a p p e a r i n a row
PA = length ( A ) / length ( S ) ;
disp ( PA , ’ p r o b a b i l i t y o f A i s ’ )
PB = length ( B ) / length ( S ) ;
disp ( PB , ’ p r o b a b i l i t y o f B i s ’ )
PC = length ( C ) / length ( S ) ;
disp ( PC , ’ p r o b a b i l i t y o f C i s ’ )
AnB = intersect (A , B )
AnC = intersect (A , C )
BnC = intersect (B , C )
PAnB = length ( AnB ) / length ( S ) ;
disp ( PAnB , ’ p r o b a b i l i t y o f t h e e v e n t AnB ’ )
PAnC = length ( AnC ) / length ( S ) ;
disp ( PAnC , ’ p r o b a b i l i t y o f t h e e v e n t AnC ’ )
PBnC = length ( BnC ) / length ( S ) ;
disp ( PBnC , ’ p r o b a b i l i t y o f t h e e v e n t BnC ’ )
if (( PA * PB ) == PAnB ) ,
disp ( ”A and B a r e i n d e p e n d e n t ” )
else
disp ( ”A and B a r e d e p e n d e n t ” )
end
31
27
28
29
30
31
32
33
34
35
36
if (( PA * PC ) == PAnC ) ,
disp ( ”A and C a r e i n d e p e n d e n t ” )
else
disp ( ”A and C a r e d e p e n d e n t ” )
end
if (( PB * PC ) == PBnC ) ,
disp ( ”B and C a r e i n d e p e n d e n t ” )
else
disp ( ”B and C a r e d e p e n d e n t ” )
end
Scilab code Exa 7.8 Independent events
1 disp ( ” E x p e r i m e n t :
2 PA =1/4;
// g i v e n
3 PB =2/5;
// g i v e n
4 disp ( ”A and B a r e
A and B b o t h s h o o t a t a t a r g e t ” )
p r o b a b i l i t y of A h i t t i n g the t a r g e t
p r o b a b i l i t y of B h i t t i n g the t a r g e t
i n d e p e n d e n t e v e n t s s o PA∗PB w i l l
be e q u a l t o p r o b a b i l i t y o f t h e e v e n t o f A and B
b o t h h i t t i n g t h e t a r g e t i . e PAnB” )
5 PAnB = PA * PB ;
6 PAorB = PA + PB - PAnB ;
7 disp ( PAorB , ’ p r o b a b i l i t y o f a t l e a s t one o f them
h i t t i n g the t a r g e t ’ )
Scilab code Exa 7.9 Independent repeated trials
1 disp ( ” E x p e r i m e n t : Three h o r s e s r a c e t o g e t h e r t w i c e ” )
2 Ph1 =1/2;
// p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g t h e
race
3 Ph2 =1/3;
race
4 Ph3 =1/6;
race
// p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g t h e
// p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g t h e
32
// s a m p l e s p a c e where
11 i m p l i e s f i r s t h o r s e w i n n i n g t h e f i r s t and
s e c o n d r a c e both , 1 2 i m p l i e s f i r s t h o r s e w i n n i n g
t h e f i r s t r a c e and s e c o n d h o r s e w i n n i n g t h e
s e c o n d r a c e and s o on
P11 = Ph1 * Ph1 // p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g
both r a c e s
P12 = Ph1 * Ph2 // p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g
t h e f i r s t r a c e and s e c o n d h o r s e w i n n i n g t h e
second race
P13 = Ph1 * Ph3 // p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g
t h e f i r s t r a c e and t h i r d h o r s e w i n n i n g t h e s e c o n d
race
P21 = Ph2 * Ph1 // p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g
t h e f i r s t r a c e and f i r s t h o r s e w i n n i n g t h e s e c o n d
race
P22 = Ph2 * Ph2 // p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g
both the r a c e s
P23 = Ph2 * Ph3 // p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g
t h e f i r s t r a c e and t h i r d h o r s e w i n n i n g t h e s e c o n d
race
P31 = Ph3 * Ph1 // p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g
t h e f i r s t r a c e and f i r s t h o r s e w i n n i n g t h e s e c o n d
race
P32 = Ph3 * Ph2 // p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g
t h e f i r s t r a c e and s e c o n d h o r s e w i n n i n g t h e
second race
P33 = Ph3 * Ph3 // p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g
both the r a c e s
disp ( P31 , ’ p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g t h e
f i r s t r a c e and f i r s t h o r s e w i n n i n g t h e s e c o n d
race i s ’)
5 S =[11 ,12 ,13 ,21 ,22 ,23 ,31 ,32 ,33]
6
7
8
9
10
11
12
13
14
15
Scilab code Exa 7.10 Repeated trials with two outcomes
33
// number o f t i m e s a f a i r c o i n i s t o s s e d and
g e t t i n g a heads i s a s u c c e s s
p =1/2; // p r o b a b i l i t y o f g e t t i n g a h e a d s
q =1/2 ; // p r o b a b i l i t y o f n o t g e t t i n g a h e a d s
P2 =( factorial (6) /( factorial (6 -2) * factorial (2) ) ) * p ^2*
q ^(6 -2) ;
disp ( P2 , ’ p r o b a b i l i t y o f g e t t i n g e x a c t l y two h e a d s ( i
. e k =2) ’ )
1 n =6;
2
3
4
5
6
7 P4 =( factorial (6) /( factorial (6 -4) * factorial (4) ) ) * p ^4*
8
9
10
11
q ^(6 -4) ; // p r o b a b i l t y o f g e t t i n g f o u r h e a d s
P5 =( factorial (6) /( factorial (6 -5) * factorial (5) ) ) * p ^5*
q ^(6 -5) ; // p r o b a b i l t y o f g e t t i n g f i v e h e a d s
P6 =( factorial (6) /( factorial (6 -6) * factorial (6) ) ) * p ^6*
q ^(6 -6) ; // p r o b a b i l t y o f g e t t i n g f i v e h e a d s
PA = P4 + P5 + P6 ;
disp ( PA , ’ p r o b a b i l i t y o f g e t t i n g a t l e a s t f o u r h e a d s ( i
. e k =4 ,5 o r 6 ) ’ )
12
13 Pn = q ^6
// p r o b a b i l i t y o f g e t t i n g no h e a d s
14 Pm =1 - Pn ;
15 disp ( Pm , ’ p r o b a b i l i t y o f g e t t i n g one o r more h e a d s ’ )
Scilab code Exa 7.12 Random variables
1
2
3
4
5
6
disp ( ”A box c o n t a i n s 12 i t e m s o f which t h r e e a r e
d e f e c t i v e ”)
disp ( ”A s a m p l e o f t h r e e i t e m s i s s e l e c t e d from t h e
box ” )
s = factorial (12) /( factorial (12 -3) * factorial (3) ) ;
disp (s , ’ number o f e l e m e n t s i n t h e s a m p l e s p a c e where
samples are of s i z e 3 ’ )
//X d e n o t e s t h e number o f d e f e c t i v e i t e m s i n t h e
sample
x =[0 ,1 ,2 ,3]; // r a n g e s p a c e o f t h e random v a r i a b l e X
34
Scilab code Exa 7.13 Probability distribution of a random variable
1 r =[1 ,2 ,3 ,4 ,5 ,6 ,5 ,4 ,3 ,2 ,1];
2 // number o f o u t c o m e s whose sum i s
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 0 , 1 1 , 1 2 r e s p e c t i v e l y such that
t h e r e i s o n l y 1 outcome i . e ( 1 , 1 ) whose sum i s 2 ,
two o u t c o m e s ( 1 , 2 ) and ( 2 , 1 ) whose sum i s 3 and
s o on
t =36;
// t o t a l number o f
elements in the sample space o f the experiment o f
tossing a pair of dice
for i =1:11;
p=r(i)/t;
disp ( p )
end
0.0277778
// p r o b a b i l i t y o f g e t t i n g a sum
of 2
0.0555556
// p r o b a b i l i t y o f g e t t i n g a
sum o f 3
0.0833333
// p r o b a b i l i t y o f g e t t i n g a sum
of 4
0.1111111
// p r o b a b i l i t y o f g e t t i n g a sum
of 5
0.1388889
// p r o b a b i l i t y o f g e t t i n g a sum
of 6
0.1666667
// p r o b a b i l i t y o f g e t t i n g a sum
of 7
0.1388889
// p r o b a b i l i t y o f g e t t i n g a sum
of 8
0.1111111
// p r o b a b i l i t y o f g e t t i n g a sum
of 9
0.0833333
// p r o b a b i l i t y o f g e t t i n g a sum
o f 10
0.0555556
// p r o b a b i l i t y o f g e t t i n g a sum
35
18
19
20
21
o f 11
0.0277778
// p r o b a b i l i t y o f g e t t i n g a sum
o f 12
x =[2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12];
// r a n g e s p a c e o f
random v a r i a b l e X which a s s i g n s t o e a c h p o i n t i n
s a m p l e s p a c e t h e sum o f t h e numbers
D =[ 2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12; 0.0277778 , 0.0555556
, 0.0833333 , 0.1111111 , 0.1388889 ,0.1666667 ,
0.1388889 ,0.1111111 , 0.0833333 , 0.0555556 ,
0.0277778];
disp (D , ’ d i s t r i b u t i o n t a b l e o f X where f i r s t row
g i v e s t h e r a n g e s p a c e and s e c o n d row g i v e s t h e
r e s p e c t i v e p r o b a b i l i t i e s i s as f o l l o w s ’ )
Scilab code Exa 7.14 Probability distribution of a random variable
1
2
3
4
5
6
7
disp ( ” a box c o n t a i n s 12 i t e m s o f which t h r e e a r e
d e f e c t i v e ”)
disp ( ”A s a m p l e o f t h r e e i t e m s i s s e l e c t e d from t h e
box ” )
r = factorial (9) /( factorial (9 -3) * factorial (3) ) //
number o f s a m p l e s o f s i z e 3 w i t h no d e f e c t i v e
items
t =220;
//
number o f d i f f e r e n t s a m p l e s o f s i z e 3 i . e t h e
number o f e l e m e n t s i n t h e s a m p l e s p a c e
P0 = r / t
//
p r o b a b i l i t y o f g e t t i n g no d e f e c t i v e i t e m
r1 =3*( factorial (9) /( factorial (9 -2) * factorial (2) ) )
// number o f s a m p l e s o f s i z e 3 g e t t i n g 1
d e f e c t i v e item
P1 = r1 / t
// p r o b a b i l i t y o f g e t t i n g 1 d e f e c t i v e i t e m
8 r2 =9*( factorial (3) /( factorial (3 -2) * factorial (2) ) )
36
// number o f s a m p l e s o f s i z e 3 g e t t i n g 2
d e f e c t i v e item
9 P2 = r2 / t
10
11
12
13
14
15
//
p r o b a b i l i t y o f g e t t i n g 2 d e f e c t i v e item
r3 =1;
// number o f s a m p l e s o f
s i z e 3 g e t t i n g 3 d e f e c t i v e item
P3 = r3 / t
// p r o b a b i l i t y o f g e t t i n g 3
d e f e c t i v e item
x =[0 ,1 ,2 ,3];
p =[ P0 , P1 , P2 , P3 ];
D =[0 ,1 ,2 ,3; P0 , P1 , P2 , P3 ];
disp (D , ’ d i s t r i b u t i o n t a b l e f o r random v a r i a b l e X t h e
u p p e r row b e i n g v a l u e s o f X ’ )
Scilab code Exa 7.15 Expectation of a random variable
1 disp ( ”A f a i r c o i n i s t o s s e d s i x t i m e s ” ) ;
2 x =[0 ,1 ,2 ,3 ,4 ,5 ,6];
// number o f h e a d s which can
3
4
5
6
7
8
9
10
occur
p =[1/64 ,6/64 ,15/64 ,20/64 ,15/64 ,6/64 ,1/64]; //
p r o b a b i l i t y o f o c c u r r i n g o f h e a d s where 1 / 6 4 i s
p r o b a b i l i t y f o r o c c u r r e n c e o f a s i n g l e head , 6 / 6 4
t h a t o f o c c u r r e n c e o f two h e a d s and s o on .
r =0;
for i =1:7;
r = r + (x(i)*p(i));
end
disp (r , ’ mean o r e x p e c t e d number o f h e a d s a r e ’ )
disp ( ”X i s a random v a r i a b l e which g i v e s p o s s i b l e
number o f d e f e c t i v e i t e m s i n a s a m p l e o f s i z e 3 ” )
;
11 // Box c o n t a i n s 12 i t e m s o f which t h r e e a r e d e f e c t i v e
12 x =[0 ,1 ,2 ,3];
// p o s s i b l e number o f d e f e c t i v e i t e m s
37
13
14
15
16
17
18
in a smaple o f s i z e 3
p =[84/220 ,108/220 ,27/220 ,1/220]; // p r o b a b i l i t y o f
o c c u r r e n c e o f e a c h number i n x r e s p e c t i v e l y where
8 4 / 2 2 0 i s t h e p r o b a b i l i t y f o r g e t t i n g no
d e f e c t i v e item , 1 0 8 / 2 2 0 i s t h a t o f g e t t i n g 1
d e f e c t i v e i t e m and s o on .
r =0;
for i =1:4;
r = r + (x(i)*p(i));
end
disp (r , ’ e x p e c t e d number o f d e f e c t i v e i t e m s i n a
sample o f s i z e 3 are ’ )
19
20 Ph1 =1/2;
21
22
23
24
25
26
27
28
// p r o b a b i l i t y o f w i n n i n g t h e r a c e by
f i r s t horse
Ph2 =1/3;
// p r o b a b i l i t y o f w i n n i n g t h e r a c e by
second horse
Ph3 =1/6;
// p r o b a b i l i t y o f w i n n i n g t h e r a c e by
third horse
//X i s t h e p a y o f f f u n c t i o n f o r t h e w i n n i n g h o r s e
X1 =2;
//X p a y s $2 a s f i r s t h o r s e w i n s t h e r a c
X2 =6;
//X p a y s $6 a s s e c o n d h o r s e w i n s t h e r a c e
X3 =9;
//X p a y s $9 a s t h i r d h o r s e w i n s t h e r a c e
E = X1 * Ph1 + X2 * Ph2 + X3 * Ph3 ;
disp (E , ’ e x p e c t e d pay o f f f o r t h e r a c e i s ’ )
Scilab code Exa 7.16 Variance and standard deviation of a random variable
// mean o f d i s t r i b u t i o n o f random
1 u =3;
variable X
// v a l u e s o f X i n t h e
d i s t r i b u t i o n a s x where i t i s t h e number o f t i m e s
h e a d s o c c u r s when a c o i n i s t o s s e d s i x t i m e s
3 p =[1/64 ,6/64 ,15/64 ,20/64 ,15/64 ,6/64 ,1/64];
//
2 x =[0 ,1 ,2 ,3 ,4 ,5 ,6];
38
p r o b a b i l i t i e s o f o c c u r r e n c e o f each value o f X ( x
) i n the d i s t r i b u t i o n such t h a t 1/64 g i v e s the
p r o b a b i l i t y o f o c c u r r e n c e o f no h e a d s a t a l l , 6 / 6 4
g i v e s t h a t o f o c c u r r e n c e o f h e a d s f o r o n l y one
t i m e and s o on
k =0;
for i =1:7;
k = k +(( x ( i ) -u ) ^2) * p ( i ) ;
end
disp (k , ’ V a r i a n c e o f X i s ’ )
s = sqrt ( k ) ;
disp (s , ’ S t a n d a r d d e v i a t i o n o f X i s ’ )
4
5
6
7
8
9
10
11
12 u =0.75;
// mean
13 x =[0 ,1 ,2 ,3];
// v a l u e s
14
15
16
17
18
19
20
21
22
o f random v a r i a b l e X a s x i n
the p r o b a b i l i t y d i s t r i b u t i o n of X
p =[84/220 ,108/220 ,27/220 ,1/220];
// p r o b a b i l i t y o f
v a l u e s i n x which a p p e a r i n d i s t r i b u t i o n t a b l e o f
X
g =0;
for i =1:4;
g = g +(( x ( i ) ) ^2) * p ( i ) ;
end
h =g -( u * u ) ;
disp (h , ’ v a r i a n c e o f X i s ’ )
sd = sqrt ( h ) ;
disp ( sd , ’ S t a n d a r d d e v i a t i o n f o r X ’ )
Scilab code Exa 7.17 Binomial diatribution
1 p =1/5;
// p r o b a b i l i t y o f t h e man h i t t i n g a t a r g e t
2 q =1 -1/5; // p r o b a b i l i t y o f t h e man n o t h i t t i n g t h e
target
3 n =100;
// number o f t i m e s t h e man f i r e s
4 e=n*p;
39
disp (e , ’ e x p e c t e d number o f t i m e s t h e man w i l l h i t
the t a r g e t ’ )
6 r = sqrt ( n * p * q ) ;
7 disp (r , ’ S t a n d a r d d e v i a t i o n ’ )
5
8
9 p =1/2;
// p r o b a b i l i t y o f g u e s s i n g t h e c o r r e c t a n s w e r
i n a f i v e q u e s t i o n t r u e − f a l s e exam
10 n =5;
// number o f q u e s t i o n s i n t h e exam
11 g = n * p ;
12 disp (g , ’ e x p e c t e d number o f c o r r e c t a n s w e r s i n t h e
exam ’ )
Scilab code Exa 7.18 Chebyshev inequality
1
2
3
4
5
6
7
8
9
10
11
12
u =75;
// mean o f a random v a r i a b l e X
n =5;
// s t a n d a r d d e v i a t i o n o f X
k =2;
// f o r k=2
l1 =u - k * n
l2 = u + k * n
P1 =1 -(1/ k ) ^2
disp ( ” t h u s t h e p r o b a b i l i t y t h a t a v a l u e o f X l i e s
b e t w e e n 65 and 85 i s a t l e a s t 0 . 7 5 a c c o r d i n g t o
Chebyshev i n e q u a l i t y ” )
k =3;
// f o r k=3
l1 =u - k * n
l2 = u + k * n
P2 =1 -(1/ k ) ^2
disp ( ” t h u s t h e p r o b a b i l i t y t h a t a v a l u e o f X l i e s
b e t w e e n 60 and 90 i s a t l e a s t 0 . 8 8 8 8 8 8 9 a c c o r d i n g
t o Chebyshev I n e q u a l i t y ” )
Scilab code Exa 7.19 Sample mean and Law of large numbers
40
1
2
3
4
5
6
7
8
disp ( ” a d i e i s t o s s e d 5 t i m e s w i t h t h e f o l l o w i n g
outcomes ”)
x1 =3;
x2 =4;
x3 =6;
x4 =1;
x5 =4;
xmean =( x1 + x2 + x3 + x4 + x5 ) /5 // mean o f t h e o u t c o m e s
disp ( ’ f o r a f a i r d i e t h e mean i s 3 . 5 . So law o f l a r g e
numbers t e l l s u s t h a t a s number o f o u t c o m e s
i n c r e a s e f o r t h i s experiment , there i s a g r e a t e r
l i k e l i h o o d t h a t themean w i l l g e t c l o s e r t o 3 . 5 ’ )
41
Chapter 8
Graph Theory
Scilab code Exa 8.1 Paths and connectivity
1
2
3
4
5
6
7
8
9
10
11
12
// r e f e r t o p a g e 8 . 6
disp ( ’ g i v e n a g r a p h w i t h 6 n o d e s v i z . node1 , node2
. . . . node6 ’ )
A =[0 1 0 1 1 0;1 0 1 0 1 0;0 1 0 0 0 1;1 0 0 0 0 0;1
1 0 0 0 0;0 0 1 0 0 0];
disp (A , ’ The a d j a c e n c y m a t r i x f o r A i s ’ )
disp ( ’ s e q u e n c e A i s a p a t h from node4 t o node6 ; but
i t i s n o t a t r a i l s i n c e t h e e d g e from node1 t o
node2 i s u s e d t w i c e ’ )
B =[0 0 0 1 1 0;0 0 0 0 1 1;0 0 0 0 0 0;1 0 0 0 0 0;1
1 0 0 0 0;0 1 0 0 0 0];
disp (B , ’ The a d j a c e n c y m a t r i x f o r B i s ’ )
disp ( ’ s e q u e n c e B i s n o t a p a t h s i n c e t h e r e i s no
e d g e from node2 t o node6 i s u s e d t w i c e ’ )
C =[0 0 0 1 1 0;0 0 1 0 1 0;0 1 0 0 1 0;1 0 0 0 0 0;1
1 1 0 0 1;0 0 0 0 1 0];
disp (C , ’ The a d j a c e n c y m a t r i x f o r C i s ’ )
disp ( ’ s e q u e n c e C i s a t r a i l s i n c e i s no e d g e i s u s e d
twice ’)
D =[0 0 0 1 1 0;0 0 0 0 0 0;0 0 0 0 1 1;1 0 0 0 0 0;1
0 1 0 0 0;0 0 1 0 0 0];
42
13
14
disp (D , ’ The a d j a c e n c y m a t r i x f o r D i s ’ )
disp ( ’ s e q u e n c e D i s a s i m p l e p a t h from node4 t o
node6 ’ )
Scilab code Exa 8.2 Minimum spanning tree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
disp ( ’ t o f i n d : m i n i m a l s p a n n i n g t r e e ’ )
disp ( ’ t h e a d j a c e n c y m a t r i x f o r t h e w e i g h t e d g r a p h (
nodeA , nodeB . . . nodeF ) o f 6 n o d e s i s : ’ )
K =[0 0 7 0 4 7;0 0 8 3 7 5;7 8 0 0 6 0;0 3 0 0 0 4;4
7 6 0 0 0;7 5 0 4 0 0]
disp ( ’ e d g e s o f t h e g r a p h ’ )
AC =7;
AE =4;
AF =7;
BC =8;
BD =3;
BE =7;
BF =5;
CE =6;
DF =4;
M =[ AC , AE , AF , BC , BD , BE , BF , CE , DF ]; // s e t o f a l l e d g e s
V = int32 ( M ) ;
L = gsort ( V ) // e d g e s s o r t e d i n d e c r e a s i n g o r d e r o f
their weights
disp ( ’ d e l e t i n g e d g e s w i t h o u t d i s c o n n e c t i n g t h e g r a p h
u n t i l 5 edges remain ’ )
N =[ BE , CE , AE , DF , BD ]; // e d g e s i n minimum s p a n n i n g
tree
Sum = sum ( N ) ;
disp ( Sum , ’ w e i g h t o f t h e m i n i m a l s p a n n i n g t r e e i s ’ )
disp ( ’ a n o t h e r method o f f i n d i n g a m i n i m a l s p a n n i n g
tree is : ’)
43
24 K = gsort (V , ’ g ’ , ’ i ’ )
// e d g e s s o r t e d i n i n c r e a s i n g
order
25 N2 =[ BD , AE , DF , CE , AF ];
26
27
// e d g e s i n minimum s p a n n i n g
tree
Sum2 = sum ( N2 ) ;
disp ( Sum2 , ’ w e i g h t o f t h e m i n i m a l s p a n n i n g t r e e i s ’ )
44
Chapter 9
Directed graphs
Scilab code Exa 9.6 Adjacency matrix
1 A =[0 0 0 1;1 0 1 1;1 0 0 1;1 0 1 0];
2 disp (A , ’ a d j a c e n c y m a t r i x o f g r a p h G i s ’ )
3 A2 = A ^2
4 A3 = A ^3
5 disp ( ’ t h e number o f o n e s i n A i s e q u a l t o t h e number
of edges
i n the graph i . e 8 ’ )
Scilab code Exa 9.8 Path matrix
1
2
3
4
5
6
7
8
9
A =[0 0 0 1;1 0 1 1;1 0 0 1;1 0 1 0];
disp (A , ’ a d j a c e n c y m a t r i x o f g r a p h G i s ’ )
A4 = A ^4;
A3 = A ^3;
A2 = A ^2;
B4 = A + A2 + A3 + A4 ;
B4 =[4 11 7 7 0 0 0 0 3 7 4 4 4 11 7 7];
for i =1:16
if ( B4 ( i ) ~=0) then
45
10 B4 ( i ) =1;
11 end
12 end
13 disp ( B4 , ’ R e p l a c i n g non z e r o
e n t r i e s o f B4 w i t h 1 , we
g e t path ( r e a c h a b i l i t y ) matrix P i s : ’ )
14 disp ( ’ t h e r e a r e z e r o e n t r i e s i n P , t h e r e f o r e t h e
graph i s not s t r o n g l y connected ’ )
46
Chapter 11
Properties of the integers
Scilab code Exa 11.2 Division algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
disp ( ’ D i v i s i o n A l g o r i t h m ’ )
a =4461; // d i v i d e n d
b =16;
// d i v i s o r
r = modulo (a , b ) // r e m a i n d e r
k = fix ( a / b )
// q u o t i e n t
j = b * k + r // d i v i d e n d= d i v i s o r ∗ q u o t i e n t+r e m a i n d e r
a = -262; // d i v i d e n d
b =3;
// d i v i s o r
k = fix ( a / b ) // r e m a i n d e r
r = modulo (a , b )
// q u o t i e n t
j = b * k + r // d i v i d e n d= d i v i s o r ∗ q u o t i e n t+r e m a i n d e r
disp ( ’ a and j have e q u a l v a l u e s . Hence d i v i s i o n
algorithm i s proved ’ )
Scilab code Exa 11.4 Primes
1
disp ( ’ D i v i s i b i l i t y and P r i m e s ’ )
47
2 x =50;
3 disp ( ’ p r i m e numbers l e s s t h a n 50 a r e ’ )
4 y = primes ( x )
5
6 disp ( ’ t h e p r i m e f a c t o r i s a t i o n o f 2 1 , 2 4 and 1 7 2 9
r e s p e c t i v e l y are : ’)
7 k = factor (21)
8 l = factor (24)
9 n = factor (1729)
Scilab code Exa 11.5 Greatest Common Divisor
1
2
3
4
5
6
7
8
9
10
11
disp ( ’ t h e GCD o f t h e f o l l o w i n g numbers i s : ’ )
V = int32 ([12 ,18]) ;
[ thegcd ]= gcd ( V )
V = int32 ([12 , -18]) ;
[ thegcd ]= gcd ( V )
V = int32 ([12 , -16]) ;
[ thegcd ]= gcd ( V )
V = int32 ([29 ,15]) ;
[ thegcd ]= gcd ( V )
V = int32 ([14 ,49]) ;
[ thegcd ]= gcd ( V )
Scilab code Exa 11.6 Euclidean algorithm
1
2
3
4
5
6
7
disp ( ’ E u c l i d e a n A l g o r i t h m ’ )
a =[540 ,168 ,36 ,24];
b =[168 ,36 ,24 ,12];
for i =1:4
V = int32 ([ a ( i ) ,b ( i ) ]) ;
thegcd =[];
thegcd ( i ) = gcd ( V ) ;
48
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
disp ( thegcd ( i ) )
end
function []= myf ( dividend , divisor )
quotient = floor ( dividend / divisor ) ;
rem = modulo ( dividend , divisor ) ;
k = quotient * divisor + rem ;
disp ( k )
if ( rem ~=0) then
myf ( divisor , rem )
end
endfunction
myf (540 ,168)
disp ( ’ f o r t h e e q u a t i o n 5 40 ∗ x +168∗ y =12 , we a r e g i v e n ’ )
a =540;
b =168;
c =24;
d =36;
d =a -3* b ;
// Eqn ( 1 )
c =b -4* d ;
// Eqn ( 2 )
k =d -1* c ;
// Eqn ( 3 )
5* d -1* b ;
// Eqn ( 4 )
k =d - b +4* d ;
// s u b s t i t u t i n g v a l u e o f c i n Eqn ( 3 )
from Eqn ( 2 )
r =5* a -16* b ;
if ( r == k ) then
disp ( ’ x=5 and y=16 ’ ) ;
end
Scilab code Exa 11.9 Fundamental theorem of Arithmetic
1 a =2^4*3^3*7*11*13
2 b =2^3*3^2*5^2*11*17
49
3 V = int32 ([ a , b ]) ;
4 [ d ]= gcd ( V )
5 lcm1 =2^4*3^3*5^2*7*11*13*17
// lcm i s t h e p r o d u c t
o f t h o s e p r i m e s which a p p e a r i n e i t h e r a o r b
Scilab code Exa 11.12 Congruence relation
1
2
3
4
5
6
7
8
9
x = poly (0 , ’ x ’ ) ;
g =3* x ^2 -7* x +5
m = horner (g ,2) // v a l u e o f p o l y n o m i a l a t 2
n = horner (g ,8) // v a l u e o f p o l y n o m i a l a t 8
j =m - n
disp (n , ” f o r n = ” )
if ( modulo (j ,6) ==0) then
mprintf ( ’ %i i s c o n g r u e n t t o %i ( mod 6 ) ’ ,m , n )
end
Scilab code Exa 11.19 Linear congruence equation
1
disp ( ’ s o l v i n g f o r t h e c o n g r u e n c e e q u a t i o n 8 x @ 1 2 (
mod 2 8 ) , where @ i s t h e s i g n f o r c o n g r u e n c e ’ )
a =8;
b =12;
m =28;
V = int32 ([ a , m ]) ;
d = gcd ( V ) ;
a1 = a / d ;
b1 = b / d ;
m1 = m / d ;
2
3
4
5
6
7
8
9
10
11 function yd = f ( x )
12 yd = ( a1 * x ) - b1
13 endfunction
50
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
disp ( ’ k i s t h e u n i q u e s o l u t i o n o f t h e e q u a t i o n ’ )
for i =0 : m1
x=i;
p=f(x);
if ( modulo (p , m1 ) == 0)
k=x
break ;
end
end
s1 = k ;
s2 = k + m1 ;
s3 = k +( m1 *2) ;
s4 = k +( m1 *3) ;
disp ( ’ s o l u t i o n s o f t h e o r i g i n a l e q u a t i o n a t d=4 ’ )
disp ( s1 )
disp ( s2 )
disp ( s3 )
disp ( s4 )
51
Chapter 12
Algebraic Systems
Scilab code Exa 12.4 Properties of operations
1 a =(8 -4) -3
2 b =8 -(4 -3)
3 disp ( ’ s i n c e a and b a r e n o t e q u a l s o s u b t r a c t i o n
non−c o m m u t a t i v e on Z ( s e t o f i n t e g e r s ) ’ )
4
5
6
7
8
9
a =[1 2;3 4]
b =[5 6;0 -2]
g= a*b
k= b*a
disp ( ’ s i n c e g and k a r e n o t e q u a l m a t r i x
m u l t i p l i c a t i o n i s non−c o m m u t a t i v e ’ )
10
11 h =(2^2) ^3
12 j =2^(2^3)
13 disp ( ’ s i n c e h and j
are not equal so e x p o n e n t i a l
o p e r a t i o n i s non a s s o c i a t i v e on t h e s e t o f
positive integers N’)
Scilab code Exa 12.17 Roots of polynomial
52
is
1
2
3
4
5
6
7
8
t = poly (0 , ’ t ’ ) ;
f = t ^3+ t ^2 -8* t +4
g = factors ( f )
disp ( r = roots ( f ) , ’ r o o t s o f f ( t ) a r e a s f o l l o w s : ’ )
t = poly (0 , ’ t ’ ) ;
h = t ^4 -2* t ^3+11* t -10
disp ( r = roots ( h ) , ’ t h e r e a l r o o t s o f h ( t ) a r e 1 and −2
’)
Scilab code Exa 12.18 Roots of polynomial
1 t = poly (0 , ’ t ’ ) ;
2 f = t ^4 -3* t ^3+6* t ^2+25* t -39
3 g = factors ( f )
4
disp ( r = roots ( f ) , ’ r o o t s o f
53
f ( t ) are as f o l l o w s : ’ )
Chapter 15
Boolean Algebra
Scilab code Exa 15.1 Basic definitions in boolean algebra
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 0 d e n o t e s F a l s e and 1 d e n o t e s t r u e
b =[0 ,1];
// b i n a r y o p e r a t i o n + on t h e s e t o f b i t s
for i =1:2
for j =1:2
k = b(i)& b(j);
disp ( k )
end
end
// b i n a r y o p e r a t i o n ∗ on t h e s e t o f b i t s
for i =1:2
for j =1:2
k = b(i)| b(j);
disp ( k )
end
end
// u n a r y o p e r a t i o n ’ on t h e s e t o f b i t s
k =~ b
clear ;
D =[1 ,2 ,5 ,7 ,10 ,14 ,35 ,70];
a =35;
54
22 b =70;
23 V = int32 ([ a , b ]) ;
24 thelcm = lcm ( V )
// a+b=lcm ( a , b )
25 V = int32 ([ a , b ])
26 thegcd = gcd ( V )
// a ∗b=gcd ( a , b )
27 abar =70/ a
// a ’=70/ a
Scilab code Exa 15.2 Boolean algebra as lattices
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
D =[1 ,2 ,5 ,7 ,10 ,14 ,35 ,70];
a = 2; // a and b b e l o n g t o D
b = 14;
V = int32 ([ a , b ]) ;
thelcm = lcm ( V )
V = int32 ([ a , b ]) ;
thegcd = gcd ( V )
abar =70/ a
bbar =70/ b
j =[ abar , b ];
h =[ a , bbar ];
V = int32 ([ j ])
lcm1 = lcm ( V )
K = int32 ([ h ])
lcm2 = lcm ( K )
55
Chapter 16
Recurrence relations
Scilab code Exa 16.14 Linear homogenous recurrence relations with constant coefficients
1 a =[];
2 a (1) =1;
// i n i t i a l c o n d i t i o n
3 a (2) =2;
// i n i t i a l c o n d i t i o n
4
disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n ) =5∗a ( n −1)−4∗a ( n
−2)+n ˆ2 ’ ) // t h i s i s a s e c o n d o r d e r r e c u r r e n c e
r e l a t i o n w i t h c o n s t a n t c o e f f i c i e n t s . I t i s non
homogenous b e c a u s e o f t h e n ˆ2
for n =3:4
a ( n ) =5* a (n -1) -4* a (n -2) + n ^2;
mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
end
5
6
7
8
9
10 a =[];
11 a (1) =1;
// i n i t i a l c o n d i t i o n
12 a (2) =2;
// i n i t i a l c o n d i t i o n
13 disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n ) =2∗a ( n −1) ∗ a ( n −2)+n
ˆ2 ’ )
// t h i s r e c u r r e n c e r e l a t i o n i s n o t l i n e a r
14 for n =3:4
15 a ( n ) =2* a (n -1) * a (n -2) + n ^2;
16 mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
56
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
end
a =[];
a (1) =1;
// i n i t i a l c o n d i t i o n
a (2) =2;
// i n i t i a l c o n d i t i o n
disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n )=n∗ a ( n −1)+3∗a ( n −2)
’)
// t h i s i s a homogenous l i n e a r s e c o n d o r d e r
recurrence r e l a t i o n without constant c o e f f i c i e n t s
b e c a u s e t h e c o e f f i c i e n t o f a [ n −1] i s n , n o t a
constant
for n =3:4
a ( n ) = n * a (n -1) +3* a (n -2) ;
mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
end
a =[];
a (1) =1;
// i n i t i a l c o n d i t i o n
a (2) =2;
// i n i t i a l c o n d i t i o n
a (3) =1;
// i n i t i a l c o n d i t i o n
disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n ) =2∗a ( n −1)+5∗a ( n −2)
−6∗a ( n −3) ’ ) // t h i s i s a homogenous l i n e a r t h i r d
order r e c u r r e n c e r e l a t i o n with constant
c o e f f i c i e n t s . Thus we n e e d t h r e e , n o t two , i n i t i a l
c o n d i t i o n s to y i e l d a unique s o l u t i o n of the
recurrence relation
for n =4:6
a ( n ) =2* a (n -1) +5* a (n -2) -6* a (n -3) ;
mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
end
Scilab code Exa 16.15 Solving linear homogenous recurrence relations with
constant coefficients
1
disp ( ’ r e c u r r e n c e r e l a t i o n o f F i b o n a c c i numbers f [ n ]=
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
f [ n−1]+ f [ n −2] ’ )
x = poly (0 , ’ x ’ ) ;
g = x ^2 -x -1;
disp (g , ’ c h a r a c t e r s t i c e q u a t i o n o f t h e r e c u r r e n c e
relation is : ’)
j =[];
j = roots ( g ) ;
disp (j , ’ r o o t s o f t h e c h a r a c t e r s t i c e q u a t i o n j 1 , j 2 ’ )
disp ( ’ f o r g e n e r a l e q u a t i o n f n=Ar ˆn+Br ˆn , v a l u e s o f
Aand B r e s p e c t i v e l y a r e c a l c u l a t e d a s : ’ )
disp ( ’ i n i t i a l c o n d i t i o n a t n=0 and n=1 r e s p e c t i v e l y
are : ’)
f1 =1;
f2 =1;
// p u t t i n g t h e v a l u e s o f f 1 and f 2 we g e t t h e
equations to s o l v e
D =[ 1.6180340 -0.618034;(1.6180340) ^2 ( -0.618034)
^2];
K =[1 1] ’;
c =[];
c=D\K;
A = c (1)
B = c (2)
disp ( ’ t h u s t h e s o l u t i o n i s f [ n
] = 0 . 4 4 7 2 1 3 6 ∗ ( ( 1 . 6 1 8 0 3 4 ) ˆn−(− 0 . 4 4 7 2 1 3 6 ) ˆ n ) ] ’ )
Scilab code Exa 16.16 Solving linear homogenous recurrence relations with
constant coefficients
1 disp ( ’ The r e c u r r e n c e r e l a t i o n t [ n ]=3 t [ n−1]+4 t [ n −2] ’ )
2 x = poly (0 , ’ x ’ ) ;
3 g = x ^2 -3* x -4;
4 disp (g , ’ c h a r a c t e r s t i c p o l y n o m i a l e q u a t i o n f o r t h e
above r e c u r r e n c e r e l a t i o n ’ )
58
5 j =[];
6 j = roots ( g ) ;
7 disp (j , ’ r o o t s
8 disp ( ’ g e n e r a l
9 disp ( ’ i n i t i a l
10
11
12
13
14
15
16
17
18
19
o f the c h a r a c t e r s t i c equation j1 , j 2 ’ )
s o l u t i o n t [ n ]= c 1 ∗( −1) ˆn+c 2 ∗4ˆ n ) ’ )
c o n d i t i o n a t n=0 and n=1 r e s p e c t i v e l y
are : ’)
t0 =0;
t1 =5;
// p u t t i n g t h e v a l u e s o f t 0 and t 1 we g e t t h e
equations to s o l v e
D =[1 1; -1 4]
K =[0 5] ’
c =[];
c=D\K;
c1 = c (1)
c2 = c (2)
disp ( ’ t h u s t h e s o l u t i o n i s t {n}=4ˆn−(−1) ˆ n ’ )
Scilab code Exa 16.17 Solving linear homogenous recurrence relations with
constant coefficients
1
2
3
4
5
6
7
8
9
10
11
disp ( ’ The r e c u r r e n c e r e l a t i o n t [ n ] = 4 ( t [ n−1]− t [ n − 2 ] ) ’
)
x = poly (0 , ’ x ’ ) ;
disp ( g = x ^2 -4* x +4 , ’ c h a r a c t e r s t i c p o l y n o m i a l e q u a t i o n
f o r the above r e c u r r e n c e r e l a t i o n ’ )
j =[];
j = roots ( g ) ;
disp (j , ’ r o o t s o f t h e c h a r a c t e r s t i c e q u a t i o n j 1 , j 2 ’ )
disp ( ’ t h e g e n e r a l s o l u t i o n i s t [ n ]= n ∗2ˆ n )
d i s p ( ’ initial condition at n =0 and n =1 respectively
are : ’ )
t 0 =1;
t 1 =1;
// p u t t i n g t h e v a l u e s o f t 0 and t 1 we g e t t h e
59
12
13
14
15
16
17
18
19
20
21
equations to s o l v e
D=[1 0 ; 2 2 ]
K=[1 1 ] ’
c = linsolve (D , K )
D =[1 0;2 2]
K =[1 1] ’
c =[];
c=D\K;
c1 = c (1)
c2 = c (2)
disp ( ’ t h u s t h e s o l u t i o n i s t {n}=2∗n−n ∗ 2 ˆ ( n−1) ’ )
Scilab code Exa 16.18 Solving linear homogenous recurrence relations with
constant coefficients
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
disp ( ’ The r e c u r r e n c e r e l a t i o n a [ n ]=2∗ a [ n−1]−3 a [ n −2] ’
)
x = poly (0 , ’ x ’ ) ;
disp ( g = x ^2 -2* x -3 , ’ c h a r a c t e r s t i c p o l y n o m i a l e q u a t i o n
f o r the above r e c u r r e n c e r e l a t i o n ’ )
j =[];
j = roots ( g ) ;
disp (j , ’ r o o t s o f t h e c h a r a c t e r s t i c e q u a t i o n j 1 , j 2 ’ )
disp ( ’ t h e g e n e r a l s o l u t i o n i s a [ n ]= c 1 ∗3ˆ n+c 2 ∗( −1) ˆ n ’
)
disp ( ’ i n i t i a l c o n d i t i o n a t n=0 and n=1 r e s p e c t i v e l y
are : ’)
// p u t t i n g t h e v a l u e s o f t 0 and t 1 we g e t t h e
equations to s o l v e
a0 =1;
a1 =2;
D =[1 1;3 -1]
K =[1 2] ’
c =[];
c=D\K;
60
16 c1 = c (1)
17 c2 = c (2)
18 disp ( ’ t h u s t h e
s o l u t i o n i s a [ n ]=0.75∗(3ˆ n ) +0.25∗(1ˆ n
) ’)
Scilab code Exa 16.19 Solving general homogenous linear recurrence relations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
disp ( ’ The r e c u r r e n c e r e l a t i o n a [ n ]=11∗ a [ n −1] −39∗ a [ n
−2]+45∗ a [ n −3] ’ )
x = poly (0 , ’ x ’ ) ;
disp ( g = x ^3 -11* x ^2+39* x -45 , ’ c h a r a c t e r s t i c p o l y n o m i a l
e q u a t i o n f o r the above r e c u r r e n c e r e l a t i o n ’ )
j =[];
j = roots ( g ) ;
disp (j , ’ r o o t s o f t h e c h a r a c t e r s t i c e q u a t i o n j 1 , j 2 ’ )
disp ( ’ h e n c e t h e g e n e r a l s o l u t i o n i s : a [ n ]= c 1 ∗ ( 3 ˆ n )+c 2
∗n ∗ ( 3 ˆ n )+c 3 ∗ ( 5 ˆ n ) ’ )
disp ( ’ i n i t i a l c o n d i t i o n a t n=0 and n=1 r e s p e c t i v e l y
are : ’)
// p u t t i n g t h e v a l u e s o f t 0 and t 1 we g e t t h e
equations to s o l v e
a0 =5;
a1 =11;
a2 =25;
D =[1 0 1;3 3 5;9 18 25];
K =[5 11 25] ’
c =[];
c=D\K;
c1 = c (1)
c2 = c (2)
c3 = c (3)
disp ( ’ t h u s t h e s o l u t i o n i s a [ n ]=(4 −2∗ n ) ∗ ( 3 ˆ n ) +5ˆn ’ )
61