Java BigInteger Calculate sqrt(BigInteger n)

Here you can find the source of sqrt(BigInteger n)

Description

sqrt

License

Apache License

Declaration

public static BigInteger sqrt(BigInteger n) 

Method Source Code

//package com.java2s;
/**// ww w  . j  a v  a2 s  . c o  m
 * Copyright (c)2015-2016 https://github.com/javahuang/rp
 * <p/>
 * Licensed under Apache License,Version 1.0
 */

import java.math.BigInteger;

public class Main {

    public static BigInteger sqrt(BigInteger n) {
        BigInteger a = BigInteger.ONE;
        BigInteger b = new BigInteger(n.shiftRight(5).add(new BigInteger("8")).toString());
        while (b.compareTo(a) >= 0) {
            BigInteger mid = new BigInteger(a.add(b).shiftRight(1).toString());
            if (mid.multiply(mid).compareTo(n) > 0)
                b = mid.subtract(BigInteger.ONE);
            else
                a = mid.add(BigInteger.ONE);
        }
        return a.subtract(BigInteger.ONE);
    }
}

Related

  1. shiftLeft(final BigInteger register, final BigInteger input, final int n)
  2. split(BigInteger maxValue, int numberOfSplits)
  3. splitBigInt(BigInteger value, int n)
  4. splitFloat(BigInteger rawFloat, int signWidth, int exponentWidth, int mantissaWidth)
  5. sqrt(BigInteger n)
  6. sqrt(BigInteger n)
  7. square(BigInteger x)
  8. subArray(BigInteger[] input, int start, int end)
  9. substring(final String lhs, final BigInteger _start, final BigInteger _end)