Question

Dynamic Programming. Consider the Fibonacci series F(0) = 1; F(1)=1; F(n)=F(n-1)+F(n-2) Define the Dynamic Programming algorithm...

Dynamic Programming.

Consider the Fibonacci series

F(0) = 1; F(1)=1; F(n)=F(n-1)+F(n-2)

Define the Dynamic Programming algorithm for calculating F(5) . How does it compare with the recursive algorithm?

in java

Homework Answers

Answer #1

Dynamic Program for fibonacci series:

class Main{
    public static void main(String args[])  
    {    
     int n1=1,n2=1,n3,i,n=5;    
     System.out.print(n1+" "+n2);//printing 0 and 1    
        
     for(i=2;i<=n;++i)//loop starts from 2 because 0 and 1 are already printed    
     {    
        n3=n1+n2;    
        System.out.print(" "+n3);    
        n1=n2;    
        n2=n3;    
     }    
      
    }
}  

Output:

Using Recursion:

class Main{
    public static int fibonacci(int n){
        if(n == 0 || n == 1){
            return 1;
        }
      
        return fibonacci(n-1) + fibonacci(n -2); //tail recursion
    }
    public static void main(String args[])  
    {    
     for(int i=0; i<=5; i++){
            System.out.print(fibonacci(i) +" ");
        }
    }
}  

Output:

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
Consider the following recursive algorithm Algorithm S(n) if n==1 return 1 else return S(n-1) + n*n*n...
Consider the following recursive algorithm Algorithm S(n) if n==1 return 1 else return S(n-1) + n*n*n 1)What does this algorithm compute? 2) Set up and solve a recurrence relation for the number of times the algorithm's basic operation is executed. 3) How does this algorithm compare with the non-recusive algorithm for computing thius function in terms of time efficeincy and space effeciency?
Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1]...
Consider the following recursive algorithm. Algorithm Test (T[0..n − 1]) //Input: An array T[0..n − 1] of real numbers if n = 1 return T[0] else temp ← Test (T[0..n − 2]) if temp ≥ T[n − 1] return temp else return T[n − 1] a. What does this algorithm compute? b. Set up a recurrence relation for the algorithm’s basic operation count and solve it.
Consider the following function : F 1 = 2, F n = (F n-1 ) 2...
Consider the following function : F 1 = 2, F n = (F n-1 ) 2 , n ≥ 2 i. What is the complexity of the algorithm that computes F n using the recursive definition given above. ii. Describe a more efficient algorithm to calculate F n and give its running time.
Given: function f = fibonacci(n) % FIBONACCI Fibonacci sequence % f = FIBONACCI(n) generates the first...
Given: function f = fibonacci(n) % FIBONACCI Fibonacci sequence % f = FIBONACCI(n) generates the first n Fibonacci numbers. f = zeros(n,1); f(1) = 1; f(2) = 2; for k = 3:n f(k) = f(k-1) + f(k-2); end AND function f = fibnum(n) if n <=1 f =1; else f = fibnum(n-1) + fibnum(n-2); end In Matlab, modify fibonacci.m and fibnum.m to compute the following sequence. dn = 0, n<=0 d1 = 1, d2 = 1 and, for n >...
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
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1)...
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1) = 1; f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
java data structure question - Consider the following recurrence function" f(1)=1; f(2)=2; f(3)=3; f(4)=2; f(5)=2 f(n)...
java data structure question - Consider the following recurrence function" f(1)=1; f(2)=2; f(3)=3; f(4)=2; f(5)=2 f(n) = 2 * f(n -1) + f(n – 5) for n >= 6 Write a program to demonstrate this recurrence relation (recursive method) and demonstrate it for the values n = 6, 7, 10, and 12; Create a second method that solves the same problem using an iterative solution. Modify your test program to show that both the recursive method and the iterative methods...
Question 4: The function f : {0,1,2,...} → R is defined byf(0) = 7, f(n) =...
Question 4: The function f : {0,1,2,...} → R is defined byf(0) = 7, f(n) = 5·f(n−1)+12n2 −30n+15 ifn≥1.• Prove that for every integer n ≥ 0, f(n)=7·5n −3n2. Question 5: Consider the following recursive algorithm, which takes as input an integer n ≥ 1 that is a power of two: Algorithm Mystery(n): if n = 1 then return 1 else x = Mystery(n/2); return n + xendif • Determine the output of algorithm Mystery(n) as a function of n....
Please Write the whole program in assembly i am able to calculate the fibonacci series but...
Please Write the whole program in assembly i am able to calculate the fibonacci series but not sure how to print it in reverse order. Please give a Complete code !! Programming Exercise 1 (10 points): [call it Ass2-Q1.asm] Write an ASM program that reads an integer number N and then displays the first N values of the Fibonacci number sequence, described by: Fib(0) = 0, Fib(1) = 1, Fib(N) = Fib(N-2) + Fib(N-1) Thus, if the input is N...
Do the following problem by developing a dynamic programming algorithm for it: Describe efficient algorithms for...
Do the following problem by developing a dynamic programming algorithm for it: Describe efficient algorithms for the following variants of the text segmentation problem. Assume that you have a subroutine IsWord that takes an array of characters as input and returns True if and only if that string is a “word”. Analyze your algorithms by bounding the number of calls to IsWord. Given an array A[1 .. n] of characters, compute the number of partitions of A into words. For...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT