BigInteger sqrt() in Java

sqrt(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.sqrt()

This method returns the integer square root of this BigInteger. The integer square root of the corresponding mathematical integer n is the largest mathematical integer s such that s*s <= n. It is equal to the value of floor(sqrt(n)), where sqrt(n) denotes the real square root of n treated as a real.

Returns: the integer square root of this.

Throws: ArithmeticException - if this is negative. (The square root of a negative integer val is (i * sqrt(-val)) where i is the imaginary unit and is equal to sqrt(-1).)

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.sqrt() = > It returns 35.

Approach

Java

import java.math.BigInteger;

public class BigIntegersqrt {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.sqrt());
    }
}

Output:

35

No comments:

Post a Comment