Java Guava | pow(int b, int k) method of IntMath Class
The method pow(int b, int k) of Guava’s IntMath class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).intValue(). This implementation runs in O(log k) time.
Syntax:
public static int pow(int b, int k)
Exception: The method pow(int b, int k) throws IllegalArgumentException if k < 0.
Example 1:
// Java code to show implementation of // pow(int b, int k) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { static int findPow(int b, int k) { // Using pow(int b, int k) // method of Guava's IntMath class int ans = IntMath.pow(b, k); // Return the answer return ans; } // Driver code public static void main(String args[]) { int b = 4; int k = 5; int ans = findPow(b, k); System.out.println(b + " to the " + k + "th power is: " + ans); b = 12; k = 3; ans = findPow(b, k); System.out.println(b + " to the " + k + "th power is: " + ans); } } |
chevron_right
filter_none
Output:
4 to the 5th power is: 1024 12 to the 3th power is: 1728
Example 2 :
// Java code to show implementation of // pow(int b, int k) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { static int findPow(int b, int k) { try { // Using pow(int b, int k) // method of Guava's IntMath class // This should throw "IllegalArgumentException" // as k < 0 int ans = IntMath.pow(b, k); // Return the answer return ans; } catch (Exception e) { System.out.println(e); return -1; } } // Driver code public static void main(String args[]) { int b = 4; int k = -5; try { // Using pow(int b, int k) // method of Guava's IntMath class // This should throw "IllegalArgumentException" // as k < 0 IntMath.pow(b, k); } catch (Exception e) { System.out.println(e); } } } |
chevron_right
filter_none
Output:
java.lang.IllegalArgumentException: exponent (-5) must be >= 0
Recommended Posts:
- Java Guava | mod() method of IntMath Class
- Java Guava | gcd(int a, int b) method of IntMath Class
- Java Guava | mean() method of IntMath Class
- Java Guava | ceilingPowerOfTwo() method of IntMath Class
- Java Guava | isPrime() method of IntMath Class
- Java Guava | floorPowerOfTwo() method IntMath Class
- Java Guava | binomial() method of IntMath Class
- Java Guava | isPowerOfTwo() method IntMath Class
- Java Guava | factorial(int n) method of IntMath Class with Examples
- Java Guava | sqrt(int x, RoundingMode mode) method of IntMath Class
- IntMath Class | Guava | Java
- Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples
- Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples
- Java Guava | IntMath.checkedPow(int b, int k) method with Examples
- Java Guava | IntMath.checkedAdd(int a, int b) method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



