Example 2: - Write A Loop To Set Sum 1 + 2 + + N and Prove That It Is Correct
The document describes how to write a loop to calculate the sum of the first n natural numbers and prove that the loop is correct. It defines the loop invariant to be "sum = 1+2+...+k-1", where k is the current iteration variable. The loop continues while k is not equal to n+1. The initialization sets sum to 0, k to 1, and requires that n is greater than or equal to 0. The proof shows that the loop invariant and termination condition ensure the postcondition of "sum = 1+2+...+n" is satisfied upon exit.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
76 views8 pages
Example 2: - Write A Loop To Set Sum 1 + 2 + + N and Prove That It Is Correct
The document describes how to write a loop to calculate the sum of the first n natural numbers and prove that the loop is correct. It defines the loop invariant to be "sum = 1+2+...+k-1", where k is the current iteration variable. The loop continues while k is not equal to n+1. The initialization sets sum to 0, k to 1, and requires that n is greater than or equal to 0. The proof shows that the loop invariant and termination condition ensure the postcondition of "sum = 1+2+...+n" is satisfied upon exit.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8
Example 2
• Write a loop to set sum = 1 + 2 + … + n and prove that it
is correct.
• We have our postcondition: {sum = 1 + 2 + … n}
• {pre: ____________} • [Initialization] • {loop-inv: ____________} • while (B:__________) { • {loop-inv: ____________} • sum = sum + k; • {_________________} • k = k+1; • {loop-inv: ____________} • } • {loop-inv: ____________} • {post: sum = 1 + 2 + … + n} • What should our loop invariant be? In the loop body, we add k to sum and increment k. This suggests that sum should be 1+2+…+k-1 going into the loop body. Let’s call this assertion {I}. • {sum = 1+2+…+k-1} • sum = sum + k; • {sum = 1+2+…+k} • k = k+1; • {sum = 1+2+…+k-1} • Now we need B. Remember that we want the property {I Λ !B} → {Q}, or • {sum = 1+2+…+k-1 Λ !B} → {sum = 1 + 2 + … + n}.
• This logic falls neatly into place if we let !B be {k-1 = n}.
So then B = !!B is simply • {!(k-1 = n)} → {k-1 != n} → {k != n+1} • {pre: _______} • [Initialization] • {loop-inv: sum = 1+2+…+k-1} • while (k != n+1) { • {loop-inv: sum = 1+2+…+k-1} • sum = sum + k; • {sum = 1+2+…+k} • k = k+1; • {loop-inv: sum = 1+2+…+k-1} • }{ • loop-inv: sum = 1+2+…+k-1} • {sum = 1+2+…+k-1 Λ k = n+1} → {post: sum = 1 + 2 + … + n} • What about the initialization of k, sum, n etc. • {pre: n >= 0} • sum = 0; {n >= 0} k = 1; • {loop-inv: sum = 1+2+…+k-1} • {n >= 0 Λ sum = 1+2+…+k-1} • while (k != n+1) { • {loop-inv: sum = 1+2+…+k-1} • sum = sum + k; • {sum = 1+2+…+k} • k = k+1; • {loop-inv: sum = 1+2+…+k-1} • } • {loop-inv: sum = 1+2+…+k-1} • {sum = 1+2+…+k-1 Λ k = n+1} → {post: sum = 1 + 2 + … + n}