Given number of matches played , find number of teams in tournament | Function to return the number of teams ; To store both roots of the equation ; sqrt ( b ^ 2 - 4 ac ) ; First root ( - b + sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Second root ( - b - sqrt ( b ^ 2 - 4 ac ) ) / 2 a ; Return the positive root ; Driver code
function number_of_teams ( M ) { var N1 , N2 , sqr ; sqr = parseInt ( Math . sqrt ( 1 + ( 8 * M ) ) ) ; N1 = ( 1 + sqr ) / 2 ; N2 = ( 1 - sqr ) / 2 ; if ( N1 > 0 ) return N1 ; return N2 ; } var M = 45 ; document . write ( number_of_teams ( M ) ) ;