Given a number N and power P. The task is to write a Python program to find the power of a number using recursion.
Definition: Power of a number can be defined as multiplication of the number repetitively the number of times of its power.
Example:
Input: Number=2 Power =3
Output: 2 power 3 = 8
Python program to find power of a number using Iterative approach
Here, we have used a for-loop to calculate the power, by iteratively multiplying the number for a given number of times.
Python3
def CalculatePower(N,X): P = 1 for i in range ( 1 , X + 1 ): P = P * N return P N,X = 2 , 3 print (CalculatePower(N,X)) N,X = 3 , 4 print (CalculatePower(N,X)) |
8 81
Refer end for complexity analysis.
Find power of a number Recursive Approach
Here we have used a recursive function calc_power() to calculate the power of a given number using recursion. This function will return 1 if power is 0, which is base condition for the function.
Python3
def calc_power(N, p): if p = = 0 : return 1 return N * calc_power(N, p - 1 ) print (calc_power( 4 , 2 )) |
16
Refer end for complexity analysis.
Find Power of number Using pow() function
Here, we have used the built-in pow() function to calculate the power of any positive integer.
Python3
print ( pow ( 9 , 2 )) |
81
Refer end for complexity analysis.
Find power of number Using ** operator
Here, we have used exponentiation to calculate the power of a number. This is most convenient to use as no function call or module import is required.
Python3
print ( 4 * * 3 ) |
64
Refer end for complexity analysis.
Find power of number Using numpy:
You can install NumPy by running the following command:
pip install numpy
using the NumPy library’s “power” function. This function allows you to calculate the power of a number using a single line of code. Here is an example of how to use it:
Python3
import numpy as np N = 2 X = 3 result = np.power(N, X) print (result) # Output: 8 #This code is contributed by Edula Vinay Kumar Reddy |
Output
8
Refer end for complexity analysis.
Find power of number Using math.exp() function
In math library, the math.exp() function in Python is used to calculate the value of the mathematical constant e (2.71828…) raised to a given power. It takes a single argument, which is the exponent to which the constant e should be raised, and returns the result as a float.
Python3
import math # Calculate e^2 result = math.exp( 2 ) print (result) # Output: 7.38905609893065 # This code is contributed by Susobhan Akhuli |
7.38905609893
Also by using math.log() and math.exp() function we can find power of any number.
Python3
import math # Calculate 2^3 result = math.exp(math.log( 2 ) * 3 ) print (result) # Output: 8.0 # This code is contributed by Susobhan Akhuli |
7.999999999999998
Refer end for complexity analysis.
Find power of numner Using Bit Manipulation
Here, we use right-shift operator “>>” to find power of a number.
Python3
def calculatePower(a, n): ans = 1 while (n > 0 ): last_bit = (n & 1 ) if (last_bit): ans = ans * a a = a * a n = n >> 1 return ans # Driver code if __name__ = = '__main__' : a = 2 n = 3 print (calculatePower(a, n)) # This code is contributed by Susobhan Akhuli |
8
Refer end for complexity analysis.
Find power of number Using math.log2() and ** Operator
Here, we can use the math.log2() in combination with the operator “**” to calculate the power of a number.
Python3
import math def calculatePower(a, n): return round ( 2 * * (math.log2(a) * n)) # Driver code if __name__ = = '__main__' : a = 2 n = 3 print (calculatePower(a, n)) # Output: a^n # This code is contributed by Susobhan Akhuli |
8
Find power of numner Using Exponentiation by Squaring Method
Python3
def exponentiation_by_squaring(base, exponent): result = 1 while exponent > 0 : if exponent % 2 = = 1 : result * = base exponent = exponent >> 1 base * = base return result print (exponentiation_by_squaring( 2 , 3 )) # 2^3 = 8 # This code is contributed by Susobhan Akhuli |
8
Find power of numner Using Recursive Exponentiation by Squaring
The recursive exponentiation by squaring method is similar to the iterative approach but implemented recursively. It also uses the divide-and-conquer strategy to reduce the number of multiplications required.
Python3
def recursive_exponentiation_by_squaring(base, exponent): if exponent = = 0 : return 1 elif exponent = = 1 : return base elif exponent % 2 = = 0 : return recursive_exponentiation_by_squaring(base * base, exponent / / 2 ) else : return base * recursive_exponentiation_by_squaring(base * base, (exponent - 1 ) / / 2 ) print (recursive_exponentiation_by_squaring( 2 , 3 )) # 2^3 = 8 |
8
Time complexity: O(log n), the same as the iterative approach,
Auxiliary space: O(log n) due to the recursive calls on the stack.
Refer end for complexity analysis.
Complexity Analysis:
Method |
Time Complexity |
Auxiliary Space |
---|---|---|
Iterative approach |
O(X) [For X times iteration] |
O(1) |
Recursive |
O(P) [For recursion p times] |
O(P) [To store in |
pow() function |
O(Log(exponent)) |
O(1) |
** Operator |
O(Log(exponent)) |
O(1) |
Use numpy: |
O(X) |
O(1) |
math.exp() |
O(1) |
O(1) |
Bit Manipulation |
O(Log(exponent)) |
O(1) |
math.log2() and ** Operator |
O(Log(exponent)) |
O(1) |
Squaring Method |
O(Log(exponent)) |
O(1) |