Question

In: Computer Science

The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13,...

The Fibonacci sequence is the series of integers

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . .

See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence:

Fib(N) = {N, if N = 0 or 1

Fib(N - 2) + Fib(N - 1), if N > 1

a) Write a recursive method fibonacci that returns the nth Fibonacci number when passed the argument n.

b) Write a non recursive version of the method fibonacci.
c) Write a driver to test your two versions of the method fi‐
bonacci.
d) Compare the recursive and iterative versions for efficiency.
(Use words, not O( ) notation.)

Use Java programming.

Solutions

Expert Solution

//Fibonacci.java
import java.util.Scanner;

public class Fibonacci {
    public static int FibR( int n){
        if(n == 0){
            return 0;
        }
        if(n == 1){
            return 1;
        }
        else{
            return FibR(n-1) + FibR(n-2);
        }
    }

    public static int FibI( int n){
        int a = 0,b = 1,c = 1;
        if(n == 0){
            return a;
        }
        if(n == 1){
            return b;
        }
        else{
            while(n>1){
                c = a + b;
                a = b;
                b = c;
                n--;
            }
            return c;
        }
    }

    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int n;
        System.out.println("Input an integer for the index of a term in the Fibonacci sequence:");
        n = scanner.nextInt();
        System.out.println("\nthe "+(n)+"-th term \""+FibR(n)+"\" in the Fibonacci sequence can be computed recursively.");
        System.out.println("the "+(n)+"-th term \""+FibI(n)+"\" in the Fibonacci sequence can be computed recursively.");
    }
}

Time complexity of recursive version is O(2^n)
Time complexity of iterative version is O(n)

Related Solutions

The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally,...
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,.... Formally, it can be expressed as: fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2 Write a multithreaded C++ program that generates the Fibonacci series using the pthread library. This program should work as follows: The user will enter on the command line the number of Fibonacci numbers that the program will generate. The program will then create a separate thread that will...
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the...
The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 … begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Write a non-recursive function Fibonacci (n) that calculates the nth Fibonacci number. Write a program to display a table of terms and the Fibonacci number in two columns for the first 15 terms, using the function you created.
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are...
The Fibonacci Sequence is a series of integers. The first two numbers in the sequence are both 1; after that, each number is the sum of the preceding two numbers. 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... For example, 1+1=2, 1+2=3, 2+3=5, 3+5=8, etc. The nth Fibonacci number is the nth number in this sequence, so for example fibonacci(1)=1, fibonacci(2)=1, fibonacci(3)=2, fibonacci(4)=3, etc. Do not use zero-based counting; fibonacci(4)is 3, not 5. Your assignment...
Assume that there are a sequence of consecutive integers 1, 2, 3, 4, 5, ... 15....
Assume that there are a sequence of consecutive integers 1, 2, 3, 4, 5, ... 15. Tom and Jim respectively select a number from the sequence randomly (no repetition). Given that Tom’s number is divisible by 5, what’s the probability that Tom’s number is greater than Jim’s number ?
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5...
DATA 3 8 2 15 2 2 0 0 4 5 2 7 0 1 5 3 0 2 5 4 1 6 9 5 3 1 2 10 6 1 1 2 1 19 6 6 6 7 0 4 1 1 1 0 1 9 2 2 2 1 16 10 10 5 2 3 1 4 4 4 3 6 2 8 5 2 7 1 6 4 0 3 1 1 1 Background: A group of...
Consider the following time series. t 1 2 3 4 5 yt 6 10 8 13...
Consider the following time series. t 1 2 3 4 5 yt 6 10 8 13 15 (a) Choose the correct time series plot. (i) (ii) (iii) (iv) What type of pattern exists in the data? (b) Use simple linear regression analysis to find the parameters for the line that minimizes MSE for this time series. If required, round your answers to two decimal places. y-intercept, b0 = 4.1 Slope, b1 = 2.1 MSE = ???? (c) What is the...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6...
x (Bins) frequency 0 0 1 0 2 0 3 2 4 5 5 8 6 13 7 33 8 42 9 66 10 77 11 105 12 103 13 110 14 105 15 84 16 70 17 51 18 40 19 27 20 27 21 15 22 5 23 7 24 2 25 2 26 1 27 0 28 0 29 0 30 0 (7) On the Histogram worksheet, calculate all frequencies of the distribution using the table shown....
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8...
A = (1 −7 5 0 0 10 8 2 2 4 10 3 −4 8 −9 6) (1) Count the number of rows that contain negative components. (2) Obtain the inverse of A and count the number of columns that contain even number of positive components. (3) Assign column names (a,b,c,d) to the columns of A. (4) Transform the matrix A into a vector object a by stacking rows. (5) Replace the diagonal components of A with (0,0,2,3). Hint:...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1...
6 5 4 5 0 0 13 48 6 1 0 7 2 0 1 1 0 2 11 5 11 27 4 0 6 Create Standard Deviation Chart (Normal Distribution Curve)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT