Question

In: Computer Science

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();

}

}

Solutions

Expert Solution

import java.util.*;
public class Main
{
public static int nthFibonacciWithLoop(int nth)
{
int i,a,b,s=0;
a=0;//initializing a to 0
b=1;// and b to 1
if(nth==0)//the first value is 0 so it returns 0 for n=0
return a;
for(i=1;i<nth;i++)//loops for other Numbers
{
s=a+b;//adding previous value to present
a=b;//exchanging values
b=s;
}
return b;//returning the present value of b
}
public static int nthFibonacciWithRecursion(int nth)
{
if(nth<=1)//end condition
{
return nth;//returns n value 0 or 1
}
return nthFibonacciWithRecursion(nth-1)+nthFibonacciWithRecursion(nth-2);//fib(3)=fib(2)+fib(1)
}
private static void check() {
//initializing first 30 fibonacci series values to array
int [] check = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
610, 987, 1597, 2584, 4181, 6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040};
boolean correctLoop = true, correctRecursion = true;//boolean values
for (int i = 0; i < 30; i++) //checking for first 30 values through loop
{
if (nthFibonacciWithLoop(i) != check[i])
correctLoop = false;
if (nthFibonacciWithRecursion(i) != check[i])
correctRecursion = false;
}
if (correctLoop && correctRecursion)//if both of them are true then w got them right
System.out.println("\nYou got them right!");
else//else it will give us which is giving wrong output loop or recursion
{
if (!correctLoop)
System.out.println("\nCheck nthFibonacciWithLoop()");
if (!correctRecursion)
System.out.println("\nCheck nthFibonacciWithRecursion()");
}
}
   public static void main(String[] args) {
   int n,i;
   Scanner s=new Scanner(System.in);
   System.out.print("Enter N: ");
   n=s.nextInt();
   for(i=0;i<n;i++)
   {
   System.out.print(nthFibonacciWithLoop(i) + " ");
   }
   System.out.println();
for ( i = 0; i <= n; i++)
{
System.out.print(nthFibonacciWithRecursion(i) + " ");
}
check();
   }
}


Related Solutions

There is a Java program that is missing one recursive function: public class Ackermann { /*...
There is a Java program that is missing one recursive function: public class Ackermann { /* / n+1 when m = 0 * ack(m,n) = | ack(m-1,1) when m > 0 and n = 0 * \ ack(m-1, ack(m, n-1)) otherwise */ public static int ack(int m, int n) { return 0; } /* Ackermann's Function Test Framework * * Be extremely careful with these test cases. Ackermann's grows very fast. * For example, ack(4, 0) = 13, but ack(5,0)...
There is a Java program that is missing one recursive function: public class BinarySearch { /*...
There is a Java program that is missing one recursive function: public class BinarySearch { /* / -1 when min > max * | mid when A[mid] = v * search(A, v, min, max) = | search(A,v,mid+1,max) when A[mid] < v * \ search(A,v,min,mid-1) otherwise * where mid = (min+max)/2 */ public static int search_rec(int[] A, int v, int min, int max) { return 0; } public static int search(int[] A, int v) { return search_rec(A, v, 0, A.length-1); }...
There is a Java program that is missing one recursive function: public class Factorial { /*...
There is a Java program that is missing one recursive function: public class Factorial { /* / 1 when n is 0; * fact(n) = | * \ n*fact(n-1) otherwise */ public static int fact(int n) { return 0; } /* Factorial Test Framework * * Notice the odd expected value for fact(20). It is negative because * fact(20) should be 2432902008176640000, but the maximum int is only * 2147483647. What does Java do when integers run out of range?...
There is a Java program that is missing one recursive function: public class GCD { /*...
There is a Java program that is missing one recursive function: public class GCD { /* There is a neat trick with recursive programming. The function described    * below requires that y is at most x. Rather than add this to the recursive    * function definition, we can add a front-end, helper function.    *    * I wrote this function for you and I called it gcd. The recursive logic goes    * in a function called...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists. public class LNode { private int m_info; private LNode m_link; public LNode(int info){ m_info = info; m_link = null; } public void setLink(LNode link){ m_link = link; } public LNode getLink(){   return m_link; } public void setInfo(int info){ m_info = info; } public int getInfo(){   return m_info; } } public class LinkedList {...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a...
Java programming. Write a public Java class called DecimalTimer with public method displayTimer, that prints a counter and then increments the number of seconds. The counter will start at 00:00 and each time the number of seconds reaches 60, minutes will be incremented. You will not need to implement hours or a sleep function, but if minutes or seconds is less than 10, make sure a leading zero is put to the left of the number. For example, 60 seconds:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT