Question: (b) Consider the code below then answer the questions that follow: 1. if n < 2 2. return false 3. while n != 1

(b) Consider the code below then answer the questions that follow: 1.

 

(b) Consider the code below then answer the questions that follow: 1. if n < 2 2. return false 3. while n != 1 4. if n%2!=0 5. return false 6. n = n /2; 7. return true i. (2 points) What does the code above do? ii. iii. iv. (3 points) Write a loop invariant that can be used to prove the correctness of the code above (10 points) Show the invariant you wrote in part ii is true at initialization, maintenance and termination. Then use that fact to prove correctness of the loop. (5 points) For each line of pseudocode above, show next to the line a cost ci for the cost of the line, and an expression for the number of times that line is executed. Based on this, give a formula for the run time T(n) of the function. Based on this cost, estimate a e-bound for T(n).

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!