Smallest root of the equation x ^ 2 + s ( x ) * x | Function to check if the sum of digits is equal to the summation assumed ; Calculate the sum of digit ; Function to find the largest root possible . ; Iterate for all possible sum of digits . ; Check if discriminent is a perfect square . ; Check if discriminent is a perfect square and if it as perefect root of the equation ; Function returns answer ; Driver code
function check ( a , b ) { var c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = parseInt ( a / 10 ) ; } return ( c == b ) ; } function root ( n ) { var found = false ; var mx = 1E18 ; for ( i = 0 ; i <= 90 ; i ++ ) { var s = i * i + 4 * n ; var sq = Math . sqrt ( s ) ; if ( sq * sq == s && check ( ( sq - i ) / 2 , i ) ) { found = true ; mx = Math . min ( mx , ( sq - i ) / 2 ) ; } } if ( found ) return mx ; else return - 1 ; } var n = 110 ; document . write ( root ( n ) ) ;