title: Recursive method for x^n optimised for when n is even
body: <p>I need to write a recursive method using Java called power that takes a double x and an integer n and that returns x^n.  Here is what I have so far.</p>
  
  <pre><code>public static double power(double x, int n) {
      if (n == 0)
          return 1;
      if (n == 1)
          return x;
      else
          return x * (power(x, n-1));
  
  }
  </code></pre>
  
  <p>This code works as expected.  However, I am trying to go the extra mile and perform the following optional exercise:  </p>
  
  <p>"Optional challenge: you can make this method more efficient, when n is even, using x^n = (x^(n/2))^2."</p>
  
  <p>I am not sure how to implement that last formula when n is even.  I do not think I can use recursion for that.  I have tried to implement the following, but it also does not work because I cannot take a double to the power of an int.</p>
  
  <pre><code>if (n%2 == 0)
          return (x^(n/2))^2;
  </code></pre>
  
  <p>Can somebody point me in the right direction?  I feel like I am missing something obvious.  All help appreciated.</p>