I'm having a hard time correctly identifying a loop invariant for the following function:
F(y)
X <-- 1
while (y > 1)
do x <-- x * y
y <-- y - 1
return (x)
I've identified the loop invariant to be x = 1 OR x = y!
as that statement holds true as a pre-condition and holds true as a post-condition.
It doesn't seem to hold true for every iteration, as for example if y = 3, then on the first iteration of the loop, x = 1 * 3 which equates to 3 and NOT 3! which equates to 6.
This is where my confusion really is I guess. Some books articles state that a loop invariant is a statement that must equate to true at the beginning or a loop (thus the precondition) and must also hold true at the end of the loop (thus post-condition) but does not necessarily need to hold true part-way through the loop.
What is the correct loop invariant for the function above?