Here you can find the source of sqrt(BigInteger n)
Parameter | Description |
---|---|
n | The BigInteger to compute the sqrt from |
public static final BigInteger sqrt(BigInteger n)
//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); } }