6. [8 marks Consider the sequence 10, 11, ... defined by To = 0, 11 =...
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...
6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...
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:...
(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...
Copy of Consider the following algorithm: i+2 while (x mod i)=0 do iti+1 Now suppose x is an element from the set {n EN|2sn s50). What is the worst-case number of comparisons that this algorithm will perform? O O O O O O
Solve and show work for problem 8 Problem 8. Consider the sequence defined by ao = 1, ai-3, and a',--2an-i-an-2 for n Use the generating function for this sequence to find an explicit (closed) formula for a 2. Problem 1. Let n 2 k. Prove that there are ktS(n, k) surjective functions (n]lk Problem 2. Let n 2 3. Find and prove an explicit formula for the Stirling numbers of the second kind S(n, n-2). Problem 3. Let n 2...
Consider the sequence defined by o = 0 and 2 = 2 +(-1)"' i l for n EN Find an expression for in standard form, then prove that your formula is correct for all integers n > 0.
Loop Invariants. I need help on the 15 point question. The 10 point question is the "procedure" from the previous question. 2: (10 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an array A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A[j] i=j-1 while i > 0) and A[i] > key do A[i+1] = A[i] i=i-1 A[i+1] = key 3: 4: 5: 6:...
Consider the following algorithm Poly(A,a) --------------- 1. n = degree of polynomial (with coef A[n],..,A[0]) 2. sum = 0 3. for i = n downto 0 4. sum = sum * a +A[i] show all steps!! (a) Determine the running time of the algorithm, your work should explain your answer (b) what is the loop invariant property of the loop in line 3.
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