For this pseudocode present a loop invariant and prove it, n >= 0.
i <= 0;
s <=2;
while i < n do
i <= i + 1
s <= s * s
The best loop invariant for this while loop is n-i > 0. Because the loop executes when the value of i is less than n. It terminates once when the i value becomes equal to n.
For example, if n = 5. i = 0 starts with 0.
Iteration 1 : n > i , i = 0, 5 - 0 > 0
Iteration 2 : n > i , i = 1, 5 - 1 > 0
Iteration 3 : n > i , i = 2, 5 - 2 > 0
Iteration 4 : n > i , i = 3, 5 - 3 > 0
Iteration 5 : n > i , i = 4, 5 - 4 > 0
Iteration 6 : n > i , i = 5, 5 - 5 = 0 --> Loop terminates.
From iteration 1 to 5 the loop invariant n - i > 0 holds.
**Comment for any further queries.
For this pseudocode present a loop invariant and prove it, n >= 0. i <= 0;...
(a) Prove the following loop invariant by induction on the
number of loop iterations: Loop Invariant: After the kth iteration
of the for loop, total = a1 + a2 + · · · + ak and L contains
all elements from a1 , a2 , . . . ,
ak that are greater than the sum of all previous terms of the
sequence.
(b) Use the loop invariant to prove that the algorithm is
correct, i.e., that it returns a...
Use the loop invariant (I) to show that the code below correctly computes the product of all elements in an array A of n integers for any n ≥ 1. First use induction to show that (I) is indeed a loop invariant, and then draw conclusions for the termination of the while loop. p = a[0] i = 0 while i <= n − 1 do //(I) p = a[0] · a[1] · · · a[i] (Loop Invariant) i +...
To prove the correctness of the following:
# pre: int >= 1
fac = 1
i = n
while i>0:
fac = fac*i
i = i-1
# post: fac = n!
which loop invariant do we need?
(Hint, you will need two statements that are both
true. One about fac and one about i)
Please provide both full statements as well as which
loop invariant is needed in a full proof.
2. To prove correctness of the following:...
Use a loop invariant to prove that the following algorithm correctly identifies the location of the minimum value in the array data. Input: data: array of integers Input: n: size of data Output: index min such that data[min] <= data[i] for any i from 1 to n Algorithm: FindMin min = 1; for i = 2 to n do if data[i] < data[min] then min = i; end end return min
Prove procedure to compute Fibinocci(n) where F0 = 0, F1 = 1, Fn = Fn-2 + Fn-1. Prove by establishing and proving loop invariant then using induction to prove soundness and termination. 1: Procedure Fib(n) 2: i←0,j←1,k←1,m←n 3: while m ≥ 3 do 4: m←m−3 5: i←j+k 6: j←i+k 7: k←i+j 8: if m = 0 then 9: return i 10: else if m = 1 then 11: return j 12: else 13. return k
Prove procedure to compute Fibinocci(n) where F0 = 0, F1 = 1, Fn = Fn-2 + Fn-1. Prove by establishing and proving loop invariant then using induction to prove soundness and termination. 1: Procedure Fib(n) 2: i←0,j←1,k←1,m←n 3: while m ≥ 3 do 4: m←m−3 5: i←j+k 6: j←i+k 7: k←i+j 8: if m = 0 then 9: return i 10: else if m = 1 then 11: return j 12: else 13. return k
What is the loop INVARIANT for this program? The loop variant
has to pass the premises:
loop {P) [U] while (b) (Sj1 S fQ) (0 S len)) i=0; r false; while (i < len) { if (a ] r) { r = true; i = len; else r true)
loop {P) [U] while (b) (Sj1 S fQ)
(0 S len)) i=0; r false; while (i
Pseudocode (to implement Algorithm); Loop Invariant
4. (15 pts) There is an algorithm called array doubling that is used to increase the size of an array to accommodate new data when an array fills up. In the algorithm, when an array fills up, a new array is created dynamically that is 2x the size of the original, the data is copied to the new array, and the old array is destroyed (a) Write an algorithm to implement an underflow strategy...
For the while loop below, use the loop invariant given to show that if the pre-condition is the loop.In each step, clearly state true before the loop then the post-condition is true after what facts are assumed and what facts will be proven. (computes the sum of a list of numbers.) fa ,an While (<n) sum := sum + am j j+1 End-while ° Pre-condition: j- 1, sum a, n is a positive integer, a,,.. a, is a list of...
Argue the correctness of Heapsort using the following loop invariant for the second loop in the algorithm (after the array has been heapified): "At the start of each iteration of the for loop, the subarray A[0 ... i-1] is a max-heap containing the i smallest elements of the original array, and the subarray A[i ... n-1] contains the n-i largest elements of the original array in sorted order