How to Calculate Power using Recursion | Pow(x, n)

In this tutorial, I am going to discuss how to calculate power using recursion.

Problem statement – Write a code to implement function pow(x, n), which calculates x raised to the power n (i.e. x^n). In this problem, We don’t have to use in-built function Math.pow.

For example:

Example 1:

Input:   x = 2.00000,    n = 3
Output:  8

Example 2:

Input: x = 2.10000,     n = 3
Output: 9.26100

Example 3:

Input: x = 2.00000,   n = -2
Output: 0.25000

In Java, we can easily calculate the power of any number using java.lang.Math.pow() method. It returns the result after calculating the value of the first argument raised to the power of the second argument.

Now, Let’s think about how we can calculate the power of a number without using Math.pow() in java.

In this tutorial, I am going to explain the iterative and recursive approaches to calculate the power of a number. Also, at the end of this tutorial, I have shared the link to a video tutorial.

Calculate Power using Recursion
Calculate Power using Recursion

Calculate Power of a Number – Iterative Approach

Let’s start with the easiest approach.  The easiest approach is to run a loop from 1 to n (where n is the exponent). Declare one variable (result) and initialize with 1. Then in each iteration multiply the value of the base with the value of the result.

To handle the case where the value of n is negative. Run a loop from 1 to n (ignoring negative sign). Calculate the result and finally divide the result by 1.

Calculate Power using Recursion – Java Code

We have discussed the iterative approach and it’s very straightforward. Let’s calculate the power of a number using recursion. If you are not familiar with the concept of recursion then check my previous tutorials on recursion.

Recursion vs Iteration – Difference between recursion and iteration

Programming questions on recursion

In recursion, the function will call itself until the base condition is not reached. So, when we solve certain problems using recursion it is important to define the base condition else the function will call itself infinitely (Once the allocated memory is full it will throw stack overflow exception).

In this example, I have just converted the iterative version of code to its recursive version. The time complexity of this approach is O(n).

How we can improve this code further? For a large value of n, the previous code will throw a time limit exceeded exception. In this example, Let’s discuss how we can solve this problem in O(logn) time complexity.

Suppose we have to calculate the power of 210. To calculate the power we can write them as (2*2)5. In this way, we have reduced the computation steps.

Now let’s take the case of odd 211. For this, we can write as 2 * (2*2)5.   Using this logic we can solve this problem efficiently.

Programming video tutorials

Data Structures Made Easy

In this video tutorial, I have explained how we can calculate the power in java using Math.pow() method.

Calculate power of a number using recursion – video tutorial

Tagged , , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.