Wednesday, July 3, 2024
HomeLanguagesJavaJava Guava | pow(long b, int k) of LongMath Class with Examples

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

Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/LongMath.html#pow-long-int-

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments