Java Guava | pow(long b, int k) of LongMath Class with Examples
The method pow(long b, int k) of Guava’s LongMath Class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).longValue(). This implementation runs in O(log k) time.
Syntax:
public static long pow(long b, int k)
Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised to the k-th power.
Return Value: This method returns the k-th power of b.
Exception: This method throws IllegalArgumentException if k is negative.
Example 1:
// Java code to show implementation of // pow(long b, int k) method of Guava's // LongMath Class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { // Driver code public static void main(String args[]) { long b1 = 4; int k1 = 5; long ans1 = LongMath.pow(b1, k1); System.out.println(b1 + " to the " + k1 + "th power is: " + ans1); long b2 = 12; int k2 = 3; long ans2 = LongMath.pow(b2, k2); System.out.println(b2 + " to the " + k2 + "rd power is: " + ans2); } } |
4 to the 5th power is: 1024 12 to the 3rd power is: 1728
Example 2:
// Java code to show implementation of // pow(long b, int k) method of Guava's // LongMath class import java.math.RoundingMode; import com.google.common.math.LongMath; class GFG { static long findPow(long b, int k) { try { // Using pow(long b, int k) // method of Guava's LongMath class // This should throw "IllegalArgumentException" // as k < 0 long ans = LongMath.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[]) { long b = 4; int k = -5; try { // Using pow(long b, int k) // method of Guava's LongMath class // This should throw "IllegalArgumentException" // as k < 0 LongMath.pow(b, k); } catch (Exception e) { System.out.println(e); } } } |
java.lang.IllegalArgumentException: exponent (-5) must be >= 0
Recommended Posts:
- Java Guava | binomial(int n, int k) of LongMath Class with Examples
- Java Guava | isPowerOfTwo(long x) of LongMath Class with Examples
- Java Guava | log2(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- Java Guava | mod(long x, long m) of LongMath Class with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
- LongMath Class | Guava | Java
- Java Guava | LongMath.checkedMultiply(int a, int b) method with Examples
- Java Guava | LongMath.checkedPow(long b, int k) method with Examples
- Java Guava | LongMath log10(long x, RoundingMode mode) method with Examples
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | factorial(int n) method of IntMath Class with Examples
- Java Guava | compare() method of Short Class 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.



