Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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_2

Step: 3

blur-text-image_3

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

=+. What steps from RMF resonate most with you?

Answered: 1 week ago