BigInteger sqrtAndRemainder() Method in Java with Examples
The java.math.BigInteger.sqrtAndRemainder() method performs an operation upon the current BigInteger by which this method is called. This method is used to calculate the integer square root (sqrt(this)) of this number and the remainder of this number with the square. It returns an array of two BigIntegers containing the integer square root ‘p’ of this and its remainder (this – p*p), respectively. BigInteger class internally uses an array of integers for processing, so the operation on an object of BigIntegers is not as fast as on primitives.
Note: This method is available since JDK 9
public BigInteger sqrtAndRemainder()
Parameters: This method accepts no parameter.
Return Value: This method returns an array of two BigIntegers with the integer square root at index 0 and the remainder at index 1.
Exception: The number must be positive otherwise ArithmeticException is thrown.
Below programs illustrate sqrtAndRemainder() method of BigInteger class
The square root of 15 is 3 and remainder is 6
The square root of 625 is 25 and remainder is 0
Program showing exception when value is negative.
java.lang.ArithmeticException: Negative BigInteger
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.