{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SolHW3f07

# SolHW3f07 - HW3F07 CS336 1 The following program computes...

This preview shows pages 1–3. Sign up to view the full content.

HW3F07 CS336 1. The following program computes the quotient q and remainder r of the division of x by y . { 0 x 0 < y } q, r := 0 , x ; do r y r, q := r - y, q + 1 od { 0 r < y q * y + r = x } What would you have to prove to show that 0 < y 0 r q * y + r = x is an invariant of the loop? Set up a fully instantiated version of the predicate to be proved true (but don’t prove). How would you show that if the loop terminates the postcondition is established? Again, set up a fully instantiated version of the predicate to be proved, but don’t prove it. Solution: Let P be 0 < y 0 r q * y + r = x . To show that P is an invariant, we need to show the following two things: P holds before loop execution begins. Fully instantiated version: 0 x 0 < y wp (” q, r := 0 , x , 0 < y 0 r q * y + r = x ) P holds after each loop iteration. Fully instantiated version: 0 < y 0 r q * y + r = x r y wp (” r, q := r - y, q +1” , 0 < y 0 r q * y + r = x ) To show that the postcondition is established if the loop terminates, we need to show that P ∧ ¬ BB R . The fully instantiated version is 0 < y 0 r q * y + r = x r < y 0 r < y q * y + r = x 2. Prove or disprove the correctness of the following algorithm. { Q : 0 < n } i := 1; { P : 1 i n ( p | : i = 2 p ) } do 2 * i n i := 2 * i od { R : 1 i n 2 * i ( p | : i = 2 p ) } Solution: We shall show that the algorithm is correct. The purpose of the algorithm is to find the largest power of 2 that is at most n . We need to show the following three things: 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
a. P holds before loop execution begins: { Q } i := 1 { P } . b. P holds after each loop iteration: { P 2 * i n } i := 2 * i { P } . c. P holds after the loop: P 2 * i > n R . a. { Q } i := 1 { P } < wp, instantiation > 0 < n wp (” i := 1” , 1 i n ( p | : i = 2 p )) < wp > 0 < n 1 1 n ( p | : 1 = 2 p ) < arith: p=0 > 0 < n 1 1 n T < arith , -simplification > 0 < n 1 n < arith, identity > T b.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern