(Translated by https://www.hiragana.jp/)
Write an iterative O(Log y) function for pow(x, y) - GeeksforGeeks
Open In App

Write an iterative O(Log y) function for pow(x, y)

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer x and a positive number y, write a function that computes xy under following conditions. 
a) Time complexity of the function should be O(Log y) 
b) Extra Space is O(1) 

Examples: 

Input: x = 3, y = 5
Output: 243

Input: x = 2, y = 5
Output: 32

We strongly recommend that you click here and practice it, before moving on to the solution.

We have discussed recursive O(Log y) solution for power. The recursive solutions are generally not preferred as they require space on call stack and they involve function call overhead. 

Following is implementation to compute xy.  

C++
// Iterative C program to implement pow(x, n)
#include <iostream>
using namespace std;

/* Iterative Function to calculate (x^y) in O(logy) */
int power(int x, unsigned int y)
{
    int res = 1; // Initialize result

    while (y > 0) {
        // If y is odd, multiply x with result
        if (y & 1)
            res = res * x;

        // y must be even now
        y = y >> 1; // y = y/2
        x = x * x; // Change x to x^2
    }
    return res;
}

// Driver program to test above functions
int main()
{
    int x = 3;
    unsigned int y = 5;

    cout<<"Power is "<<power(x, y);

    return 0;
}

// this code is contributed by shivanisinghss2110
C Java Python3 C# JavaScript PHP

Output
Power is 243

Time Complexity: O(log y), since in loop each time the value of y decreases by half it's current value.
Auxiliary Space: O(1), since no extra space has been taken.

Another approach:

Step 1: Start the function with the base and exponent as input parameters.
Step 2: Check if the exponent is equal to zero, return 1.
Step 3: Recursively call the function with the base and the exponent divided by 2.
Step 4: If the exponent is even, return the square of the result obtained from the recursive call.
Step 5: If the exponent is odd, return the product of the base, the square of the result obtained from the recursive call, and the base again.

C++
#include <iostream>
using namespace std;

// Recursive Function to calculate (x^y) in O(logy)
int power(int x, int y)
{
    if (y == 0)
        return 1;

    int temp = power(x, y / 2);
    if (y % 2 == 0)
        return temp * temp;
    else
        return x * temp * temp;
}

// Driver program to test above functions
int main()
{
    int x = 3;
    int y = 5;

    cout << "Power is " << power(x, y);

    return 0;
}
Java Python C# JavaScript

Output
Power is 243 

Complexity Analysis:

The time complexity of the power function implemented using recursion is O(log n), where n is the value of the exponent.

In this implementation, the function repeatedly divides the exponent by 2 and squares the base until the exponent becomes zero. The number of iterations required to reach the base case is log2(n), where log2 denotes the logarithm to the base 2. Therefore, the time complexity of the function is proportional to the number of iterations, which is log2(n).

This approach reduces the number of multiplications needed to calculate the result and improves the performance of the function compared to the naive iterative approach, which has a time complexity of O(n).

However, the space complexity of the recursive implementation is also O(log n), since each recursive call adds a new stack frame to the call stack until the base case is reached. Therefore, the space required by the function grows logarithmically with the size of the input. This can become a concern for very large values of n, as it may lead to a stack overflow error.

In summary, the recursive implementation of the power function has a time complexity of O(log n) and a space complexity of O(log n).


 


Iterative O(log) function for pow(x,y)
Article Tags :
Practice Tags :

Similar Reads