Question: ( a ) ( 1 0 0 pts ) Consider the following algorithm, and assume we have a function log 2 ( x ) which

(a)(100 pts) Consider the following algorithm, and assume we have a function log2(x) which calculates
floor(log2
(x)) in \Theta (log x) time. Describe what function g1 computes on input positive integers a and
b, determine a tight \Theta bound on its runtime, and argue that it the runtime correct.
1: function g1(a, b)
2: t =1
3: s =0
4: lgb = log2(b)
5: for i =0 to lgb do
6: if b t = t then
7: s += a t
8: t =2
9: return s
(b)(80-120 pts) Modify the algorithm above into a function g2(a, b) which computes pow(a, b). You can receive
up to 80 points for a \Theta (b) algorithm and 120 points for a \Theta (log b) algorithm.
(c)(50pts) Give an algorithm which computes log2(b) on input positive integer b, in \Theta (log b) time, and argue
that both the algorithm and the runtime are correct.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!