# Chapter6 - Counting¶

## Ex 6.1 Pg 133¶

In [2]:
from numpy import intersect1d, union1d
M=8#    #number of male professors teaching calculus
F=5#    #number of female professors teaching calculus
T=M+F #
print 'number of ways a student can choose a calculus professor\n',T

E=[2,3,5,7]#   #event of choosing a prime number less than 10
F=[2,4,6,8]#   #event of choosing an even number less than 10
G=intersect1d(E,F)#  #event of getting an even and prime number
H=len(E)+len(F)-len(G)#
print 'event of getting an even or a prime number\n',H

E=[11,13,17,19]# #event of choosing a prime number between 10 and 20
F=[12,14,16,18]#  #event of choosing an even number between 10 and 20
G=union1d(E,F)#    #event of choosing a number which is prime or even
k=len(G)#
print 'number of ways of choosing a number which is prime or even\n',k

number of ways a student can choose a calculus professor
13
event of getting an even or a prime number
7
number of ways of choosing a number which is prime or even
8


## Ex6.2 Pg 133¶

In [3]:
print 'a license plate contains two letters followed by three digits where first digit can not be zero'
n=26# #number of english letters
n*n#  #number of ways of choosing two letters in the license plate
p=10# #number of digits (0-9)
(p-1)*p*p# #number of ways to select the three digits with the first digit not being zero
k=n*n*(p-1)*p*p#
print 'total number of license plates that can be printed\n',k

print '\n\na president ,a secretary and a treasurer has to be elected in an orga-nisation of 26 members.No person is elected to more than one postion'
t=26#  #total number of members in the organisation
j=t*(t-1)*(t-2)#
print 'number of ways to elect the three officers (president,secretary,treasurer\n',j

a license plate contains two letters followed by three digits where first digit can not be zero
total number of license plates that can be printed
608400

