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.
source
share