33.0k views
4 votes
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.

User Lennysan
by
5.2k points

1 Answer

7 votes

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)

Step-by-step 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)

User Jovita
by
4.9k points