Question

Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...

Complete java program below.

Complete non-recursive version nthFibonacciWithLoop() method.

Complete recursive version nthFibonacciWithRecursion() method.

public class Fibonacci {

// Fib(N): N N = 0 or N = 1

// Fib(N-1) + Fib(N-2) N > 1

// For example,

// Fib(0) = 0

// Fib(1) = 1

// Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1

// Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1 =

2

// ...

// Fib(10) = (0), 1, 1, 2, 3, , 5, 8, 13, 21, 34, 55, ...

// Return the nth Fibonacci number.

public static int nthFibonacciWithLoop(int nth) {

}

public static int nthFibonacciWithRecursion(int nth) {

}

// Check first 30 Fibonicci numbers.

private static void check() {

int [] check = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,

610, 987, 1597, 2584, 4181, 6765};

boolean correctLoop = true, correctRecursion = true;

for (int i = 0; i < 20; i++) {

if (nthFibonacciWithLoop(i) != check[i])

correctLoop = false;

if (nthFibonacciWithRecursion(i) != check[i])

correctRecursion = false;

}

if (correctLoop && correctRecursion)

System.out.println("\nYou got them right!");

else {

if (!correctLoop)

System.out.println("\nCheck nthFibonacciWithLoop()");

if (!correctRecursion)

System.out.println("\nCheck nthFibonacciWithRecursion()");

}

}

public static void main(String [] args) {

System.out.print("Enter N: ");

int n = new java.util.Scanner(System.in).nextInt();

for (int i = 0; i <= n; i++) {

System.out.print(nthFibonacciWithLoop(i) + " ");

}

System.out.println();

for (int i = 0; i <= n; i++) {

System.out.print(nthFibonacciWithRecursion(i) + " ");

}

check();

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

public class Fibonacci
{
// Fib(N): N N = 0 or N = 1
// Fib(N-1) + Fib(N-2) N > 1
// For example,
// Fib(0) = 0
// Fib(1) = 1
// Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1
// Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1 =2
// ...
// Fib(10) = (0), 1, 1, 2, 3, , 5, 8, 13, 21, 34, 55, ...
// Return the nth Fibonacci number.
public static int nthFibonacciWithLoop(int nth)
{
int a = 0, b = 1, c;
if (nth == 0)
return a;
for (int i = 2; i <= nth; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
public static int nthFibonacciWithRecursion(int nth)
{
if (nth <= 1)
return nth;
return nthFibonacciWithRecursion(nth - 1) + nthFibonacciWithRecursion(nth - 2);
}
// Check first 30 Fibonicci numbers.
private static void check()
{
int [] check = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
610, 987, 1597, 2584, 4181, 6765};
boolean correctLoop = true, correctRecursion = true;
for (int i = 0; i < 20; i++)
{
if (nthFibonacciWithLoop(i) != check[i])
correctLoop = false;
if (nthFibonacciWithRecursion(i) != check[i])
correctRecursion = false;
}
if (correctLoop && correctRecursion)
System.out.println("\nYou got them right!");
else
{
if (!correctLoop)
System.out.println("\nCheck nthFibonacciWithLoop()");
if (!correctRecursion)
System.out.println("\nCheck nthFibonacciWithRecursion()");
}
}
public static void main(String [] args)
{
System.out.print("Enter N: ");
int n = new java.util.Scanner(System.in).nextInt();
for (int i = 0; i <= n; i++)
{
System.out.print(nthFibonacciWithLoop(i) + " ");
}
System.out.println();
for (int i = 0; i <= n; i++)
{
System.out.print(nthFibonacciWithRecursion(i) + " ");
}
check();
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just need to add comment for the code blow. Pleas add comment for each method...

    I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. *    * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • I cannot get this to work in IntelliJ import java.util.Scanner; public class Fibonacci { public static...

    I cannot get this to work in IntelliJ import java.util.Scanner; public class Fibonacci { public static int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); System.out.println(fibonacci(n)); in.close(); } }

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • 1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class....

    1.Take this recursive Fibonacci implementation and convert it into the caching based version discussed in class. Implement your caching to store a maximum of 5 values. Create 2 variations: one that stores all values and one that only stores even values. Make sure that you don't leave any gaps in your cache — if you have 5 cache entries you must cache 5 unique and valid values. Compare the caching implementations to the recursive implementation using the time utility. How...

  • 3. Determine the general result (in terms of n) of the following recursive function (4 pts.)...

    3. Determine the general result (in terms of n) of the following recursive function (4 pts.)               public static void func2(int n)      {         if(n == 1)            System.out.println(“*”);         else         {            for (int i = 1; i <= n; i++)               System.out.print(“*”);            System.out.println();            func2(n-1);                      }               } 4. Does func2 above perform down or bottom up computation? ___________________ (2 pts.)

  • In JAVA Thank You What is the exact output produced by running the method test? /**...

    In JAVA Thank You What is the exact output produced by running the method test? /** * TestSwap.java * Demonstrates parameter passing involving arrays and integers, * scope of variables, flow of control, and overloaded methods. */ public class TestSwap { public void swap (int x, int y) { int temp; temp = x; x = y; y = temp; System.out.println("Inside swap version 1:"); System.out.println("x = " + x); System.out.println("y = " + y); } public void swap (int[] a,...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • Let’s work together to develop a call tree for the execution of the following recursive method....

    Let’s work together to develop a call tree for the execution of the following recursive method. (The method allows us to recursively generate the nth integer in the Fibonacci sequence, although you don’t need to be familiar with that sequence to understand this problem.) public static int fib(int n) { if (n == 0 || n == 1) { return 1; } else { int prev1 = fib(n - 2); int prev2 = fib(n - 1); return prev1 + prev2;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT