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

Output:

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

Output:

java.lang.IllegalArgumentException: exponent (-5) must be >= 0

My Personal Notes arrow_drop_up In love with a semicolon because sometimes i miss it so badly)

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.