Python program to find the power of a number using recursion (original) (raw)
Last Updated : 02 May, 2023
Given a number N and power P, the task is to find the power of a number ( i.e. NP ) using recursion.
Examples:
Input: N = 2 , P = 3
Output: 8Input: N = 5 , P = 2
Output: 25
Approach: Below is the idea to solve the above problem:
The idea is to calculate power of a number ‘N’ is to multiply that number ‘P’ times.
Follow the below steps to Implement the idea:
- Create a recursive function with parameters number N and power P.
- If P = 0 return 1.
- Else return N times result of the recursive call for N and P-1.
Below is the implementation of the above approach.
Python3
def
power(N, P):
`` if
P
=
=
0
:
`` return
1
`` return
(N
*
power(N, P
-
1
))
if
__name__
=
=
'__main__'
:
`` N
=
5
`` P
=
2
`` print
(power(N, P))
Time Complexity: O(P), For P recursive calls.
Auxiliary Space: O(P), For recursion call stack.
Optimized Approach :
Calling the recursive function for (n, p) -> (n, p-1) -> (n, p-2) -> … -> (n, 0) taking P recursive calls. In the optimized approach the idea is to
decrease the number of functions from p to log p.
Let’s see how.
we know that
if p is even we can write N p = N p/2 * N p/2 = (N p/2) 2 and
if p is odd we can wrte N p = N * (N (p-1)/2 * N (p-1)/2) = N * (N (p-1)/2) 2
for example : 24 = 22 * 22
also, 25 = 2 * (22 * 22)
From this definaion we can derive this recurrance relation as
if p is even
result = ( func(N, p/2) ) 2
else
result = N * ( func(N, (p-1)/2) ) 2
Below is the implementation of the above approach in python3
Python3
def
power(N, P):
`` if
P
=
=
0
:
`` return
1
`` if
P
%
2
=
=
0
:
`` result
=
power(N, P
/
/
2
)
`` return
result
*
result
`` else
:
`` result
=
power(N, (P
-
1
)
/
/
2
)
`` return
N
*
result
*
result
if
__name__
=
=
'__main__'
:
`` N
=
5
`` P
=
2
`` print
(power(N, P))
Time Complexity: O(log P), For log2P recursive calls.
Auxiliary Space: O(log P), For recursion call stack.
Similar Reads
- Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read
- Python List Exercise List OperationsAccess List ItemChange List itemReplace Values in a List in PythonAppend Items to a listInsert Items to a listExtend Items to a listRemove Item from a listClear entire listBasic List programsMaximum of two numbersWays to find length of listMinimum of two numbersTo interchange first an 3 min read
- Python String Exercise Basic String ProgramsCheck whether the string is Symmetrical or PalindromeFind length of StringReverse words in a given StringRemove i’th character from stringAvoid Spaces in string lengthPrint even length words in a stringUppercase Half StringCapitalize the first and last character of each word in 4 min read
- Python Tuple Exercise Basic Tuple ProgramsPython program to Find the size of a TuplePython – Maximum and Minimum K elements in TupleCreate a list of tuples from given list having number and its cube in each tuplePython – Adding Tuple to List and vice – versaPython – Sum of tuple elementsPython – Modulo of tuple elementsP 3 min read
- Python Dictionary Exercise Basic Dictionary ProgramsPython | Sort Python Dictionaries by Key or ValueHandling missing keys in Python dictionariesPython dictionary with keys having multiple inputsPython program to find the sum of all items in a dictionaryPython program to find the size of a DictionaryWays to sort list of dicti 3 min read
- Python Set Exercise Basic Set ProgramsFind the size of a Set in PythonIterate over a set in PythonPython - Maximum and Minimum in a SetPython - Remove items from SetPython - Check if two lists have at-least one element commonPython program to find common elements in three lists using setsPython - Find missing and addit 2 min read