BigInteger sqrtAndRemainder() in Java

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

Syntax:

BigInteger[] java.math.BigInteger.sqrtAndRemainder()

This method returns an array of two BigIntegers containing the integer square root s of this and its remainder this - s*s, respectively. 

Returns: An array of two  BigIntegers with the integer square root at offset 0 and the remainder at offset 1.

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.sqrtAndRemainder() = > It returns {35,9}.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");

        BigInteger[] arr = bigInteger.sqrtAndRemainder();

        System.out.println(arr[0] + " " + arr[1]);
    }
}

Output:

35 9

No comments:

Post a Comment