Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Algorithm and Complexity, Python Task 1a) Write a program to test and compare the time used for exponentiation by expt, it_exp and better_exp on your

Algorithm and Complexity, Python

Task 1a) Write a program to test and compare the time used for exponentiation by expt, it_exp and better_exp on your system.

b) Estimate the complexity of all the functions.

def it_exp(b, n):

return exp_it(b, n, 1)

def exp_it(b, count, prod):

if count == 0:

return prod

else:

return exp_it(b, count - 1, b*prod)

def square(x):

return x*x

def better_exp(b, n):

if n == 0:

return 1

elif (n % 2 == 0):

return square(better_exp(b, n // 2))

else:

return b*better_exp(b, n-1)

image text in transcribed

[] def it_exp(b, n): return exp_it(b, n, 1) def exp_it(b, count, prod): if count == 0: return prod else: return exp_it(b, count - 1, b*prod) it_exp(2, 3) 8 [] def square(x): return x x def better_exp(b, n): if n == 0: return 1 elif (n % 2 == 0): return square(better_exp(b, n // 2)) else: return b*better_exp(b, n-1) [] better_exp(2, 3) 8 [] # Task 1a) Write a program to test and compare time used for exponentiation by expt, it_exp and better_exp on your system. b) Estimate the complexity of all of the above functions. #

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions

Question

Find the 14th term of the arithmetic sequence 3,

Answered: 1 week ago