📜  Java中的 BigInteger sqrtAndRemainder() 方法及示例

📅  最后修改于: 2022-05-13 01:54:23.754000             🧑  作者: Mango

Java中的 BigInteger sqrtAndRemainder() 方法及示例

Java.math.BigInteger.sqrtAndRemainder() 方法对调用此方法的当前 BigInteger 执行操作。此方法用于计算该数的整数平方根(sqrt(this)) 以及该数与平方的余数。它返回一个由两个 BigInteger 组成的数组,其中分别包含 this 的整数平方根“p”及其余数 (this – p*p)。 BigInteger 类内部使用整数数组进行处理,因此对 BigIntegers 对象的操作不如对原语的快。

注意:此方法从JDK 9开始可用

句法:

public BigInteger[] sqrtAndRemainder()

参数:此方法不接受任何参数。

返回值:此方法返回一个由两个 BigInteger 组成的数组,其中整数平方根在索引 0 处,余数在索引 1 处。

异常:该数字必须为正数,否则将引发 ArithmeticException。

下面的程序说明了 BigInteger 类的 sqrtAndRemainder() 方法

示例 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);
        }
    }
}

输出:

The square root of
15
is 3
and remainder is 6

示例 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);
        }
    }
}

输出:

The square root of
625
is 25
and remainder is 0

示例 3:
当值为负时程序显示异常。

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

输出:

java.lang.ArithmeticException: Negative BigInteger

参考资料: https: Java/docs/api/java/math/BigInteger.html#sqrtAndRemainder–