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.
# pre: int >= 1
fac = 1
i = n
while i>0:
fac = fac*i
i = i-1
# post: fac = n!
statement 1:loop invariant would be fac*(i!) = n! where n is the initial value of i that is passed to the function. This statement is always true, no matter how many iterations of the loop have already been done.
statement 2: in each iteration value of i reduced by 1 till it
reaches 0
proof:
Precondition :
i = n ,fac=1
fac*(i!) = 1*(n!) = n!
Maintenance :
in each iteration fac = fac*i
and i=i-1;
(fac*i)*((i-1)!) = n!
Termination :
fac = 1*2*3*4*.........*(n-1)*n = n!
To prove the correctness of the following: # pre: int >= 1 fac = 1 i...
Hey, could someone help me check my answers with these please :)
Thank you
Which of the following does the choice rule not allow you to do? Take any pre-condition of the if: construction as a pre-condition of the if: and else: blocks Take the condition of the if statement a a pre-condition of the if: block Take (P) as a post-condition of the whole if: construction if (Py is a post-condition of both the if: and else: blocks Take...
6. [8 marks Consider the sequence 10, 11, ... defined by To = 0, 11 = 1, and ri = i-1 + 24-2 for all i > 2. Consider the following algorithm that attempts to compute the value In, given an n > 0. Algorithm 6 1: //pre: (n e Z) ^ (n > 0) 2: if n == 0 then 3: bro 1: else 5: a 0 6: 5+ 1 it1 //LoopInv: while i<n do ct a+b atb bc...
(15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...
Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0 and an array A[0..(n - 1)] of n integers. Output: The smallest index s such that A[s] is the largest value in A[0..(n - 1)]. For example, if n = 9 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 1, since the largest value in...
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...
double sinLoops (double x) { int i = 7; //this didn't exist. double result = 1/(m+1) // line 3 double pi=3.14; //You need the individual taylor series element. do { double num = x; //these need to be reset each time. double fac = 1; //if not they overflow and infinity/infinity is NaN and it exits. for (int counter = 1; counter < (2 * i + 1); counter++) { } (2 * i + 1); counter2 >= 1; counter2--)...
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
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
Consider the code below int s =1; for (int i=1; i<N; i++){ //Assertion: s = s = s+(2*i+1);} printf("%d", s); a) What is a reasonable assertion for the main loop? b) What does the code accomplish? c) Using the method of initalization-maintenance-termination, prove that the invariant holds, and that the final output is correct.
An array A of n integers is called semi-sorted if it is increasing until some index and then decreasing afterwards. In other words, there is some index 1 ≤ p ≤ n such that: • A[i] < A[i + 1] for 1 ≤ i < p, and • A[i] > A[i + 1] for p ≤ i < n. Note that in this case, A[p] is the maximum element of A. Give an algorithm with running time O(logn) that finds...