Write program to calculate pow(b, e)
Given two numbers b and e, the task is to implement a function to compute b^e.
Examples:
Input: b = 3.00000, e = 5
Output: 243.00000Input: b = 0.55000, e = 3
Output: 0.16638Input: b = -0.67000, e = -7
Output: -16.49971
Table of Content
[Naive Approach 1] Using Iteration - O(e) Time and O(1) Space
The idea is to simply multiply b exactly e times using a iterative loop.
// C++ program to calculate pow(b, e)
#include <iostream>
using namespace std;
// Naive iterative solution to calculate pow(b, e)
double power(double b, int e) {
// Initialize result to 1
double pow = 1;
// Multiply x for n times
for (int i = 0; i < abs(e); i++)
pow = pow * b;
if (e < 0)
return 1/pow;
return pow;
}
int main() {
double b = 3.0;
int e = 5;
double res = power(b, e);
cout << res;
return 0;
}
// C program to calculate pow(b, e)
#include <stdio.h>
#include <math.h>
// Naive iterative solution to calculate pow(b, e)
double power(double b, int e) {
// Initialize result to 1
double pow = 1;
// Multiply b for e times
for (int i = 0; i < abs(e); i++)
pow = pow * b;
if (e < 0)
return 1 / pow;
return pow;
}
int main() {
double b = 3.0;
int e = 5;
double res = power(b, e);
printf("%f", res);
return 0;
}
// Java program to calculate pow(b, e)
class GfG {
// Naive iterative solution to calculate pow(b, e)
static double power(double b, int e) {
// Initialize result to 1
double pow = 1;
// Multiply b for e times
for (int i = 0; i < Math.abs(e); i++)
pow = pow * b;
if (e < 0)
return 1 / pow;
return pow;
}
public static void main(String[] args) {
double b = 3.0;
int e = 5;
double res = power(b, e);
System.out.println(res);
}
}
# Python program to calculate pow(b, e)
# Naive iterative solution to calculate pow(b, e)
def power(b, e):
# Initialize result to 1
pow = 1
# Multiply b for e times
for i in range(abs(e)):
pow = pow * b
if e < 0:
return 1 / pow
return pow
if __name__ == "__main__":
b = 3.0
e = 5
res = power(b, e)
print(res)
// C# program to calculate pow(b, e)
using System;
class GfG {
// Naive iterative solution to calculate pow(b, e)
static double power(double b, int e) {
// Initialize result to 1
double pow = 1;
// Multiply b for e times
for (int i = 0; i < Math.Abs(e); i++)
pow = pow * b;
if (e < 0)
return 1 / pow;
return pow;
}
static void Main(string[] args) {
double b = 3.0;
int e = 5;
double res = power(b, e);
Console.WriteLine(res);
}
}
// JavaScript program to calculate pow(b, e)
// Naive iterative solution to calculate pow(b, e)
function power(b, e) {
// Initialize result to 1
let pow = 1;
// Multiply b for e times
for (let i = 0; i < Math.abs(e); i++)
pow = pow * b;
if (e < 0)
return 1 / pow;
return pow;
}
// Driver Code
const b = 3.0;
const e = 5;
const res = power(b, e);
console.log(res);
Output
243
[Naive Approach 2] Using Recursion - O(e) Time and O(e) Space
The idea is to recursively multiply b exactly e times. To do so, define a recursive function that return b, if e > 0 else returns 1.
// C++ program to calculate pow(b, e)
#include <iostream>
using namespace std;
double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
// For all other cases
return b * power(b, e - 1);
}
int main() {
double b = 3.0;
int e = 5;
double res = power(b, e);
cout << res;
return 0;
}
// C program to calculate pow(b, e)
#include <stdio.h>
double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
// For all other cases
return b * power(b, e - 1);
}
int main() {
double b = 3.0;
int e = 5;
double res = power(b, e);
printf("%f", res);
return 0;
}
// Java program to calculate pow(b, e)
class GfG {
static double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
// For all other cases
return b * power(b, e - 1);
}
public static void main(String[] args) {
double b = 3.0;
int e = 5;
double res = power(b, e);
System.out.println(res);
}
}
# Python program to calculate pow(b, e)
def power(b, e):
# Base Case: pow(b, 0) = 1
if e == 0:
return 1
if e < 0:
return 1 / power(b, -e)
# For all other cases
return b * power(b, e - 1)
if __name__ == "__main__":
b = 3.0
e = 5
res = power(b, e)
print(res)
// C# program to calculate pow(b, e)
using System;
class GfG {
static double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
// For all other cases
return b * power(b, e - 1);
}
static void Main(string[] args) {
double b = 3.0;
int e = 5;
double res = power(b, e);
Console.WriteLine(res);
}
}
// JavaScript program to calculate pow(b, e)
function power(b, e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
// For all other cases
return b * power(b, e - 1);
}
// Driver Code
const b = 3.0;
const e = 5;
const res = power(b, e);
console.log(res);
Output
243
[Expected Approach] Using Divide and Conquer - O(log e) Time and O(log e) Space
The idea is to use Divide and Conquer and recursively bisect e in two equal parts. There are two possible cases:
- If e is even: power(b, e) = power(b, e / 2) * power(b, e / 2);
- If e is odd: power(b, e) = b * power(b, e / 2) * power(b, e / 2);
// C++ program to calculate pow(b, e)
#include <iostream>
using namespace std;
double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
double temp = power(b, e / 2);
if (e % 2 == 0)
return temp * temp;
else
return b * temp * temp;
}
int main() {
double b = 3.0;
int e = 5;
double res = power(b, e);
cout << res;
return 0;
}
// C program to calculate pow(b, e)
#include <stdio.h>
// Recursive function to calculate pow(b, e)
double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
double temp = power(b, e / 2);
if (e % 2 == 0)
return temp * temp;
else
return b * temp * temp;
}
int main() {
double b = 3.0;
int e = 5;
double res = power(b, e);
printf("%lf", res);
return 0;
}
// Java program to calculate pow(b, e)
public class Main {
// Recursive function to calculate pow(b, e)
static double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
double temp = power(b, e / 2);
if (e % 2 == 0)
return temp * temp;
else
return b * temp * temp;
}
public static void main(String[] args) {
double b = 3.0;
int e = 5;
double res = power(b, e);
System.out.println(res);
}
}
# Python program to calculate pow(b, e)
# Recursive function to calculate pow(b, e)
def power(b, e):
# Base Case: pow(b, 0) = 1
if e == 0:
return 1
if e < 0:
return 1 / power(b, -e)
temp = power(b, e // 2)
if e % 2 == 0:
return temp * temp
else:
return b * temp * temp
if __name__ == "__main__":
b = 3.0
e = 5
res = power(b, e)
print(res)
// C# program to calculate pow(b, e)
using System;
class Program {
// Recursive function to calculate pow(b, e)
static double power(double b, int e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
double temp = power(b, e / 2);
if (e % 2 == 0)
return temp * temp;
else
return b * temp * temp;
}
static void Main(string[] args) {
double b = 3.0;
int e = 5;
double res = power(b, e);
Console.WriteLine(res);
}
}
// JavaScript program to calculate pow(b, e)
// Recursive function to calculate pow(b, e)
function power(b, e) {
// Base Case: pow(b, 0) = 1
if (e == 0)
return 1;
if (e < 0)
return 1 / power(b, -e);
let temp = power(b, Math.floor(e / 2));
if (e % 2 == 0)
return temp * temp;
else
return b * temp * temp;
}
// Driver Code
const b = 3.0;
const e = 5;
const res = power(b, e);
console.log(res);
Output
243
Using Inbuilt Functions - O(log e) Time and O(1) Space
The idea is to use inbuilt functions provided by various languages to calculate b^e.
Like in C++,
pow(b, e)
can be used to calculate b^e. Similarly, in Python, the exact same function can be used, or the**
operator can be used to find the power.
// C++ program to calculate power Using builtin methods
#include <iostream>
#include <cmath>
using namespace std;
double power(double b, int e) {
return pow(b, e);
}
int main() {
double b = 3.0;
int e = 5;
cout << power(b, e);
}
// Java program to calculate power
// Using builtin methods
class GFG {
static double power(double b, int e) {
return Math.pow(b, e);
}
public static void main(String[] args) {
double b = 3.0;
int e = 5;
System.out.println(power(b, e));
}
}
# Python program to calculate power
# Using builtin methods
def power(b, e):
# using (**) operator
# return b**e
# Return type of pow()
# function is double
return pow(b, e)
if __name__ == "__main__":
b = 3.0
e = 5
print(power(b, e))
// C# program to calculate power
// Using builtin methods
using System;
class GFG {
static double power(double b, int e) {
// Math.pow() is a function that
// return floating number
return Math.Pow(b, e);
}
static void Main() {
double b = 3.0;
int e = 5;
Console.WriteLine(power(b, e));
}
}
// Javascript program to calculate power
// Using builtin methods
function power(b, e) {
// Math.pow() is a function that
// return floating number
return parseInt(Math.pow(b, e));
}
// Driver Code
const b = 3.0;
const e = 5;
console.log(power(b, e));
Output
243
Related Articles:
Write an iterative O(Log y) function for pow(x, y)
Modular Exponentiation (Power in Modular Arithmetic)