Question

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 < n; i++) {
            int temp = fib;
            fib += prevFib;
            prevFib = temp;
        }
        return fib;
    }
    //Display and print running time
    public static void dispTime() {
        long startTime = System.nanoTime();
        System.out.println("t = " + (System.nanoTime() - startTime) + " nanosecs.");
    }

    public static void main(String[] args) {
        Fibonacci fib = new Fibonacci();
        Scanner input = new Scanner(System.in);

        System.out.println("Enter a value for n: ");
        int n = input.nextInt();

        System.out.println("Iterative version:");
        long startTime = System.nanoTime();
        System.out.println(fib.fibIterative(n));
        System.out.println("t = " + (System.nanoTime() - startTime) + " nanosecs.");

        System.out.println("Recursive version:");
        startTime = System.nanoTime();
        System.out.println(fib.fibRecursive(n));
        System.out.println("t = " + (System.nanoTime() - startTime) + " nanosecs.");
    }
}

I'm having trouble with the following questions:

3. Analysis:
For your recursive method:
(a) (10 points) What is the worst-case big-O running time with re-
spect 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 re-
spect 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 run-
ning 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 ").

Thank you!

Homework Answers

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
There is a Java program that is missing one recursive function: public class Fibonacci { /*...
There is a Java program that is missing one recursive function: public class Fibonacci { /* / 0 when n = 0 * fib(n) = | 1 when n = 1 * \ fib(n-1)+fib(n-2) otherwise */ public static int fib(int n) { return 0; } /* Fibonacci Test Framework * * Note, this takes a long time to compute fib(44). */ public static void main(String[] args) { int[] input = { 11, 22, 33, 44}; int[] expect = { 89,...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...
Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...
Suppose that you want to add items to an array such that the items are always...
Suppose that you want to add items to an array such that the items are always ordered in ascending order; e.g., [1, 2, 2, 4, 5, 8, 9, 14, 32], and any duplicate values are adjacent to each other. We haven’t talked about sorting algorithms yet, so assume you want to be able to keep the items in the array in order without having to sort them. So for example, suppose you want to add the integer value 7 to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT