java.math.BigInteger.pow(int exponent)方法用於計算BigInteger的加冪,以其他形式傳遞給指數等於(this)的數字的冪exponent。此方法對當前BigInteger執行操作,通過該操作調用此方法並將指數作為參數傳遞。
用法:
public BigInteger pow(int exponent)
參數:此方法接受參數指數,該指數是應將BigInteger提升到的指數。返回:此方法返回一個BigInteger,它等於(this)exponent。
Exception:
參數指數必須為正數(指數> = 0),否則為ArithmeticException被拋出。
Examples:
Input: BigInteger1=321456, exponenet=5 Output: 3432477361331488865859403776 Explanation: BigInteger1.pow(exponent)=3432477361331488865859403776. 321456^5=3432477361331488865859403776 Input: BigInteger1=45321, exponenet=3 Output: 93089018611161 Explanation: BigInteger1.pow(exponent)=93089018611161. 321456^5=93089018611161
下麵的程序說明BigInteger類的pow()方法
範例1:
// Java program to demonstrate
// pow() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger b1;
b1 = new BigInteger("321456");
int exponent = 5;
// apply pow() method
BigInteger result = b1.pow(exponent);
// print result
System.out.println("Result of pow operation between BigInteger "
+ b1 + " and exponent " + exponent + " equal to " + result);
}
}
輸出:
Result of pow operation between BigInteger 321456 and exponent 5 equal
to 3432477361331488865859403776
範例2:
// Java program to demonstrate
// pow() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger b1;
b1 = new BigInteger("12346");
int exponent = 6;
// apply pow() method
BigInteger result = b1.pow(exponent);
// print result
System.out.println("Result of pow operation between BigInteger "
+ b1 + " and exponent " + exponent + " equal to " + result);
}
}
輸出:
Result of pow operation between BigInteger 41432345678 and exponent 6 equal to 5058679076487529899393537031261743031889730764186441745527485504
範例3:
程序在作為參數傳遞的指數小於零時顯示異常。
// Java program to demonstrate
// pow() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating BigInteger object
BigInteger b1;
b1 = new BigInteger("76543");
int exponent = -17;
try {
// apply pow() method
BigInteger result = b1.pow(exponent);
// print result
System.out.println("Result of pow operation between " + b1
+ " and " + exponent + " equal to " + result);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
java.lang.ArithmeticException: Negative exponent
參考:https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#pow(int)
相關用法
- Java BigInteger or()用法及代碼示例
- Java BigInteger and()用法及代碼示例
- Java BigInteger not()用法及代碼示例
- Java BigInteger xor()用法及代碼示例
- Java BigInteger abs()用法及代碼示例
- Java BigInteger mod()用法及代碼示例
- Java BigInteger valueOf()用法及代碼示例
- Java BigInteger toByteArray()用法及代碼示例
- Java BigInteger longValue()用法及代碼示例
- Java BigInteger intValue()用法及代碼示例
- Java BigInteger floatValue()用法及代碼示例
- Java BigInteger doubleValue()用法及代碼示例
- Java BigInteger remainder()用法及代碼示例
- Java BigInteger toString()用法及代碼示例
- Java BigInteger add()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger pow() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。