Question: Hi, can you help me with some program correctness questions.I'm doing exercises from my textbook but not sure if answer is correct. for a) precondition

Hi, can you help me with some program correctness questions.I'm doing exercises from my textbook but not sure if answer is correct. for a) precondition is: a,b are integers and b is >=0. post: returns a^b. for correctness, path 1 terminates at 3. a^0 = 1 so its true. for path 2, it terminates at 6. when it enters the recursive call, b is of the form 2k and a is a number. so the recursive call holds and it holds. same for path 3. would this be correct for a?
For b, im not sure what the appropriate loop variant and invariant should be. is there any method of determining what the correct one would be.
7. In this question, we study two different algorithms for exponen- tiation: a recursive and iterative algorithm. First, state pre- and postconditions that the algorithms must satisfy (they're the same for the two). Then, prove that each algorithm is correct according to the specifications. 1 def exp rec(a, b): return 1 4 else if b mod 2 x exp.rec(a, b / 2) return xx 7 else: x = exp.rec (a, (b return xx a 1)/2) - 1 def exp iter(a, b): 2 ans 1 mult = a INTRODUCTION TO THE T while exp > : if exp mod 21: ans mult mult mult mult exp exp /1 2 10 return ans 7. In this question, we study two different algorithms for exponen- tiation: a recursive and iterative algorithm. First, state pre- and postconditions that the algorithms must satisfy (they're the same for the two). Then, prove that each algorithm is correct according to the specifications. 1 def exp rec(a, b): return 1 4 else if b mod 2 x exp.rec(a, b / 2) return xx 7 else: x = exp.rec (a, (b return xx a 1)/2) - 1 def exp iter(a, b): 2 ans 1 mult = a INTRODUCTION TO THE T while exp > : if exp mod 21: ans mult mult mult mult exp exp /1 2 10 return ans
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
