title: How to figure out what the base case for a recursive Square root guessing program
body: <p>The problem I'm working on is this: </p>
  
  <blockquote>
    <p>The following method was known to the ancient Greeks for computing
    square roots. Given a value x > 0 and a guess g for the square root, a
    better guess is (g + x/g) / 2. Write a recursive helper method public
    static squareRootGuess(double x, double g). If g^2 is approximately
    equal to x, return g, otherwise, return squareRootGuess with the
    better guess. Then write a method public static squareRoot(double x)
    that uses the helper method.</p>
  </blockquote>
  
  <p>I'm not sure what I'm supposed to be doing here. I don't believe my base case is correct for this function. I don't have a good grasp on what exactly the problem is asking.</p>
  
  <pre><code>public class rootchecker {
  
      public static void main(String[] args) 
      {
          System.out.println(squareRoot(125));
          System.out.println(squareRoot(4));
      }
  
      // helper
      public static double squareRoot(double x)
      {
          return squareRootGuess(x, x-1);
      }
  
      // recursive
      public static double squareRootGuess(double x, double g)
      {
          if (x == 1)
          {
              return 1;
          }
          else
          {
              double ans = (g + (x/g) / 2);
              return squareRootGuess(x-1, ans);
          }
      }
  
  
  }
  </code></pre>
  
  <p><b>Update:</b></p>
  
  <p>I have fixed my base case. My code is now as follows:</p>
  
  <pre><code>public class rootchecker {
  
      public static void main(String[] args) 
      {
          System.out.println(squareRoot(125));
          System.out.println(squareRoot(4));
      }
  
      // helper
      public static double squareRoot(double x)
      {
          return squareRootGuess(x, x-1);
      }
  
      // recursive
      public static double squareRootGuess(double x, double g)
      {
          if (Math.abs(Math.sqrt(x) - g) &lt; 0.001)
          {
              return g;
          }
          else
          {
              double ans = (g + (x/g) / 2);
              return squareRootGuess(x, ans);
          }
      }
  
  
  }
  </code></pre>
  
  <p>Continuing on for the problem, is my logic correct for the else statement? I don't believe it is, considering I get a Stack Overflow Error.</p>