当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BigInteger sqrtAndRemainder()用法及代码示例


java.math.BigInteger.sqrtAndRemainder()方法对当前的BigInteger执行操作,通过该操作调用此方法。此方法用于计算此数字的整数平方根(sqrt(this)),并使用平方计算该数字的余数。它返回一个由两个BigInteger组成的数组,分别包含this和它的余数(this – p * 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://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#sqrtAndRemainder–



相关用法


注:本文由纯净天空筛选整理自Rajnis09大神的英文原创作品 BigInteger sqrtAndRemainder() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。