{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter6 - Counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex 6.1 Pg 133"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of ways a student can choose a calculus professor\n",
      "13\n",
      "event of getting an even or a prime number\n",
      "7\n",
      "number of ways of choosing a number which is prime or even\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "from numpy import intersect1d, union1d\n",
    "M=8#    #number of male professors teaching calculus\n",
    "F=5#    #number of female professors teaching calculus\n",
    "T=M+F #\n",
    "print 'number of ways a student can choose a calculus professor\\n',T\n",
    "\n",
    "E=[2,3,5,7]#   #event of choosing a prime number less than 10\n",
    "F=[2,4,6,8]#   #event of choosing an even number less than 10\n",
    "G=intersect1d(E,F)#  #event of getting an even and prime number \n",
    "H=len(E)+len(F)-len(G)#  \n",
    "print 'event of getting an even or a prime number\\n',H\n",
    "\n",
    "E=[11,13,17,19]# #event of choosing a prime number between 10 and 20\n",
    "F=[12,14,16,18]#  #event of choosing an even number between 10 and 20\n",
    "G=union1d(E,F)#    #event of choosing a number which is prime or even\n",
    "k=len(G)# \n",
    "print 'number of ways of choosing a number which is prime or even\\n',k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.2 Pg 133"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a license plate contains two letters followed by three digits where first digit can not be zero\n",
      "total number of license plates that can be printed\n",
      "608400\n",
      "\n",
      "\n",
      "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\n",
      "number of ways to elect the three officers (president,secretary,treasurer\n",
      "15600\n"
     ]
    }
   ],
   "source": [
    "print 'a license plate contains two letters followed by three digits where first digit can not be zero' \n",
    "n=26# #number of english letters\n",
    "n*n#  #number of ways of choosing two letters in the license plate\n",
    "p=10# #number of digits (0-9)\n",
    "(p-1)*p*p# #number of ways to select the three digits with the first digit not being zero\n",
    "k=n*n*(p-1)*p*p#\n",
    "print 'total number of license plates that can be printed\\n',k\n",
    "\n",
    "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'\n",
    "t=26#  #total number of members in the organisation\n",
    "j=t*(t-1)*(t-2)# \n",
    "print 'number of ways to elect the three officers (president,secretary,treasurer\\n',j\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.3 Pg 134"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "factorial of 6 : \n",
      "720\n",
      "value of 8!/6! is: 56.0\n",
      "value of 12!/9! is: 1320.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "print 'factorial of 6 : '\n",
    "facto2=2*1#\n",
    "facto3=3*facto2\n",
    "facto4=3*facto3\n",
    "facto4=4*facto3\n",
    "facto5=5*facto4\n",
    "facto6=6*facto5\n",
    "print facto6\n",
    "k=8*7*factorial(6)/factorial(6)#\n",
    "print 'value of 8!/6! is:',k\n",
    "j=12*11*10*factorial(9)/factorial(9)#\n",
    "print 'value of 12!/9! is:',j\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.4 Pg 135"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8C2 =  28.0\n",
      "9C4 =  126.0\n",
      "12C5 =  792.0\n",
      "10C3 =  120.0\n",
      "13C1 =  13.0\n",
      "value of 10C7 is 120.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "def func1(n,r):  #calculating binomial coefficient\n",
    "    k=factorial(n)/(factorial(r)*factorial(n-r))#\n",
    "    return k\n",
    "print \"8C2 = \",func1(8,2)\n",
    "print \"9C4 = \",func1(9,4)\n",
    "print \"12C5 = \",func1(12,5)\n",
    "print \"10C3 = \",func1(10,3)\n",
    "print \"13C1 = \",func1(13,1)\n",
    " \n",
    "p = factorial(10)/(factorial(10-7)*factorial(7))  #calculating 10C7\n",
    "q= factorial(10)/(factorial(10-3)*factorial(3))  #calculating 10C3\n",
    "print 'value of 10C7 is',p\n",
    "#10-7=3 so 10C7 can also be computed as 10C3\n",
    "#both p and q have same values but second method saves time and space\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.5 Pg 136"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "finding the number of three-letter words using only the given six letters(A,B,C,D,E,F) without repetition\n",
      "number of three-letter words  possible 120\n"
     ]
    }
   ],
   "source": [
    "print 'finding the number of three-letter words using only the given six letters(A,B,C,D,E,F) without repetition'\n",
    "n=6#  #total number of letters\n",
    "l1=n#  #number of ways in which first letter of the word can be chosen\n",
    "l2=n-1# #number of ways in which second letter of the word can be chosen\n",
    "l3= n-2# #number of ways in which third letter can be chosen\n",
    "k=l1*l2*l3#\n",
    "print 'number of three-letter words  possible',k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.6 Pg 137"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The number of seven-letter words that can be formed using letters of the word BENZENE =  420.0\n",
      "\n",
      "a set of 4 indistinguishable red coloured flags, 3 indistinguishable white flags and a blue flag is given\n",
      "\n",
      "number of different signals ,each consisting of eight flags =  280.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "def funct1(n,p,q):\n",
    "    k= factorial(n)/(factorial(p)*factorial(q))#\n",
    "    return k\n",
    "k=funct1(7,3,2)  #in \"BENZENE\" three letters are alike(the three Es) and two are alike (the two Ns)\n",
    "print 'The number of seven-letter words that can be formed using letters of the word BENZENE = ',k\n",
    "\n",
    "print '\\na set of 4 indistinguishable red coloured flags, 3 indistinguishable white flags and a blue flag is given'\n",
    "j=funct1(8,4,3)\n",
    "print '\\nnumber of different signals ,each consisting of eight flags = ',j\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.7 Pg 138"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "four objects are given (a,b,c,d) and three are taken at a time\n",
      "number of combinations of the four objects given =  4.0\n",
      "total number of permuatations for the problem =  24.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "print 'four objects are given (a,b,c,d) and three are taken at a time'  \n",
    "combinations = factorial(4)/(factorial(4-3)*factorial(3))#\n",
    "print 'number of combinations of the four objects given = ',combinations\n",
    "k=factorial(3)#  #number of permutations of objects in a combination\n",
    "permutations = combinations*k#\n",
    "print 'total number of permuatations for the problem = ',permutations\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.8 Pg 138"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the number of committees of three that can be formed out of eight people =  56.0\n",
      "total number of ways that a farmer can choose all these animals =  14000.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "def myfunc(n,r):\n",
    "    k=factorial(n)/(factorial(n-r)*factorial(r))#\n",
    "    return k\n",
    "k=myfunc(8,3)\n",
    "print'the number of committees of three that can be formed out of eight people = ', k\n",
    "  \n",
    "cows=myfunc(6,3)  #number of ways that a farmer can choose 3 cows out of 6 cows\n",
    "pigs=myfunc(5,2)  #number of ways that a farmer can choose 2 pigs out of 5 pigs\n",
    "hens=myfunc(8,4)  #number of ways that a farmer can choose 4 hens out of 8 hens\n",
    "p=cows*pigs*hens#       \n",
    "print 'total number of ways that a farmer can choose all these animals = ',p\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.9 Pg 139"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of non negative integer solutions of the given equation x+y+z=18 =  190.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "#each solution  to the equation can be viewed as a combination of objects\n",
    "r=18#  #number of objects \n",
    "M=3#   #kinds of object\n",
    "m=factorial(r+(M-1))/(factorial(r+(M-1)-(M-1))*factorial(M-1))#\n",
    "print 'number of non negative integer solutions of the given equation x+y+z=18 = ',m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.14 Pg 145"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "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\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "c1=3# #number of toys that the youngest child should get\n",
    "c2=2#  #number of toys that  the third child should get\n",
    "c3=2#  #number of toys that the second child should get\n",
    "c4=2#   #number of toys that the eldest son should get\n",
    "m=factorial(9)/(factorial(3)*factorial(2)*factorial(2)*factorial(2))#\n",
    "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"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.15 Pg 146"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "each partition of the students can be arranged in 3! ways as an ordered partition\n",
      "number of ways that 12 students can be partitioned into three teams so that each team consists of 4 students 5775.0\n"
     ]
    }
   ],
   "source": [
    "from scipy.misc import factorial\n",
    "p=12#  #total number of students\n",
    "t=3#  #number of teams or partition\n",
    "print 'each partition of the students can be arranged in 3! ways as an ordered partition'\n",
    "r=factorial(12)/(factorial(4)*factorial(4)*factorial(4))  #number of ordered partitions\n",
    "m=r/factorial(t)#  #number of unordered partitions\n",
    "print 'number of ways that 12 students can be partitioned into three teams so that each team consists of 4 students',m"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ex6.16 Pg 147"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The number of integers in the set U, which are not divisible by 3,5 and 7 is 457.0\n"
     ]
    }
   ],
   "source": [
    "from numpy import floor\n",
    "U=1000#  #number of elements in the set of positive integers not exceeding 1000\n",
    "A=U/3#   #number of elements in the subset of integers divisible by 3\n",
    "B=U/5#   #number of elements in the subset of integers divisible by 5\n",
    "C=U/7#   #number of elements in the subset of integers divisible by 7\n",
    "AandB=floor(U/(3*5))   #number of elements in the subset containing numbers divisible by both 3 and 5\n",
    "AandC=floor(U/(3*7))   #number of elements in the subset containing numbers divisible by both 3 and 7\n",
    "BandC=floor(U/(5*7))   #number of elements in the subset containing numbers divisible by both 5 and 7\n",
    "AandBandC=floor(U/(3*5*7))  #number of elements in the subset containing numbers divisible by 3,5 and 7\n",
    "s=U-(A+B+C)+(AandB+AandC+BandC)-(AandBandC)#  # By inclusion-exclusion principle\n",
    "S=round(s)#\n",
    "print 'The number of integers in the set U, which are not divisible by 3,5 and 7 is',S"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
