Java BigInteger Calculate sqrt(BigInteger n)

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

Description

Computes the square root of a BigInteger

License

BSD License

Parameter

Parameter Description
n The BigInteger to compute the sqrt from

Return

sqrt(n)

Declaration

public static final BigInteger sqrt(BigInteger n) 

Method Source Code


//package com.java2s;
/*/*from  w  w  w . j  a  v a 2  s  .  c  o  m*/
 * This file is part of an unofficial ISO20008-2.2 sample implementation to
 * evaluate certain schemes for their applicability on Android-based mobile
 * devices. The source is licensed under the modified 3-clause BSD license,
 * see the readme.
 * 
 * The code was published in conjunction with the publication called 
 * "Group Signatures on Mobile Devices: Practical Experiences" by
 * Potzmader, Winter, Hein, Hanser, Teufl and Chen
 */

import java.math.BigInteger;

public class Main {
    /** '1' as a {@link BigInteger} */
    public static final BigInteger ONE = BigInteger.ONE;
    /** '8' as a {@link BigInteger} */
    public static final BigInteger EIGHT = BigInteger.valueOf(8);

    /**
     * Computes the square root of a {@link BigInteger}
     * 
     * @param n The {@link BigInteger} to compute the sqrt from
     * 
     * @return sqrt(n)
     */
    public static final BigInteger sqrt(BigInteger n) {
        BigInteger a = BigInteger.ONE;
        BigInteger b = n.shiftRight(5).add(EIGHT);
        while (b.compareTo(a) >= 0) {
            BigInteger mid = a.add(b).shiftRight(1);
            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. split(BigInteger maxValue, int numberOfSplits)
  2. splitBigInt(BigInteger value, int n)
  3. splitFloat(BigInteger rawFloat, int signWidth, int exponentWidth, int mantissaWidth)
  4. sqrt(BigInteger n)
  5. sqrt(BigInteger n)
  6. square(BigInteger x)
  7. subArray(BigInteger[] input, int start, int end)
  8. substring(final String lhs, final BigInteger _start, final BigInteger _end)
  9. sum(BigInteger valueA, BigInteger valueB)