Computing the nth root of p using BigDecimals

I am currently trying to solve this problem as described here:

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.

// Power of Cryptography 113

import java.util.Scanner;
import java.math.BigDecimal;
import java.math.RoundingMode;

// k can be 10^9 
// n <= 200
// p <= 10^101

class crypto { 
    public static void main(String[] args) {
        Scanner in = new Scanner(;
    while(in.hasNext()) {
        // Given two integers (n,p)
        // Find k such k^n = p
        int n = in.nextInt();
        BigDecimal p = in.nextBigDecimal();

    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) < 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)) > 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;

And here is the resulting error code:

Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(
at crypto.nthroot(
at crypto.nthroot(
at crypto.main(


That is expected if the resulting mathematical decimal number is non-terminating. The Javadocs for the 1-arg overload of divide state:


ArithmeticException - if the exact quotient does not have a terminating decimal expansion

Use another overload of the divide method to specify a scale (a cutoff) (and a RoundingMode).

Anybody here for a working code snippet? Here we go:

public final class RootCalculus {

  private static final int SCALE = 10;
  private static final int ROUNDING_MODE = BigDecimal.ROUND_HALF_DOWN;

  public static BigDecimal nthRoot(final int n, final BigDecimal a) {
      return nthRoot(n, a, BigDecimal.valueOf(.1).movePointLeft(SCALE));

  private static BigDecimal nthRoot(final int n, final BigDecimal a, final BigDecimal p) {
    if (a.compareTo(BigDecimal.ZERO) < 0) {
      throw new IllegalArgumentException("nth root can only be calculated for positive numbers");
    if (a.equals(BigDecimal.ZERO)) {
        return BigDecimal.ZERO;
    BigDecimal xPrev = a;
    BigDecimal x = a.divide(new BigDecimal(n), SCALE, ROUNDING_MODE);  // starting "guessed" value...
    while (x.subtract(xPrev).abs().compareTo(p) > 0) {
        xPrev = x;
        x = BigDecimal.valueOf(n - 1.0)
              .add(a.divide(x.pow(n - 1), SCALE, ROUNDING_MODE))
              .divide(new BigDecimal(n), SCALE, ROUNDING_MODE);
    return x;

  private RootCalculus() {

Just set SCALE to however precise you need the calculation to be.

Need Your Help

CSS3 Animation not working on Firefox But working on chrome

html css css3

I am trying to make my animation work on firefox, Its working fine on google chrome but not in firefox or any other browser.

JMeter: "Response code: 500, Response message: Internal Server Error" in Sampler Result

performance testing server jmeter response

I am extracting all dynamic values of my Website dynamically however, I can see "Response code: 500, Response message: Internal Server Error" in Sampler Result.