If you have a few years of experience in Computer Science or research, and you’re interested in sharing that experience with the community, have a look at our **Contribution Guidelines**.

# The Most Efficient Way to Implement an Integer Based Power Function

Last modified: October 13, 2021

## 1. Overview

In this tutorial, we’ll talk about the most efficient way to implement an integer-based power function.

Firstly, we’ll define the problem. Then we’ll give an example to explain it.

Secondly, we’ll present two different approaches to solving this problem.

## 2. Defining the Problem

Given two positive integers and , we need to compute .

Let’s take a look at the following examples:

- , .
- , .
- , .

Recall that equals multiplied by itself times.

## 3. Loop Approach

### 3.1. Main Idea

**The main idea in this approach is to transform the power operation into a series of multiplication operations. **For example, for an example is equal to ( multiplied by itself times).

We’ll use for loop to multiply by itself times to solve our problem.

### 3.2. Algorithm

Let’s take a look at the implementation:

First, we declare as a variable to store the answer, and its initial value equals . The reason we start by 1 is that any number to the power of zero equals one. Second, we start a for loop that has iterations, in each iteration, we multiply by the current .

In the end, the will be equal to .

### 3.3. **Complexity**

**The complexity of this algorithm is **, where is the exponential part in our problem. The reason behind this complexity is that we’re using a loop to do operations.

## 4. Fast Power Approach

### 4.1. Main Idea

In this approach, we’ll use one of the power properties. **R****ecall that is equal to **. So, each time we can square the current base and divide the power by two. We have two cases to handle:

**In case the exponential part is an even number, then is equal to .****On the other hand, if is an odd number, then is equal to .**

At the end, when the power becomes equal to zero, the result will be equal to the answer to the problem.

### 4.2. Algorithm

Let’s take a look at the implementation:

Initially, we declare as a variable to store the answer. We set its initial value to since any number to the power of zero is equal to one. Next, as long as the power is greater than zero, we check whether the power is odd. If so, we multiply the by the base and decrease the power by one.

After this step, the value will be even. So, we divide the power by two and square the base . We keep repeating this process until the power becomes equal to zero.

Finally, we return , which is equal to .

### 4.3. **Complexity**

**The complexity of this algorithm is **, where is the exponential part.

The reason behind this complexity is that in each iteration, we divide the power by two. So, in total, we have iterations inside the loop.

## 5. Conclusion

In this article, we presented the most efficient way to implement an integer-based power function.

First, we provided an example to explain the problem. Then we gave two different approaches to solving it and walked through their implementations.