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
Syntax:
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
Example 1:
// Java program to demonstrate// sqrtAndRemainder() method of BigIntegerĀ Ā import java.math.BigInteger;Ā Ā class Main {Ā Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // BigInteger object to store resultĀ Ā Ā Ā Ā Ā Ā Ā BigInteger res[];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // For user inputĀ Ā Ā Ā Ā Ā Ā Ā // Use Scanner or BufferedReaderĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā // Two object of String createdĀ Ā Ā Ā Ā Ā Ā Ā // Holds the values to perform operationĀ Ā Ā Ā Ā Ā Ā Ā String input1 = "15";Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Convert the string input to BigIntegerĀ Ā Ā Ā Ā Ā Ā Ā BigInteger aĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new BigInteger(input1);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Using sqrtAndRemainder() methodĀ Ā Ā Ā Ā Ā Ā Ā try {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā res = a.sqrtAndRemainder();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Display the resultĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("The square root of\n"Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + a + "\nis " + res[0]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "\nand remainder is "Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + res[1]);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā catch (ArithmeticException e) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(e);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }} |
Output:
The square root of 15 is 3 and remainder is 6
Example 2:
// Java program to demonstrate// sqrtAndRemainder() method of BigIntegerĀ Ā import java.math.BigInteger;Ā Ā class Main {Ā Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // BigInteger object to store resultĀ Ā Ā Ā Ā Ā Ā Ā BigInteger res[];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // For user inputĀ Ā Ā Ā Ā Ā Ā Ā // Use Scanner or BufferedReaderĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā // Two object of String createdĀ Ā Ā Ā Ā Ā Ā Ā // Holds the values to perform operationĀ Ā Ā Ā Ā Ā Ā Ā String input1 = "625";Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Convert the string input to BigIntegerĀ Ā Ā Ā Ā Ā Ā Ā BigInteger aĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new BigInteger(input1);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Using sqrtAndRemainder() methodĀ Ā Ā Ā Ā Ā Ā Ā try {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā res = a.sqrtAndRemainder();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Display the resultĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("The square root of\n"Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + a + "\nis " + res[0]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "\nand remainder is "Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + res[1]);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā catch (ArithmeticException e) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(e);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }} |
Output:
The square root of 625 is 25 and remainder is 0
Example 3:
Program showing exception when value is negative.
// Java program to demonstrate// sqrtAndRemainder() method of BigIntegerĀ Ā import java.math.BigInteger;Ā Ā class Main {Ā Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // BigInteger object to store resultĀ Ā Ā Ā Ā Ā Ā Ā BigInteger res[];Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // For user inputĀ Ā Ā Ā Ā Ā Ā Ā // Use Scanner or BufferedReaderĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā // Two object of String createdĀ Ā Ā Ā Ā Ā Ā Ā // Holds the values to perform operationĀ Ā Ā Ā Ā Ā Ā Ā String input1 = "-9";Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Convert the string input to BigIntegerĀ Ā Ā Ā Ā Ā Ā Ā BigInteger aĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = new BigInteger(input1);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Using sqrtAndRemainder() methodĀ Ā Ā Ā Ā Ā Ā Ā try {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā res = a.sqrtAndRemainder();Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Display the resultĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println("The square root of\n"Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + a + "\nis " + res[0]Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + "\nand remainder is "Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + res[1]);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā catch (ArithmeticException e) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(e);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }} |
Output:
java.lang.ArithmeticException: Negative BigInteger
References: https://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#sqrtAndRemainderā



