-1
import java.lang.*;
import java.math.*;

public class GFG {

    public static void main(String[] args) {

        // Creating a BigInteger object
        BigInteger big;

        big = new BigInteger("31739");

        BigInteger res  =big.sqrt();

        // print result
        System.out.println("Square root value of BigInteger " + big
        + " is " + res);
    }
}

Cannot find symbol sqrt() in BigInteger Please help me!!!!!

enter image description here

daedsidog
  • 1,732
  • 2
  • 17
  • 36
  • 2
    The above code will run for Java 9. Check Java9 doc for sqrt() method : https://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#sqrt-- – Abhishek Dec 22 '18 at 07:56
  • None of the answers seems to know the extremely fast (especially for BigIntegers with of 256 bits or larger) algorithm 1.12 from Brent and Zimmermann's ["Modern Computer Arithmetic"](https://members.loria.fr/PZimmermann/mca/mca-cup-0.5.9.pdf). Try it. I implemented it in my Delphi BigInteger library and it is much faster than anything shown here. – Rudy Velthuis Dec 23 '18 at 00:06

1 Answers1

0

This example works unde Java 9 or higher. For previous versions, you have to implement it yourse:

public static BigInteger sqrt(BigInteger val) {
    BigInteger half = BigInteger.ZERO.setBit(val.bitLength() / 2);
    BigInteger cur = half;

    while (true) {
        BigInteger tmp = half.add(val.divide(half)).shiftRight(1);

        if (tmp.equals(half) || tmp.equals(cur))
            return tmp;

        cur = half;
        half = tmp;
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35