title: Computing the nth root of p using BigDecimals
body: <p>I am currently trying to solve this problem as described here:</p>
  
  <p><a href="http://uva.onlinejudge.org/external/1/113.pdf" rel="nofollow noreferrer">http://uva.onlinejudge.org/external/1/113.pdf</a></p>
  
  <p>The plan was to implement a recursive function to derive the solution. Some of the code here comes from Rosetta code for determining the nth root.</p>
  
  <pre><code>// Power of Cryptography 113
  
  import java.util.Scanner;
  import java.math.BigDecimal;
  import java.math.RoundingMode;
  
  // k can be 10^9 
  // n &lt;= 200
  // p &lt;= 10^101
  
  class crypto { 
      public static void main(String[] args) {
          Scanner in = new Scanner(System.in);
          while(in.hasNext()) {
              // Given two integers (n,p)
              // Find k such k^n = p
              int n = in.nextInt();
              BigDecimal p = in.nextBigDecimal();
              System.out.println(nthroot(n,p));
          }
      }
  
      public static BigDecimal nthroot(int n, BigDecimal A) {
          return nthroot(n, A, .001);
      }
  
      public static BigDecimal nthroot(int n, BigDecimal A, double p) {
          if(A.compareTo(BigDecimal.ZERO) &lt; 0) return new BigDecimal(-1);
          // we handle only real positive numbers
          else if(A.equals(BigDecimal.ZERO)) {
              return BigDecimal.ZERO;
          }
          BigDecimal x_prev = A;
          BigDecimal x = A.divide(new BigDecimal(n));  // starting "guessed" value...
          BigDecimal y = x.subtract(x_prev);
  
          while(y.abs().compareTo(new BigDecimal(p)) &gt; 0) {
              x_prev = x;
              BigDecimal temp = new BigDecimal(n-1.0);
              x = (x.multiply(temp).add(A).divide(x.pow(temp.intValue())).divide(new BigDecimal(n)));
          }
          return x;
      }
  }
  </code></pre>
  
  <p>And here is the resulting error code:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
  at java.math.BigDecimal.divide(BigDecimal.java:1616)
  at crypto.nthroot(crypto.java:38)
  at crypto.nthroot(crypto.java:24)
  at crypto.main(crypto.java:19)
  </code></pre>