Smallest root of the equation x ^ 2 + s ( x ) * x | Java program to find smallest value of root of an equation under given constraints . ; 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 program t
class GFG { static boolean check ( long a , long b ) { long c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = a / 10 ; } return ( c == b ) ; } static long root ( long n ) { boolean found = false ; long mx = ( long ) 1E18 ; for ( long i = 0 ; i <= 90 ; i ++ ) { long s = i * i + 4 * n ; long sq = ( long ) 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 ; } public static void main ( String [ ] args ) { long n = 110 ; System . out . println ( root ( n ) ) ; } }