Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

Python: recursion in finding the power of X

$
0
0
def pow(X, N):    # base case    if N == 0:        return 1    # recursion call    temp = pow(X, N//2)     # if N is odd then we will have to multiply x    if N%2 == 1:        return temp*temp*X    return temp*temp
Algorithm  time complexity : O(log n)  space complexity : O(log n)

Can someone help me with this code? Mainly I want to know what is the role of return statements in this as I get confused a lot from a beginner POV and what is N//2 ?


Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>