Given two integers x and n, write a function to compute xn. We may assume that x and n are small and overflow doesn’t happen.

Examples :
Input : x = 2, n = 3
Output : 8
Input : x = 7, n = 2
Output : 49
Below solution divides the problem into subproblems of size y/2 and call the subproblems recursively.
C++
#include<iostream>
using namespace std;
class gfg
{
public:
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
};
int main()
{
gfg g;
int x = 2;
unsigned int y = 3;
cout << g.power(x, y);
return 0;
}
|
C
#include<stdio.h>
int power(int x, unsigned int y)
{
if (y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
int main()
{
int x = 2;
unsigned int y = 3;
printf("%d", power(x, y));
return 0;
}
|
Java
class GFG {
static int power(int x, int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
public static void main(String[] args)
{
int x = 2;
int y = 3;
System.out.printf("%d", power(x, y));
}
}
|
Python3
def power(x, y):
if (y == 0): return 1
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)))
x = 2; y = 3
print(power(x, y))
|
C#
using System;
public class GFG {
static int power(int x, int y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
else
return x * power(x, y / 2) * power(x, y / 2);
}
public static void Main()
{
int x = 2;
int y = 3;
Console.Write(power(x, y));
}
}
|
PHP
<?php
function power($x, $y)
{
if ($y == 0)
return 1;
else if ($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);
}
$x = 2;
$y = 3;
echo power($x, $y);
?>
|
Javascript
<script>
function power(x, y)
{
if (y == 0)
return 1;
else if (y % 2 == 0)
return power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
else
return x * power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
}
let x = 2;
let y = 3;
document.write(power(x, y));
</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++
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
|
C
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
|
Java
static int power(int x, int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp*temp;
else
return x*temp*temp;
}
|
Python3
def power(x,y):
temp = 0
if( y == 0):
return 1
temp = power(x, int(y / 2))
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
|
C#
static int power(int x, int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp*temp;
else
return x*temp*temp;
}
|
Javascript
<script>
function power(x , y)
{
var temp;
if( y == 0)
return 1;
temp = power(x, y / 2);
if (y % 2 == 0)
return temp*temp;
else
return x*temp*temp;
}
</script>
|
Time Complexity of optimized solution: O(logn)
Let us extend the pow function to work for negative y and float x.
C++
#include <bits/stdc++.h>
using namespace std;
float power(float x, int y)
{
float temp;
if(y == 0)
return 1;
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;
}
}
int main()
{
float x = 2;
int y = -3;
cout << power(x, y);
return 0;
}
|
C
#include<stdio.h>
float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
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;
}
}
int main()
{
float x = 2;
int y = -3;
printf("%f", power(x, y));
return 0;
}
|
Java
class GFG {
static float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
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;
}
}
public static void main(String[] args)
{
float x = 2;
int y = -3;
System.out.printf("%f", power(x, y));
}
}
|
Python3
def power(x, y):
if(y == 0): return 1
temp = power(x, int(y / 2))
if (y % 2 == 0):
return temp * temp
else:
if(y > 0): return x * temp * temp
else: return (temp * temp) / x
x, y = 2, -3
print('%.6f' %(power(x, y)))
|
C#
using System;
public class GFG{
static float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
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;
}
}
public static void Main()
{
float x = 2;
int y = -3;
Console.Write(power(x, y));
}
}
|
PHP
<?php
function power($x, $y)
{
$temp;
if( $y == 0)
return 1;
$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;
}
}
$x = 2;
$y = -3;
echo power($x, $y);
?>
|
Javascript
<script>
function power(x, y)
{
var temp;
if (y == 0)
return 1;
temp = power(x, parseInt(y / 2));
if (y % 2 == 0)
return temp * temp;
else
{
if (y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
var x = 2;
var y = -3;
document.write( power(x, y).toFixed(6));
</script>
|
Output :
0.125000
Time Complexity: O(log|n|)
Auxiliary Space: O(1)
Using recursion:
This approach is almost similar to iterative solution
Java
import java.io.*;
class GFG {
public static int power(int x, int y)
{
if (y == 0)
return 1;
if (x == 0)
return 0;
return x * power(x, y - 1);
}
public static void main(String[] args)
{
int x = 2;
int y = 3;
System.out.println(power(x, y));
}
}
|
C#
using System;
class GFG{
public static int power(int x, int y)
{
if (y == 0)
return 1;
if (x == 0)
return 0;
return x * power(x, y - 1);
}
public static void Main(String[] args)
{
int x = 2;
int y = 3;
Console.WriteLine(power(x, y));
}
}
|
Javascript
<script>
function power(x , y) {
if (y == 0)
return 1;
if (x == 0)
return 0;
return x * power(x, y - 1);
}
var x = 2;
var y = 3;
document.write(power(x, y));
</script>
|
Output:
8
Using Math.pow() function of java:
Java
import java.io.*;
class GFG {
public static int power(int x, int y)
{
return (int)Math.pow(x, y);
}
public static void main(String[] args)
{
int x = 2;
int y = 3;
System.out.println(power(x, y));
}
}
|
Javascript
<script>
function power( x, y)
{
return parseInt(Math.pow(x, y));
}
let x = 2;
let y = 3;
document.write(power(x, y));
</script>
|
Output:
8
Write an iterative O(Log y) function for pow(x, y)
Modular Exponentiation (Power in Modular Arithmetic)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.