Question

In: Computer Science

The Fibonacci series of numbers is a favorite of mathematicians, and goes as follows: 1, 1,...

The Fibonacci series of numbers is a favorite of mathematicians, and goes as follows: 1, 1, 2, 3, 5, 8, 13, 21, . . . That is, the first two numbers are 1, and every subsequent number is the sum of the previous two. The mathematical definition as a recursive function is the following.

∀n = 0, 1, 2, . . .

F(n) = { 1 if n <=1

F(n-1) + F(n-2) if n > 1

The closed form of such function is in O(φ n ), where φ is a number called the golden ratio.

1. Coding: Write a Java class Fibonacci that has the following static methods.

(a) (20 points) A recursive method that returns F(n).

(b) (20 points) A non-recursive method that returns F(n).

(c) (10 points) A main method to obtain from the user the index n, and call the methods above one after the other. For each of the method calls, measure and display the running time using the following snippet of code.

...

// store the time now

long startTime = System.nanoTime();

// your method call here

...

// display the time elapsed

System.out.println("t = " +

(System.nanoTime() - startTime) + " nanosecs.");

2. Experiments: (10 points) Measure running times for different values of n and fill a table like the following. (You may need to adjust n to smaller values if it takes too long or your stack overflows, 0 points if you do not have at least 5 columns of measurements.)

n | 0 | 1 | 2 | 4 | 6 | 8 | 16

recursive |   | | | |   | |

non-recursive | | | | | | |

3. Analysis: For your recursive method:

(a) (10 points) What is the worst-case big-O running time with respect to n? (Explain how you computed the running time, 0 points if you do not say what is the basic operation counted and what counting rules you used.)

(b) (10 points) Are your measurements consistent with this big-O running time? (Explain using your measurements, 0 points if you do not say something like "looking at row recursive, when n is doubled the running time is xxx".)

For your non-recursive method:

(c) (10 points) What is the worst-case big-O running time with respect to n? (Explain how you computed the running time, 0 points if you do not say what is the basic operation counted and what counting rules you used.)

(d) 10 points Are your measurements consistent with this big-O running time? (Explain using your measurements, 0 points if you do not say something like "looking at row non-recursive, when n is doubled the running time is xxx".)

Solutions

Expert Solution

  • Coding:

1. Recursive method:

static int fibRec(int n) {
       if (n <= 1) {
           return 1;
       } else {
           return fibRec(n - 1) + fibRec(n - 2);
       }
   }

2. Iterative Method:

static int fibItr(int n) {
       int a, b, c = 1;
       a = 1;
       b = 1;
       for (int i = 2; i <= n; i++) {
           c = a + b;
           a = b;
           b = c;
       }
       return c;
   }

3. Main Method:

private static Scanner s;

   public static void main(String[] args) throws Throwable {
       s = new Scanner(System.in);
       System.out.println("Please input a number:");
       int number = s.nextInt();
       long startTime = System.nanoTime();
       System.out.println(fibRec(number));
       System.out.println("t for Recursive Approach = " + (System.nanoTime() - startTime) + " nanosecs.");
       startTime = System.nanoTime();
       System.out.println(fibItr(number));
       System.out.println("t for Iterative Approach = " + (System.nanoTime() - startTime) + " nanosecs.");
   }

  • Experiments

n        | 0          | 1     | 2        | 4        | 6      | 8         | 16

recursive | 139200 | 141600 | 166000 | 147401 | 132800 | 461800 | 517001

non-recur | 26200 | 28300   | 30400 | 29000 | 26001 | 30100   | 28001


Related Solutions

Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1,...
Python: Using Jupyter Notebook 1. Write code to generate Fibonacci series. Fibonacci numbers – 1, 1, 2, 3, 5, 8, … 2. Check if a number is an Armstrong number A positive integer is called an Armstrong number of order n if abcd... = a^n + b^n + c^n + d^n + ... In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example: 153 = 1*1*1...
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...
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...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The...
Using Python: The Fibonacci sequence is a famous series of numbers with the following rules: The first number in the sequence is 0 - The second number in the sequence is 1 - The other numbers in the sequence are composed by adding up the two previous numbers in the sequence. We therefore have the following sequence: 1 st number: 0 2nd number: 1 3 rd number: 0 + 1 = 1 4 th number: 1+1 =2 5 th number:...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers...
In this assignment, you will calculate and print a list of Fibonacci Numbers . Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones: The sequence Fn of Fibonacci numbers is defined by the recurrence relation:  which says any Nth Fibonacci number is the sum of the (N-1) and (N-2)th Fibonacci numbers. Instructions Your task will be...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence,...
(a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 114, … etc. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. We define Fib(0)=0,...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number...
Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 + 125 + 27 = 153. Namely, 153 is equal to the sum of the cubes of its own digits. Perfect Numbers: A number is said to be perfect if it is the sum of its own divisors (excluding itself). For example, 6 is perfect since 1, 2, and 3 divide evenly into...
Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number...
Problem 1: More than 2500 years ago, mathematicians got interested in numbers. Armstrong Numbers: The number 153 has the odd property that 13+53 + 33 = 1 + 125 + 27 = 153. Namely, 153 is equal to the sum of the cubes of its own digits. Perfect Numbers: A number is said to be perfect if it is the sum of its own divisors (excluding itself). For example, 6 is perfect since 1, 2, and 3 divide evenly into...
The Fibonacci numbers are recursively dened by F1 = 1; F2 = 1 and for n...
The Fibonacci numbers are recursively dened by F1 = 1; F2 = 1 and for n > 1; F_(n+1) = F_n + F_(n-1): So the rst few Fibonacci Numbers are: 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; 89; 144; : : : There are numerous properties of the Fibonacci numbers. a) Use the principle of Strong Induction to show that all integers n > 1 and m > 0 F_(n-1)F_(m )+ F_(n)F_(m+1) = F_(n+m): Solution. (Hint: Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT