HomeLanguagesJavaBigInteger sqrtAndRemainder() Method in Java with Examples

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–

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32738 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7124 POSTS0 COMMENTS
Nicole Veronica
12254 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12337 POSTS0 COMMENTS
Shaida Kate Naidoo
7234 POSTS0 COMMENTS
Ted Musemwa
7497 POSTS0 COMMENTS
Thapelo Manthata
7214 POSTS0 COMMENTS
Umr Jansen
7183 POSTS0 COMMENTS