Java BigInteger sqrtAndRemainder()方法及示例

Java BigInteger sqrtAndRemainder()方法及示例

java.math.BigInteger .sqrtAndRemainder()方法对被调用此方法的当前BigInteger进行操作。这个方法用于计算这个数字的整数平方根(sqrt(this))和这个数字的余数与平方。它返回一个包含两个BigIntegers的数组,分别包含这个数的整数平方根’p’和它的余数(this - p*p)。BigInteger类内部使用整数数组进行处理,所以对BigIntegers对象的操作不如对基数的操作快。

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

语法

public BigInteger[] sqrtAndRemainder()

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

返回值: 本方法返回一个由两个BigIntegers组成的数组,整数的平方根在索引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://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#sqrtAndRemainder-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程