Write a function pow(x, n), which calculates x raised to the power n


Topic: Write a function pow(x, n), which calculates x raised to the power n

Solution

def myPow(x, n):
    def pow(y, n):
        if n == 0:
            return 1.0
        else:
            partial = pow(x, n//2)
            result = partial * partial
            if n%2 == 1:
                result *= x
            return result
           
  
    if n >= 0:
        return pow(x, n)
    else:
        return 1/ pow(x, -n)
		



List all Python Programs