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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

The square root of
15
is 3
and remainder is 6

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

The square root of
625
is 25
and remainder is 0

Example 3:
Program showing exception when value is negative.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

java.lang.ArithmeticException: Negative BigInteger

References: https://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#sqrtAndRemainder–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.