Smallest root of the equation x ^ 2 + s ( x ) * x | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( long long a , long long b ) { long long int c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = a / 10 ; } return ( c == b ) ; } long long root ( long long n ) { bool found = 0 ; long long mx = 1e18 ; for ( long long i = 0 ; i <= 90 ; i ++ ) { long long s = i * i + 4 * n ; long long sq = sqrt ( s ) ; if ( sq * sq == s && check ( ( sq - i ) / 2 , i ) ) { found = 1 ; mx = min ( mx , ( sq - i ) / 2 ) ; } } if ( found ) return mx ; else return -1 ; } int main ( ) { long long n = 110 ; cout << root ( n ) ; }