Smallest root of the equation x^2 + s(x)*x – n = 0, where s(x) is the sum of digits of root x.

You are given an integer n, find the smallest positive integer root of equation x, or else print -1 if no roots are found.

Equation: x^2 + s(x)*x – n = 0

where x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system.
1 <= N <= 10^18



Examples:

Input: N = 110 
Output: 10 
Explanation: x = 10 is the minimum root. 
             As s(10) = 1 + 0 = 1 and 
             102 + 1*10 - 110=0.  

Input: N = 4
Output: -1 
Explanation: there are no roots of the 
equation possible

A naive approach will be to iterate through all the possible values of X and find out if any such root exists but this won’t be possible as the value of n is very large.

An efficient approach will be as follows
Firstly let’s find the interval of possible values of s(x). Hence x^2 <= N and N <= 10^18, x <= 109. In other words, for every considerable solution x the decimal length of x does not extend 10 digits. So Smax = s(9999999999) = 10*9 = 90.
Let's brute force the value of s(x) (0 <= s(x) <= 90). Now we have an ordinary square equation. The deal is to solve the equation and to check that the current brute forced value of s(x) is equal to sum of digits of the solution. If the solution exists and the equality holds, we should get the answer and store the minimum of the roots possible.

Below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find smallest value of root
// of an equation under given constraints.
#include <bits/stdc++.h>
using namespace std;
  
// function to check if the sum of digits is
// equal to the summation assumed
bool check(long long a, long long b)
{
    long long int c = 0;
  
    // calculate the sum of digit
    while (a != 0) {
        c = c + a % 10;
        a = a / 10;
    }
  
    return (c == b);
}
  
// function to find the largest root possible.
long long root(long long n)
{
    bool found = 0;
    long long mx = 1e18;
  
    // iterate for all possible sum of digits.
    for (long long i = 0; i <= 90; i++) {
  
        // check if discriminent is a perfect square.
        long long s = i * i + 4 * n;
        long long sq = sqrt(s);
  
        // check if discriminent is a perfect square and
        // if it as perefect root of the equation
        if (sq * sq == s && check((sq - i) / 2, i)) {
            found = 1;
            mx = min(mx, (sq - i) / 2);
        }
    }
  
    // function returns answer
    if (found)
        return mx;
    else
        return -1;
}
  
// driver program to check the above function
int main()
{
    long long n = 110;
    cout << root(n);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find smallest value of root
// of an equation under given constraints.
  
class GFG{
// function to check if the sum of digits is
// equal to the summation assumed
static boolean check(long a, long b)
{
    long c = 0;
  
    // calculate the sum of digit
    while (a != 0) {
        c = c + a % 10;
        a = a / 10;
    }
  
    return (c == b);
}
  
// function to find the largest root possible.
static long root(long n)
{
    boolean found = false;
    long mx = (long)1E18;
  
    // iterate for all possible sum of digits.
    for (long i = 0; i <= 90; i++) {
  
        // check if discriminent is a perfect square.
        long s = i * i + 4 * n;
        long sq = (long)Math.sqrt(s);
  
        // check if discriminent is a perfect square and
        // if it as perefect root of the equation
        if (sq * sq == s && check((sq - i) / 2, i)) {
            found = true;
            mx = Math.min(mx, (sq - i) / 2);
        }
    }
  
    // function returns answer
    if (found)
        return mx;
    else
        return -1;
}
  
// driver program to check the above function
public static void main(String[] args)
{
    long n = 110;
    System.out.println(root(n));
}
}
// This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find smallest 
# value of root of an equation
# under given constraints.
import math
  
# function to check if the sum 
# of digits is equal to the
# summation assumed
def check(a, b):
    c = 0;
  
    # calculate the
    # sum of digit
    while (a != 0):
        c = c + a % 10;
        a = int(a / 10);
  
    return True if(c == b) else False;
  
# function to find the
# largest root possible.
def root(n):
    found = False;
      
    # float(1E+18)
    mx = 1000000000000000001
  
    # iterate for all 
    # possible sum of digits.
    for i in range(91): 
  
        # check if discriminent
        # is a perfect square.
        s = i * i + 4 * n;
        sq = int(math.sqrt(s));
  
        # check if discriminent is 
        # a perfect square and
        # if it as perefect root 
        # of the equation
        if (sq * sq == s and 
            check(int((sq - i) / 2), i)): 
            found = True;
            mx = min(mx, int((sq-i) / 2));
  
    # function returns answer
    if (found):
        return mx;
    else:
        return -1;
  
# Driver Code
n = 110;
print(root(n));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//C# program to find smallest value of root 
// of an equation under given constraints. 
using System;
public class GFG{ 
    // function to check if the sum of digits is 
    // equal to the summation assumed 
    static bool check(long a, long b) 
    
        long c = 0; 
  
        // calculate the sum of digit 
        while (a != 0) { 
            c = c + a % 10; 
            a = a / 10; 
        
  
        return (c == b); 
    
  
    // function to find the largest root possible. 
    static long root(long n) 
    
        bool found = false
        long mx = (long)1E18; 
  
        // iterate for all possible sum of digits. 
        for (long i = 0; i <= 90; i++) { 
  
            // check if discriminent is a perfect square. 
            long s = i * i + 4 * n; 
            long sq = (long)Math.Sqrt(s); 
  
            // check if discriminent is a perfect square and 
            // if it as perefect root of the equation 
            if (sq * sq == s && check((sq - i) / 2, i)) { 
                found = true
                mx = Math.Min(mx, (sq - i) / 2); 
            
        
  
        // function returns answer 
        if (found) 
            return mx; 
        else
            return -1; 
    
  
    // driver program to check the above function 
    public static void Main() 
    
        long n = 110; 
        Console.Write(root(n)); 
    
// This code is contributed by Raput-Ji

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP 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
function check($a, $b)
{
    $c = 0;
  
    // calculate the
    // sum of digit
    while ($a != 0)
    {
        $c = $c + $a % 10;
        $a = (int)($a / 10);
    }
  
    return ($c == $b) ? true : false;
}
  
// function to find the
// largest root possible.
function root($n)
{
    $found = false;
      
    // float(1E+18)
    $mx = 1000000000000000001; 
  
    // iterate for all 
    // possible sum of digits.
    for ($i = 0; $i <= 90; $i++) 
    {
  
        // check if discriminent
        // is a perfect square.
        $s = $i * $i + 4 * $n;
        $sq = (int)(sqrt($s));
  
        // check if discriminent is 
        // a perfect square and
        // if it as perefect root 
        // of the equation
        if ($sq * $sq == $s && 
            check((int)(($sq - $i) / 2), $i)) 
        {
            $found = true;
            $mx = min($mx, (int)(($sq
                                  $i) / 2));
        }
    }
  
    // function returns answer
    if ($found)
        return $mx;
    else
        return -1;
}
  
// Driver Code
$n = 110;
echo root($n);
  
// This code is contributed by mits
?>

chevron_right



Output:

10 


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar, Rajput-Ji



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.