0

So I have an upcoming test about proofs and I was looking through some practice questions. I came across a question which I don't understand the answer to, and I was hoping I could get some input.

Consider the following algorithm:

Factorial(n: integer > 0)
temp = 1
for i = 1 to n do
        temp = temp * i
return temp

Write down the loop invariant for the loop in this algorithm.

The answer marked as correct is:

before loop, temp = i = 1

Why is this correct? I'm struggling to understand how it proves the validity of the algorithm. Here is what I would have put, which I am also uncertain about:

before each loop, temp = (i - 1)!

Thanks in advance.

0 Answers0