Question

function fib(n) if n=0 then return(1) if n=1 then return(1) last=1 current=1 for i=2 to n...

function fib(n)

if n=0 then return(1)

if n=1 then return(1)

last=1

current=1

for i=2 to n do

temp=last+current

last=current

current=temp

return(current)

Fibonacci numbers are defined as F0=1, F1=1, Fi=Fi-1+Fi-2 for i>

The algorithm of above can also be proven correct using the Loop Invariant method. The proof will first show that it will correctly compute F0 & F1 by virtue of lines 1 and 2, and then show that it will correctly compute Fn, n>1, using the LI technique on the for loop. For this latter part of the correctness proof, complete the Loop Invariant below by filing in the blanks. Then complete the three parts of the rest of the proof.

Loop Invariant:

Before any execution of the for loop of line 5 in which the loop variable i=k, 2≤k≤n, the variable last will contain _______ and the variable current will contain _________.

Initialization:

Maintenance:

Termination:

Homework Answers

Answer #1

Before the execution of the for loop, the variable last contains the (k-2)th Fibonacci number F(k-2) and the variable current contains (k-1)th Fibonacci number F(k-1).

Initialization: Before the first iteration, with i=2, we have current containing F(1)=1 and last containing F(0)=1. Hence, the loop invariant is true.

Maintenance: Let before the iteration of the for loop with i=k, the variable current contains F(k-1) and last contains F(k-2). Then, after the iteration (before the iteration with i=k+1), temp=last+current sets temp to be F(k-1)+F(k-2)=F(k), last=current sets last as F(k-1) and current=temp sets current as F(k). So, the loop invariant remains true.

Termination: The for loop runs till i=n, so the final value of current when i=n is F(n). Since this value is returned, the function successfully computes the nth Fibonacci number.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Assume that the inner loop works correctly. Using a loop-invariant proof for the outer loop, formally...
Assume that the inner loop works correctly. Using a loop-invariant proof for the outer loop, formally prove that your pseudo-code correctly sorts the given array. Be sure that your loop invariant and proof cover the initialization, maintenance, and termination conditions Given code: /** * a[0:n-1] is an array of n elements. * temp is a variable to facilitate exchange. */ BubbleSort(a, n) Begin for k = 1 to n-1 increment by 1 do // where, k is for the pass...
Recall that the Fibonacci numbers are defined by F0 = 0,F1 = 1 and Fn+2 =...
Recall that the Fibonacci numbers are defined by F0 = 0,F1 = 1 and Fn+2 = Fn+1 + Fn for all n ∈N∪{0}. (1) Make and prove an (if and only if) conjecture about which Fibonacci numbers are multiples of 3. (2) Make a conjecture about which Fibonacci numbers are multiples of 2020. (You do not need to prove your conjecture.) How many base cases would a proof by induction of your conjecture require?
*********I need question 6 answered which is from question 5 which is ********* Question 5 :...
*********I need question 6 answered which is from question 5 which is ********* Question 5 : Program Correctness I (1 point) Use the loop invariant (I) to show that the code below correctly computes n P−1 k=0 2k (this sum represents the sum of the first n even integers where n ≥ 1). Algorithm 1 evenSum(int n) 1: p = 2(n − 1) 2: i = n − 1 3: while i > 0 do 4: //(I) p = nP−1...
In mathematical terms, the sequence Fn of Fibonacci numbers is 0, 1, 1, 2, 3, 5,...
In mathematical terms, the sequence Fn of Fibonacci numbers is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….. Write a function int fib(int n) that returns Fn. For example, if n = 0, then fib() should return 0, PROGRAM: C
The Fibonacci series is given by; F0=0, F1=1,F2=1, F3=2,F4=3,…F(i)=F(i-1)+F(i-2) Given that r^2=r+1. Show that F(i) ≥...
The Fibonacci series is given by; F0=0, F1=1,F2=1, F3=2,F4=3,…F(i)=F(i-1)+F(i-2) Given that r^2=r+1. Show that F(i) ≥ r^{n-2}, where F(i) is the i th element in the Fibonacci sequence
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
I have the following program which returns F(n) for the fibonacci sequence both recursively and iteratively:...
I have the following program which returns F(n) for the fibonacci sequence both recursively and iteratively: public class Fibonacci { //Recursive method public int fibRecursive(int n) { //If n is in the 0th or 1st place return n if (n <= 1) return n; return fibRecursive(n - 1) + fibRecursive(n - 2); } //Iterative method public int fibIterative(int n) { if (n <= 1) return n; int fib = 1, prevFib = 1; for (int i = 2; i <...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to 2 2                position=i 3                for j=1 to (i–1) 4                          if A[j]>A[position] then position=j 5                if position ≠ i then 6                          temp=A[i] 7                          A[i]=A[position] 8                          A[position]=temp (4 points) If input A=[12,5,11,6,10,7,9,8], what will A be after the 3rd iteration of the outermost for loop of the Selection-sort algorithm completes? A=[__, __, __, __, __, __, __, __] (8 points) Modify the algorithm to solve the...
For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing...
For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing correctness for the main loop in each of the following programs and briefly argue initialization, preservation, and termination. EXAMPLE PROBLEM: //Function to return the max of an array A Maximum(array of integers A) Local integer integer m m=0 for i = 1 to n if A[i] > m then m = A[i] end function Maximum EXAMPLE SOLUTION: The loop invariant is m = max(0,...
write the flow chart and PsuedoCode 1. Make a peanut butter sandwich. 2. Calculate the value...
write the flow chart and PsuedoCode 1. Make a peanut butter sandwich. 2. Calculate the value of n! Do not use the factorial fn, use the actual recursive definition**. 3. Draw a deck of cards from a standard deck of 52 cards until the queen of hearts is drawn. How many cards did you draw? 4. Fibonacci Numbers. The Fibonacci Numbers are an interesting sequence of positive integers where each value is the sum of the previous two values. We...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT