📜  Java中的 BigInteger pow() 方法

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

Java中的 BigInteger pow() 方法

Java.math.BigInteger .pow(int exponent)方法用于计算 BigInteger 的乘方,作为指数传递的某个其他数字的幂,其值等于 (this) exponent 。此方法对调用此方法的当前 BigInteger 执行操作,并将指数作为参数传递。
句法:

public BigInteger pow(int exponent)

参数:此方法接受一个参数指数,它是这个 BigInteger 应该被提升到的指数。
回报:
此方法返回一个 BigInteger,它等于 (this) exponent
例外:
参数指数必须为正数(指数 >= 0),否则将引发ArithmeticException
例子:

Input: BigInteger1=321456, exponent=5
Output: 3432477361331488865859403776
Explanation: BigInteger1.pow(exponent)=3432477361331488865859403776. 
321456^5=3432477361331488865859403776

Input: BigInteger1=45321, exponent=3
Output: 93089018611161
Explanation: BigInteger1.pow(exponent)=93089018611161. 
321456^5=93089018611161

下面的程序说明了 BigInteger 类的 pow() 方法
示例 1:

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


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


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


输出:

示例 2:

Java

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

输出:

示例 3:
当作为参数传递的指数小于零时,程序显示异常。

Java

// 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 : Java ( Java)