Python’s pow function returns the result of raising a number to a given power. Define a function expo that performs this task, and state its computational complexity using big-O notation. The first argument of this function is the number, and the second argument is the exponent (nonnegative numbers only). You may use either a loop or a recursive function in your implementation.

Answer :

MrRoyal

Answer:

The function is as follows:

def expo(num,n):

   prod= 1

   for i in range(n):

       prod*=num

       

   return prod

The big-O notation is: O(n)

Explanation:

This defines the function

def expo(num,n):

This initialzes the product to 1

   prod= 1

This iteration is perfomed n times

   for i in range(n):

The number is multiplied by itself n times

       prod*=num

       

This returns the calculated power

   return prod

To calculate the complexity.

The loop is repeated n times.

Each operation has a complexity O(1).

So, in for n number of operations; the complexity is: O(n * 1)

This gives: O(n)

Other Questions