當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。