How to choose and set the correct loop invariant?

I was asked the following problem about loop invariants:

Consider

x=4; for(i=5;i<k;i++) { a=a+x+i; x=x*2; }

Define a suitable loop invariant that uses a closed formula for a and x.

Now, how do you know that you have the right cycle? I mean, you can set the loop invariant: "At the jth iteration," x "is less than" a ", which will be correct, but will not use any closed formulas to the right?

How can I use the operator (loop invariant) to determine the value of "a" at the end of the loop? All the examples that I saw just describe the loop invariant and are not used as a closed formula.

Any ideas?

Thank you for your help.

+3
source share
1 answer

: x (j) [ j 0 k-5], x j- , x(j+1) = x(j) * 2, x(j) 4 * 2**j ( ** " " ), x(0) 4; a(j+1) = a(j) + x(j) + j + 5 - , a(0), a(j) a(0) + -. , , x(j)? - Concrete Mathematics - , IS...?

a(j), , x(j), , , ...?

+2
source

Source: https://habr.com/ru/post/1724583/


All Articles