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 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++
#include <iostream>
using namespace std;
int power( int x, unsigned int y)
{
int res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
int main()
{
int x = 3;
unsigned int y = 5;
cout<< "Power is " <<power(x, y);
return 0;
}
|
C
#include <stdio.h>
int power( int x, unsigned int y)
{
int res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
int main()
{
int x = 3;
unsigned int y = 5;
printf ( "Power is %d" , power(x, y));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int power( int x, int y)
{
int res = 1 ;
while (y > 0 )
{
if ((y & 1 ) == 1 )
res = res * x;
y = y >> 1 ;
x = x * x;
}
return res;
}
public static void main (String[] args)
{
int x = 3 ;
int y = 5 ;
System.out.println( "Power is " +
power(x, y));
}
}
|
Python3
def power(x, y):
res = 1
while (y > 0 ):
if ((y & 1 ) = = 1 ) :
res = res * x
y = y >> 1
x = x * x
return res
x = 3
y = 5
print ( "Power is " ,
power(x, y))
|
C#
using System;
class GFG
{
static int power( int x, int y)
{
int res = 1;
while (y > 0)
{
if ((y & 1) == 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
static public void Main ()
{
int x = 3;
int y = 5;
Console.WriteLine( "Power is " +
power(x, y));
}
}
|
PHP
<?php
function power( $x , $y )
{
$res = 1;
while ( $y > 0)
{
if ( $y & 1)
$res = $res * $x ;
$y = $y >> 1;
$x = $x * $x ;
}
return $res ;
}
$x = 3;
$y = 5;
echo "Power is " , power( $x , $y );
?>
|
Javascript
<script>
function power(x, y)
{
let res = 1;
while (y > 0) {
if (y & 1)
res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
let x = 3;
y = 5;
document.write( "Power is " + power(x, y));
</script>
|
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;
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;
}
int main()
{
int x = 3;
int y = 5;
cout << "Power is " << power(x, y);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int x= 3 ;
int y= 5 ;
System.out.println( "Power is: " +power(x,y));
}
public static 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;
}
}
}
|
Python3
def power(x, y):
if y = = 0 :
return 1
temp = power(x, y / / 2 )
if y % 2 = = 0 :
return temp * temp
else :
return x * temp * temp
def main():
x = 3
y = 5
print ( "Power is : " , power(x,y))
main()
|
C#
using System;
class GFG {
static 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;
}
static void Main()
{
int x = 3;
int y = 5;
Console.WriteLine( "Power is " + power(x, y));
}
}
|
Javascript
function power(x, y) {
if (y == 0)
return 1;
let temp = power(x, Math.floor(y / 2));
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
let x = 3;
let y = 5;
console.log( "Power is " + power(x, y));
|
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 log?(n), where log? denotes the logarithm to the base 2. Therefore, the time complexity of the function is proportional to the number of iterations, which is log?(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).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
12 Apr, 2023
Like Article
Save Article