{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 6: Counting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.14: Ordered_partitions.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "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", "disp(m,'number of ways nine toys can be divided between four children with the youngest son getting 3 toys and others getting 2 each')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.15: Unordered_partitions.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "p=12; //total number of students\n", "t=3; //number of teams or partition\n", "disp('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", "disp(m,'number of ways that 12 students can be partitioned into three teams so that each team consists of 4 students')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.16: Inclusion_exclusion_principle_revisited.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "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", "disp(S,'The number of integers in the set U, which are not divisible by 3,5 and 7 is')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.1: Sum_rule_principle.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "M=8; //number of male professors teaching calculus\n", "F=5; //number of female professors teaching calculus\n", "T=M+F ;\n", "disp(T,'number of ways a student can choose a calculus professor')\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=intersect(E,F); //event of getting an even and prime number \n", "H=length(E)+length(F)-length(G); \n", "disp(H,'event of getting an even or a prime number')\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=union(E,F); //event of choosing a number which is prime or even\n", "k=length(G); \n", "disp(k,'number of ways of choosing a number which is prime or even')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.2: Product_rule_principle.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "disp('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", "disp(k,'total number of license plates that can be printed')\n", "\n", "disp('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", "t=26; //total number of members in the organisation\n", "j=t*(t-1)*(t-2); \n", "disp(j,'number of ways to elect the three officers (president,secretary,treasurer')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.3: Factorial_notation.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "disp('To find: factorial of a 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", "\n", "k=8*7*factorial(6)/factorial(6);\n", "disp(k,'value of 8!/6! is:')\n", "j=12*11*10*factorial(9)/factorial(9);\n", "disp(j,'value of 12!/9! is:')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.4: Binomial_coefficients.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function [k]=func1(n,r) //calculating binomial coefficient\n", "k=factorial(n)/(factorial(r)*factorial(n-r));\n", "endfunction\n", "func1(8,2)\n", "func1(9,4)\n", "func1(12,5)\n", "func1(10,3)\n", "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", "disp(p,'value of 10C7 is')\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" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.5: Permutations.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "disp('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", "disp(k,'number of three-letter words possible')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.6: Permutations_with_repetitions.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function [k]= funct1(n,p,q)\n", "k= factorial(n)/(factorial(p)*factorial(q));\n", "endfunction\n", "k=funct1(7,3,2) //in 'BENZENE' three letters are alike(the three Es) and two are alike (the two Ns)\n", "disp(k,'The number of seven-letter words that can be formed using letters of the word BENZENE')\n", "\n", "disp('a set of 4 indistinguishable red coloured flags, 3 indistinguishable white flags and a blue flag is given')\n", "j=funct1(8,4,3); \n", "disp(j,'number of different signals ,each consisting of eight flags')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.7: Combinations.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "disp('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", "disp(combinations,'number of combinations of the four objects given')\n", "k=factorial(3); //number of permutations of objects in a combination\n", "permutations = combinations*k;\n", "disp(permutations,'total number of permuatations for the problem')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.8: Combinations.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "function [k]= myfunc(n,r)\n", "k=factorial(n)/(factorial(n-r)*factorial(r));\n", "endfunction\n", "k=myfunc(8,3); \n", "disp(k,'the number of committees of three that can be formed out of eight people')\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", "disp(p,'total number of ways that a farmer can choose all these animals')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 6.9: Combinations_with_repetitions.sci" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//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", "disp(m,'number of non negative integer solutions of the given equation x+y+z=18')" ] } ], "metadata": { "kernelspec": { "display_name": "Scilab", "language": "scilab", "name": "scilab" }, "language_info": { "file_extension": ".sce", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-octave", "name": "scilab", "version": "0.7.1" } }, "nbformat": 4, "nbformat_minor": 0 }