Monday, 21 May 2018

How to Implement a Power Function in Java - Coding Problems

Even though the Java library has a power function, Math.pow() to calculate the power of a number in Java, it's a common coding exercise for Java programmer to implement a power function. The java.lang.Math.pow(double a, double b) returns the value of the first number raised to the power of the second number and you need to do the same. In other words, write a function to calculate the power of integer numbers for simplicity.

1. Problem:


Write a function in Java to calculate the power of integers.

Method Signature: power(int x, int y)

Purpose: Function should return the power of x^y

Input: power(2, 3) shouls return 8

2. Solution:


Oracle Java Tutorials and Materials, Oracle Java Guides, Oracle Java Learning
You can solve this problem by writing a function which just multiplies given number to itself by given number of times. For example, if power(x, y) you can return the value of x multiplied by itself y number of times. This can easily be done in a loop as shown in our Java program in the next section, but here is how the sample code looks like:

 public static long power(int x, int y) {
    long result = x;

    for (int i = 1; i < y; i++) {
      result = result * x;
    }

    return result;
  }

If you want, you can also start the loop from y and went down until you reach 1 but I like this approach. The function returns the power of x^y.

3. Analysis:


This problem is very similar to another popular coding problem, the power of two, which we discussed earlier. In that problem, I have shown you two ways, one using multiplication and other using bitwise operator because it was the power of 2 and left shift is equal to multiplying by two. Unfortunately, we cannot use the same technique here, hence I have just shown the solution using multiplication operator.

4. Java Program to calculate the power of a number in Java


package tool;

/**
 *
 * A simple Java Program to implement a power function pow(x, y) which should
 * return x^y.
 *
 * Input : (2, 3) Output: 8
 */
public class Hello {

  public static void main(String[] args) {

    System.out.println("2 to the power 3 : = " + power(2, 3));
    System.out.println("3 to the power 3 : = " + power(3, 3));
    System.out.println("2 to the power 5 : = " + power(2, 5));
    System.out.println("5 to the power 2 : = " + power(5, 2));
    System.out.println("9 to the power 2 : = " + power(9, 2));
  }

  /*
   * Calculate power using multiplication operator
   */
  public static long power(int x, int y) {
    long result = x;

    for (int i = 1; i < y; i++) {
      result = result * x;
    }

    return result;
  }

}

Output:
2 to the power 3 : = 8
3 to the power 3 : = 27
2 to the power 5 : = 32
5 to the power 2 : = 25
9 to the power 2 : = 81

That's all about how to calculate the power of a number in Java. You can see that our method is behaving as per expectation but if you want you can test more up to the mind when it really tests the logic e.g. trying out invalid values like zero, negative etc. Also, trying with the maximum value of Integer and see if the program works fine or not.

Related Posts