Maximum number of people that can be killed with strength P | Function to return the maximum number of people that can be killed ; Loop will break when the ith person cannot be killed ; Driver code
function maxPeople ( p ) { var tmp = 0 , count = 0 ; for ( var i = 1 ; i * i <= p ; i ++ ) { tmp = tmp + ( i * i ) ; if ( tmp <= p ) count ++ ; else break ; } return count ; } var p = 14 ; document . write ( maxPeople ( p ) ) ;