title: Algorithm to solve an equation
body: <p>I have this problem for the course &quot;Algorithm and data structures&quot;</p>
  <blockquote>
  <p>You have a equation x^2+s(x)+200·x=N, where x and N are natural numbers and S(x) is the sum of digits of number x.</p>
  <p>On the input we have N and A, B such that  A≤B and A, B≤1,000,000,000. You need to check if there is a natural number x in the interval [A, B] that solves the equation. If found you need to return that number, otherwise return -1.</p>
  </blockquote>
  <pre><code>Example Input:
  
      1456
      10 80
  
  Output
      
      -1
  </code></pre>
  <p>I managed to solve this problem by using some math and a bit modified version of brute force algorithm. But are there any more effective(algorithm based) ways to solve this problem?</p>
  <p>This is my code:</p>
  <pre><code>import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.util.StringTokenizer;
  
  public class Range {
      
      static int proveri(long N, long A, long B) {
          long res = 0;
          long start = (long)((-200 + Math.sqrt(4*N + 4))/2);
          //System.out.println(start);
          for (long i = Math.max(A, start); i &lt;= B; i++) {
              res = i * i + S(i) + 200 * i;
              if(res == N)
                  return (int)i;
              if(res &gt; N)
                  return -1;
          }
          
          return -1;
      }
      
      static int S(long x) {
          int sum = 0;
          while(x &gt; 0) {
              sum += x % 10;
              x /= 10;
          }
          return sum;
      }
      
      public static void main(String[] args) throws Exception {
          int i,j,k;
          
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          
          long N = Long.parseLong(br.readLine());
          
          StringTokenizer st = new StringTokenizer(br.readLine());
          long A = Long.parseLong(st.nextToken());
          long B = Long.parseLong(st.nextToken());
          
          int res = proveri(N, A, B);
          System.out.println(res);
          
          br.close();
          
      }
      
  }
  </code></pre>