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
< ? php function check ( $ a , $ b ) { $ c = 0 ; while ( $ a != 0 ) { $ c = $ c + $ a % 10 ; $ a = ( int ) ( $ a / 10 ) ; } return ( $ c == $ b ) ? true : false ; } function root ( $ n ) { $ found = false ; $ mx = 1000000000000000001 ; for ( $ i = 0 ; $ i <= 90 ; $ i ++ ) { $ s = $ i * $ i + 4 * $ n ; $ sq = ( int ) ( sqrt ( $ s ) ) ; if ( $ sq * $ sq == $ s && check ( ( int ) ( ( $ sq - $ i ) / 2 ) , $ i ) ) { $ found = true ; $ mx = min ( $ mx , ( int ) ( ( $ sq - $ i ) / 2 ) ) ; } } if ( $ found ) return $ mx ; else return -1 ; } $ n = 110 ; echo root ( $ n ) ; ? >