November 3, 2017

# Recursive exponential function in Python

For definition and more details please refer to the following post. This post only reimplements the code in Python.

**Python Code**

def RecursiveExp(x, n): # First base case if n == 0: return 1 # Second base case if n == 1: return x # Even values of (n) if n % 2 == 0: y = RecursiveExp(x, n / 2) return y * y # Odd values of (n) else: y = RecursiveExp(x, n - 1) return x * y # Examples for i in range(10): print "2 to the power of {} = {}".format(i, RecursiveExp(2, i))

If you run the code above you should get the output below

2 to the power of 0 = 1 2 to the power of 1 = 2 2 to the power of 2 = 4 2 to the power of 3 = 8 2 to the power of 4 = 16 2 to the power of 5 = 32 2 to the power of 6 = 64 2 to the power of 7 = 128 2 to the power of 8 = 256 2 to the power of 9 = 512

If you have questions please the comments sections below. Thanks for reading.