Below solution divides the problem into subproblems of size y/2 and call the subproblems recursively.
C++
// C++ program to calculate pow(x,n)
#include<iostream>
usingnamespacestd;
classgfg
{
/* Function to calculate x raised to the power y */
public:
intpower(intx, unsigned inty)
{
if(y == 0)
return1;
elseif(y % 2 == 0)
returnpower(x, y / 2) * power(x, y / 2);
else
returnx * power(x, y / 2) * power(x, y / 2);
}
};
/* Driver code */
intmain()
{
gfg g;
intx = 2;
unsigned inty = 3;
cout << g.power(x, y);
return0;
}
// This code is contributed by SoM15242
C
#include<stdio.h>
/* Function to calculate x raised to the power y */
intpower(intx, unsigned inty)
{
if(y == 0)
return1;
elseif(y%2 == 0)
returnpower(x, y/2)*power(x, y/2);
else
returnx*power(x, y/2)*power(x, y/2);
}
/* Program to test function power */
intmain()
{
intx = 2;
unsigned inty = 3;
printf("%d", power(x, y));
return0;
}
Java
classGFG {
/* Function to calculate x raised to the power y */
staticintpower(intx, inty)
{
if(y == 0)
return1;
elseif(y % 2== 0)
returnpower(x, y / 2) * power(x, y / 2);
else
returnx * power(x, y / 2) * power(x, y / 2);
}
/* Program to test function power */
publicstaticvoidmain(String[] args)
{
intx = 2;
inty = 3;
System.out.printf("%d", power(x, y));
}
}
// This code is contributed by Smitha Dinesh Semwal
Python3
# Python3 program to calculate pow(x,n)
# Function to calculate x
# raised to the power y
defpower(x, y):
if(y ==0): return1
elif(int(y %2) ==0):
return(power(x, int(y /2)) *
power(x, int(y /2)))
else:
return(x *power(x, int(y /2)) *
power(x, int(y /2)))
# Driver Code
x =2; y =3
print(power(x, y))
# This code is contributed by Smitha Dinesh Semwal.
C#
usingSystem;
publicclassGFG {
// Function to calculate x raised to the power y
staticintpower(intx, inty)
{
if(y == 0)
return1;
elseif(y % 2 == 0)
returnpower(x, y / 2) * power(x, y / 2);
else
returnx * power(x, y / 2) * power(x, y / 2);
}
// Program to test function power
publicstaticvoidMain()
{
intx = 2;
inty = 3;
Console.Write(power(x, y));
}
}
// This code is contributed by shiv_bhakt.
PHP
<?php
// Function to calculate x
// raised to the power y
functionpower($x, $y)
{
if($y== 0)
return1;
elseif($y% 2 == 0)
returnpower($x, (int)$y/ 2) *
power($x, (int)$y/ 2);
else
return$x* power($x, (int)$y/ 2) *
power($x, (int)$y/ 2);
}
// Driver Code
$x= 2;
$y= 3;
echopower($x, $y);
// This code is contributed by ajit
?>
Javascript
<script>
// Javascript program to calculate pow(x,n)
// Function to calculate x
// raised to the power y
functionpower(x, y)
{
if(y == 0)
return1;
elseif(y % 2 == 0)
returnpower(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
else
returnx * power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
}
// Driver code
let x = 2;
let y = 3;
document.write(power(x, y));
// This code is contributed by mukesh07
</script>
Output :
8
Time Complexity: O(n) Space Complexity: O(1)
Algorithmic Paradigm: Divide and conquer. Above function can be optimized to O(logn) by calculating power(x, y/2) only once and storing it.
C++
/* Function to calculate x raised to the power y in O(logn)*/
intpower(intx, unsigned inty)
{
inttemp;
if( y == 0)
return1;
temp = power(x, y / 2);
if(y % 2 == 0)
returntemp * temp;
else
returnx * temp * temp;
}
// This code is contributed by Shubhamsingh10
C
/* Function to calculate x raised to the power y in O(logn)*/
intpower(intx, unsigned inty)
{
inttemp;
if( y == 0)
return1;
temp = power(x, y/2);
if(y%2 == 0)
returntemp*temp;
else
returnx*temp*temp;
}
Java
/* Function to calculate x raised to the power y in O(logn)*/
staticintpower(intx, inty)
{
inttemp;
if( y == 0)
return1;
temp = power(x, y / 2);
if(y % 2== 0)
returntemp*temp;
else
returnx*temp*temp;
}
// This code is contributed by divyeshrabadiya07.
Python3
# Function to calculate x raised to the power y in O(logn)
defpower(x,y):
temp =0
if( y ==0):
return1
temp =power(x, int(y /2))
if(y %2==0)
returntemp *temp;
else
returnx *temp *temp;
# This code is contributed by avanitrachhadiya2155
C#
/* Function to calculate x raised to the power y in O(logn)*/
staticintpower(intx, inty)
{
inttemp;
if( y == 0)
return1;
temp = power(x, y / 2);
if(y % 2 == 0)
returntemp*temp;
else
returnx*temp*temp;
}
// This code is contributed by divyesh072019.
Javascript
<script>
/* Function to calculate x raised to the power y in O(logn)*/
functionpower(x , y)
{
vartemp;
if( y == 0)
return1;
temp = power(x, y / 2);
if(y % 2 == 0)
returntemp*temp;
else
returnx*temp*temp;
}
// This code is contributed by todaysgaurav
</script>
Time Complexity of optimized solution: O(log(n))
Auxiliary Space: O(log(n))
Let us extend the pow function to work for negative y and float x.
C++
/* Extended version of power function
that can work for float x and negative y*/
#include <bits/stdc++.h>
usingnamespacestd;
floatpower(floatx, inty)
{
floattemp;
if(y == 0)
return1;
temp = power(x, y / 2);
if(y % 2 == 0)
returntemp * temp;
else
{
if(y > 0)
returnx * temp * temp;
else
return(temp * temp) / x;
}
}
// Driver Code
intmain()
{
floatx = 2;
inty = -3;
cout << power(x, y);
return0;
}
// This is code is contributed
// by rathbhupendra
C
/* Extended version of power function that can work
for float x and negative y*/
#include<stdio.h>
floatpower(floatx, inty)
{
floattemp;
if( y == 0)
return1;
temp = power(x, y/2);
if(y%2 == 0)
returntemp*temp;
else
{
if(y > 0)
returnx*temp*temp;
else
return(temp*temp)/x;
}
}
/* Program to test function power */
intmain()
{
floatx = 2;
inty = -3;
printf("%f", power(x, y));
return0;
}
Java
/* Java code for extended version of power function
that can work for float x and negative y */
classGFG {
staticfloatpower(floatx, inty)
{
floattemp;
if( y == 0)
return1;
temp = power(x, y/2);
if(y%2== 0)
returntemp*temp;
else
{
if(y > 0)
returnx * temp * temp;
else
return(temp * temp) / x;
}
}
/* Program to test function power */
publicstaticvoidmain(String[] args)
{
floatx = 2;
inty = -3;
System.out.printf("%f", power(x, y));
}
}
// This code is contributed by Smitha Dinesh Semwal.
Python3
# Python3 code for extended version
# of power function that can work
# for float x and negative y
defpower(x, y):
if(y ==0): return1
temp =power(x, int(y /2))
if(y %2==0):
returntemp *temp
else:
if(y > 0): returnx *temp *temp
else: return(temp *temp) /x
# Driver Code
x, y =2, -3
print('%.6f'%(power(x, y)))
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# code for extended version of power function
// that can work for float x and negative y
usingSystem;
publicclassGFG{
staticfloatpower(floatx, inty)
{
floattemp;
if( y == 0)
return1;
temp = power(x, y/2);
if(y % 2 == 0)
returntemp * temp;
else
{
if(y > 0)
returnx * temp * temp;
else
return(temp * temp) / x;
}
}
// Program to test function power
publicstaticvoidMain()
{
floatx = 2;
inty = -3;
Console.Write(power(x, y));
}
}
// This code is contributed by shiv_bhakt.
PHP
<?php
// Extended version of power
// function that can work
// for float x and negative y
functionpower($x, $y)
{
$temp;
if( $y== 0)
return1;
$temp= power($x, $y/ 2);
if($y% 2 == 0)
return$temp* $temp;
else
{
if($y> 0)
return$x*
$temp* $temp;
else
return($temp*
$temp) / $x;
}
}
// Driver Code
$x= 2;
$y= -3;
echopower($x, $y);
// This code is contributed by ajit
?>
Javascript
<script>
// Javascript code for extended
// version of power function that
// can work for var x and negative y
functionpower(x, y)
{
vartemp;
if(y == 0)
return1;
temp = power(x, parseInt(y / 2));
if(y % 2 == 0)
returntemp * temp;
else
{
if(y > 0)
returnx * temp * temp;
else
return(temp * temp) / x;
}
}
// Driver code
varx = 2;
vary = -3;
document.write( power(x, y).toFixed(6));
// This code is contributed by aashish1995
</script>
Output :
0.125000
Time Complexity: O(log|n|)
Auxiliary Space: O(1)
Using recursion:
This approach is almost similar to iterative solution
C++
// C++ program for the above approach
#include <bits/stdc++.h>
usingnamespacestd;
intpower(intx, inty)
{
// If x^0 return 1
if(y == 0)
return1;
// If we need to find of 0^y
if(x == 0)
return0;
// For all other cases
returnx * power(x, y - 1);
}
// Driver Code
intmain()
{
intx = 2;
inty = 3;
cout << (power(x, y));
}
// This code is contributed by Aditya Kumar (adityakumar129)
C
// C program for the above approach
#include <stdio.h>
intpower(intx, inty)
{
// If x^0 return 1
if(y == 0)
return1;
// If we need to find of 0^y
if(x == 0)
return0;
// For all other cases
returnx * power(x, y - 1);
}
// Driver Code
intmain()
{
intx = 2;
inty = 3;
printf("%d\n",power(x, y));
}
// This code is contributed by Aditya Kumar (adityakumar129)
Java
// Java program for the above approach
importjava.io.*;
classGFG {
publicstaticintpower(intx, inty)
{
// If x^0 return 1
if(y == 0)
return1;
// If we need to find of 0^y
if(x == 0)
return0;
// For all other cases
returnx * power(x, y - 1);
}
// Driver Code
publicstaticvoidmain(String[] args)
{
intx = 2;
inty = 3;
System.out.println(power(x, y));
}
}
Python3
# Python3 program for the above approach
defpower(x, y):
# If x^0 return 1
if(y ==0):
return1
# If we need to find of 0^y
if(x ==0):
return0
# For all other cases
returnx *power(x, y -1)
# Driver Code
x =2
y =3
print(power(x, y))
# This code is contributed by shivani.
C#
// C# program for the above approach
usingSystem;
classGFG{
publicstaticintpower(intx, inty)
{
// If x^0 return 1
if(y == 0)
return1;
// If we need to find of 0^y
if(x == 0)
return0;
// For all other cases
returnx * power(x, y - 1);
}
// Driver Code
publicstaticvoidMain(String[] args)
{
intx = 2;
inty = 3;
Console.WriteLine(power(x, y));
}
}
// This code is contributed by Rajput-Ji
Javascript
<script>
// javascript program for the above approach
functionpower(x , y) {
// If x^0 return 1
if(y == 0)
return1;
// If we need to find of 0^y
if(x == 0)
return0;
// For all other cases
returnx * power(x, y - 1);
}
// Driver Code
varx = 2;
vary = 3;
document.write(power(x, y));
// This code is contributed by Rajput-Ji
</script>
Output:
8
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Math.pow() function of java:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
usingnamespacestd;
intpower(intx, inty)
{
// return type of pow()
// function is double
return(int)pow(x, y);
}
// Driver Code
intmain()
{
intx = 2;
inty = 3;
cout << (power(x, y));
}
// This code is contributed by hemantraj712.
Java
// Java program for the above approach
importjava.io.*;
classGFG {
publicstaticintpower(intx, inty)
{
// Math.pow() is a function that
// return floating number
return(int)Math.pow(x, y);
}
// Driver Code
publicstaticvoidmain(String[] args)
{
intx = 2;
inty = 3;
System.out.println(power(x, y));
}
}
Python3
# Python3 program for the above approach
defpower(x, y):
# Return type of pow()
# function is double
returnpow(x, y)
# Driver Code
x =2
y =3
print(power(x, y))
# This code is contributed by susmitakundugoaldanga
C#
// C# program for the above approach
usingSystem;
publicclassGFG {
publicstaticintpower(intx, inty)
{
// Math.pow() is a function that
// return floating number
return(int)Math.Pow(x, y);
}
// Driver code
staticpublicvoidMain()
{
intx = 2;
inty = 3;
Console.WriteLine(power(x, y));
}
}
Javascript
<script>
// Javascript program for the above approach
functionpower( x, y)
{
// Math.pow() is a function that
// return floating number
returnparseInt(Math.pow(x, y));
}
// Driver Code
let x = 2;
let y = 3;
document.write(power(x, y));
// This code is contributed by sravan kumar
</script>
Output:
8
Time Complexity: O(1)
Auxiliary Space: O(1)
Iterative approach : Binary explanation
Some important concepts related to this approach:
Every number can be written as the sum of powers of 2
We can traverse through all the bits of a number from LSB to MSB in O(log(n)) time.
For example :
3^10 = 3^8 + 3^2 ; (10 in binary can be represented as : 1010 , where from left side the first 1 represents 3^2 and second 1 represents 3^8 )
3^19 = 3^16 + 3^2+3 ; (19 in binary can be represented as : 10011 , where from left side the first 1 represents 3^1 and second 1 represents 3^2 and the
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy