The java.math.BigInteger class provides a set of mathematical operations for integers that are too large to be represented by primitive data types. One of these operations is the modPow method, which calculates the modular exponentiation of a BigInteger.
Here are some code examples that demonstrate the usage of modPow to perform modular exponentiation:
// calculate 3^1234 mod 5 BigInteger base = BigInteger.valueOf(3); BigInteger exponent = BigInteger.valueOf(1234); BigInteger modulus = BigInteger.valueOf(5); BigInteger result = base.modPow(exponent, modulus); System.out.println(result); // output: 4
// calculate (2^35 + 1)^100 mod 99 BigInteger base = BigInteger.valueOf(2).pow(35).add(BigInteger.ONE); BigInteger exponent = BigInteger.valueOf(100); BigInteger modulus = BigInteger.valueOf(99); BigInteger result = base.modPow(exponent, modulus); System.out.println(result); // output: 49
These examples show how to use modPow to calculate large modular exponentiations. The first example calculates 3^1234 mod 5, which is equivalent to raising 3 to the 1234th power and taking the remainder when divided by 5. The second example calculates (2^35 + 1)^100 mod 99 using the same formula.
The modPow method is part of the java.math package library, which provides additional mathematical operations for working with large numbers.
Java BigInteger.modPow - 30 examples found. These are the top rated real world Java examples of java.math.BigInteger.modPow extracted from open source projects. You can rate examples to help us improve the quality of examples.