Probability of distributing M items among X bags such that first bag contains N items | Function to find factorial of a number ; Function to find nCr ; Function to find probability of first bag to contain N items such that M items are distributed among X bags ; Driver code ; Function call
function factorial ( n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } function nCr ( n , r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } function Probability ( M , N , X ) { return parseFloat ( nCr ( M - N - 1 , X - 2 ) / ( nCr ( M - 1 , X - 1 ) * 1.0 ) ) ; } let M = 9 , X = 3 , N = 4 ; document . write ( Probability ( M , N , X ) . toFixed ( 6 ) ) ;