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();
}
}
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.
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
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:...
The following is a correct implementation of a non-recursive method to find the nth Fibonacci number in Java: public static long fib(int n){ long[ ] fibNums = new long[n + 1]; fibNums[0] = 0; finNums[1] = 1; for(int i = 2; i <= n; i++){ fibNums[i] = fibNums [i - 1] + finBums[i - 2]; } return fibNums[n]; } I am having trouble understanding the code. Why the creation of the long array make this...
In java need help with the TODO sections creating recursive methods public class CountUpDown { /** * countUp - a recursive function that counts up from 1 to n * * @param n the integer value to count up to */ private static void countUp(int n) { // TODO PRELAB // IMPLEMENT THIS RECURSIVE METHOD } /** * countDown - a recursive function that counts down from n to 1 * * @param n the integer value to count down...
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 = 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...
Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *fib(void *arg); int main(int argc, char **argv){ int n = atoi(argv[1]); printf("%d\n", (int)fib(n)); } void *fib(void *arg){ int n; pthread_t thread1; pthread_t thread2; void *a; void *b; int c; n = (int)arg; if (n <= 0) return 0; if (n == 1) return 1; pthread_create(&thread1, NULL, fib, n-1); ...
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(); } }
This java code won't run and I can't figure out the problem with it. Please help import java.io.*; import java.util.*; import java.math.*; import java.util.Scanner; public class Fibonacci { // Returns n-th Fibonacci number static BigInteger fib(int n) { BigInteger[] Fibo = new BigInteger[n+2]; Fibo[0] = BigInteger.ZERO; Fibo[1] = BigInteger.ONE; for (int j=2 ; j<=n ; j++) { Fibo[j] = Fibo[j-1].add(Fibo[j-2]); } return (Fibo[n]); }...
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. 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...