BigInteger类modPow()方法 (BigInteger Class modPow() method)
modPow() method is available in java.math package.
modPow()方法在java.math包中可用。
modPow() method is used to calculate the mod pow by using the (this BigInteger) raised to the power of the first given argument (exp) mod the second given parameter [i.e. (this BigInteger) pow (exp) mod (BigDecimal val) ].
modPow()方法用于通过使用提高到第一个给定参数(exp)的幂的(this BigInteger)mod第二个给定参数[即(this BigInteger)pow(exp)mod(BigDecimal val)的幂来计算mod pow ]。
modPow() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
modPow()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
modPow() method may throw an exception at the time of returning mod pow.
返回mod pow时, modPow()方法可能会引发异常。
ArithmeticException: This exception may throw when the given parameter holds value 0 or negative.
ArithmeticException :当给定参数的值为0或负数时,可能引发此异常。
Syntax:
句法:
public BigInteger modPow(BigInteger exp, BigInteger val);
Parameter(s):
参数:
BigInteger exp – represents the object by which to calculate mod pow.
BigInteger exp –表示用于计算mod pow的对象。
BigInteger val – represents the exponent raised to the power of this BigInteger.
BigInteger val –表示此BigInteger的功能所得到的指数。
Return value:
返回值:
The return type of this method is BigInteger, it returns BigInteger that holds the value by using [ (this BigInteger) pow (exp) mod (BigInteger val) ].
该方法的返回类型为BigInteger ,它通过使用[(this BigInteger)pow(exp)mod(BigInteger val)]返回保存该值的BigInteger。
Example:
例:
// Java program to demonstrate the example
// of BigInteger modPow(BigInteger exp, BigInteger val)
// method of BigInteger
import java.math.*;
public class ModPowOfBI {
public static void main(String args[]) {
// Initialize three variables str, exp and mod
String str = "10";
String exp = "2";
String mod = "11";
// Initialize three BigInteger objects
BigInteger b_int1 = new BigInteger(str);
BigInteger b_int2 = new BigInteger(exp);
BigInteger b_int3 = new BigInteger(mod);
// Display b_int1 , b_int2 and b_int3
System.out.println("b_int1: " + b_int1);
System.out.println("b_int2: " + b_int2);
System.out.println("b_int3: " + b_int3);
// Here, we are calculating modPow between
// two BigInteger values and it is calculated
// by using[(this BigInteger b_int1 pow (b_int2))
// mod (BigInteger b_int3) ] like 10^2 mod 11
BigInteger modpow_val = b_int1.modPow(b_int2, b_int3);
System.out.println("b_int1.modPow(b_int2,b_int3): " + modpow_val);
}
}
Output
输出量
b_int1: 10
b_int2: 2
b_int3: 11
b_int1.modPow(b_int2,b_int3): 1
翻译自: https://www.includehelp.com/java/biginteger-modpow-method-with-example.aspx