a president ,a secretary and a treasurer has to be elected in an orga-nisation of 26 members.No person is elected to more than one postion
number of ways to elect the three officers (president,secretary,treasurer
15600


## Ex6.3 Pg 134¶

In [4]:
from scipy.misc import factorial
print 'factorial of 6 : '
facto2=2*1#
facto3=3*facto2
facto4=3*facto3
facto4=4*facto3
facto5=5*facto4
facto6=6*facto5
print facto6
k=8*7*factorial(6)/factorial(6)#
print 'value of 8!/6! is:',k
j=12*11*10*factorial(9)/factorial(9)#
print 'value of 12!/9! is:',j

factorial of 6 :
720
value of 8!/6! is: 56.0
value of 12!/9! is: 1320.0


## Ex6.4 Pg 135¶

In [5]:
from scipy.misc import factorial
def func1(n,r):  #calculating binomial coefficient
k=factorial(n)/(factorial(r)*factorial(n-r))#
return k
print "8C2 = ",func1(8,2)
print "9C4 = ",func1(9,4)
print "12C5 = ",func1(12,5)
print "10C3 = ",func1(10,3)
print "13C1 = ",func1(13,1)

p = factorial(10)/(factorial(10-7)*factorial(7))  #calculating 10C7
q= factorial(10)/(factorial(10-3)*factorial(3))  #calculating 10C3
print 'value of 10C7 is',p
#10-7=3 so 10C7 can also be computed as 10C3
#both p and q have same values but second method saves time and space

8C2 =  28.0
9C4 =  126.0
12C5 =  792.0
10C3 =  120.0
13C1 =  13.0
value of 10C7 is 120.0


## Ex6.5 Pg 136¶

In [6]:
print 'finding the number of three-letter words using only the given six letters(A,B,C,D,E,F) without repetition'
n=6#  #total number of letters
l1=n#  #number of ways in which first letter of the word can be chosen
l2=n-1# #number of ways in which second letter of the word can be chosen
l3= n-2# #number of ways in which third letter can be chosen
k=l1*l2*l3#
print 'number of three-letter words  possible',k

finding the number of three-letter words using only the given six letters(A,B,C,D,E,F) without repetition
number of three-letter words  possible 120


## Ex6.6 Pg 137¶

In [7]:
from scipy.misc import factorial
def funct1(n,p,q):
k= factorial(n)/(factorial(p)*factorial(q))#
return k
k=funct1(7,3,2)  #in "BENZENE" three letters are alike(the three Es) and two are alike (the two Ns)
print 'The number of seven-letter words that can be formed using letters of the word BENZENE = ',k

print '\na set of 4 indistinguishable red coloured flags, 3 indistinguishable white flags and a blue flag is given'
j=funct1(8,4,3)
print '\nnumber of different signals ,each consisting of eight flags = ',j

The number of seven-letter words that can be formed using letters of the word BENZENE =  420.0

a set of 4 indistinguishable red coloured flags, 3 indistinguishable white flags and a blue flag is given

number of different signals ,each consisting of eight flags =  280.0


## Ex6.7 Pg 138¶

In [8]:
from scipy.misc import factorial
print 'four objects are given (a,b,c,d) and three are taken at a time'
combinations = factorial(4)/(factorial(4-3)*factorial(3))#
print 'number of combinations of the four objects given = ',combinations
k=factorial(3)#  #number of permutations of objects in a combination
permutations = combinations*k#
print 'total number of permuatations for the problem = ',permutations

four objects are given (a,b,c,d) and three are taken at a time
number of combinations of the four objects given =  4.0
total number of permuatations for the problem =  24.0


## Ex6.8 Pg 138¶

In [9]:
from scipy.misc import factorial
def myfunc(n,r):
k=factorial(n)/(factorial(n-r)*factorial(r))#
return k
k=myfunc(8,3)
print'the number of committees of three that can be formed out of eight people = ', k

cows=myfunc(6,3)  #number of ways that a farmer can choose 3 cows out of 6 cows
pigs=myfunc(5,2)  #number of ways that a farmer can choose 2 pigs out of 5 pigs
hens=myfunc(8,4)  #number of ways that a farmer can choose 4 hens out of 8 hens
p=cows*pigs*hens#
print 'total number of ways that a farmer can choose all these animals = ',p

the number of committees of three that can be formed out of eight people =  56.0
total number of ways that a farmer can choose all these animals =  14000.0


## Ex6.9 Pg 139¶

In [10]:
from scipy.misc import factorial
#each solution  to the equation can be viewed as a combination of objects
r=18#  #number of objects
M=3#   #kinds of object
m=factorial(r+(M-1))/(factorial(r+(M-1)-(M-1))*factorial(M-1))#
print 'number of non negative integer solutions of the given equation x+y+z=18 = ',m

number of non negative integer solutions of the given equation x+y+z=18 =  190.0


## Ex6.14 Pg 145¶

In [11]:
from scipy.misc import factorial
c1=3# #number of toys that the youngest child should get
c2=2#  #number of toys that  the third child should get
c3=2#  #number of toys that the second child should get
c4=2#   #number of toys that the eldest son should get
m=factorial(9)/(factorial(3)*factorial(2)*factorial(2)*factorial(2))#
print 'number of ways nine toys can be divided between four children with the youngest son getting 3 toys and others getting 2 each',m

number of ways nine toys can be divided between four children with the youngest son getting 3 toys and others getting 2 each 7560.0


## Ex6.15 Pg 146¶

In [12]:
from scipy.misc import factorial
p=12#  #total number of students
t=3#  #number of teams or partition
print 'each partition of the students can be arranged in 3! ways as an ordered partition'
r=factorial(12)/(factorial(4)*factorial(4)*factorial(4))  #number of ordered partitions
m=r/factorial(t)#  #number of unordered partitions
print 'number of ways that 12 students can be partitioned into three teams so that each team consists of 4 students',m

each partition of the students can be arranged in 3! ways as an ordered partition
number of ways that 12 students can be partitioned into three teams so that each team consists of 4 students 5775.0


## Ex6.16 Pg 147¶

In [13]:
from numpy import floor
U=1000#  #number of elements in the set of positive integers not exceeding 1000
A=U/3#   #number of elements in the subset of integers divisible by 3
B=U/5#   #number of elements in the subset of integers divisible by 5
C=U/7#   #number of elements in the subset of integers divisible by 7
AandB=floor(U/(3*5))   #number of elements in the subset containing numbers divisible by both 3 and 5
AandC=floor(U/(3*7))   #number of elements in the subset containing numbers divisible by both 3 and 7
BandC=floor(U/(5*7))   #number of elements in the subset containing numbers divisible by both 5 and 7
AandBandC=floor(U/(3*5*7))  #number of elements in the subset containing numbers divisible by 3,5 and 7
s=U-(A+B+C)+(AandB+AandC+BandC)-(AandBandC)#  # By inclusion-exclusion principle
S=round(s)#
print 'The number of integers in the set U, which are not divisible by 3,5 and 7 is',S

The number of integers in the set U, which are not divisible by 3,5 and 7 is 457.0