Find if given number is sum of first n natural numbers | Function to check if the s is the sum of first N natural number ; Solution of Quadratic Equation ; Condition to check if the solution is a integer ; Driver code ; Function call
function isvalid ( s ) { let k = ( - 1.0 + Math . sqrt ( 1 + 8 * s ) ) / 2 ; if ( Math . ceil ( k ) == Math . floor ( k ) ) return k ; else return - 1 ; } let s = 15 ; document . write ( isvalid ( s ) ) ;