Question

In: Computer Science

Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part...

Fibonacci Sequence in JAVA

f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2)

Part of a code
public class FS {
public static void main(String[] args){
IntSequence seq = new FibonacciSequence();
for(int i=0; i<20; i++) {
if(seq.hasNext() == false) break;
System.out.print(seq.next()+" ");
}
System.out.println(" ");
}
}
///Fill in the rest of the code!
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

You should define IntSequence as the interface.

You can't use for, while loop.

Thanks!!

Solutions

Expert Solution

IntSequence.java
------
public interface IntSequence
{
   public boolean hasNext();
   public int next();
}


FibonacciSequence.java
====

public class FibonacciSequence implements IntSequence {

   private int n;


   public int next(){
       int f = 0;
       int f1 = 0, f2 = 1;
       if(n == 0)
           f = 0;
       else if (n == 1)
           f = 1;
       else{
           for(int i = 2; i <= n; i++)
           {
               f = f1 + f2;
               f1 = f2;
               f2 = f;
           }
       }
       n++;
       return f;
   }
   @Override
   public boolean hasNext() {
       return true;
   }


}


FS.java
----
public class FS {
   public static void main(String[] args){
       IntSequence seq = new FibonacciSequence();
       for(int i=0; i<20; i++) {
           if(seq.hasNext() == false) break;
           System.out.print(seq.next()+" ");
       }
       System.out.println(" ");
   }
}

output
---
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181


Related Solutions

Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n −...
Fibonacci Sequence: F(0) = 1, F(1) = 2, F(n) = F(n − 1) + F(n − 2) for n ≥ 2 (a) Use strong induction to show that F(n) ≤ 2^n for all n ≥ 0. (b) The answer for (a) shows that F(n) is O(2^n). If we could also show that F(n) is Ω(2^n), that would mean that F(n) is Θ(2^n), and our order of growth would be F(n). This doesn’t turn out to be the case because F(n)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2)...
2. The Fibonacci sequence is defined as f(n) = f(n - 1) + f(n - 2) with f(0) = 0 and f(1) = 1. Find f(54) by a program or maually. Note that this number must be positive and f(53) = 53.......73 (starting with 53 and ending with 73). I must admit that my three machines including a desktop are unable to find f(54) and they quit during computation. The answer is f(54) = 86267571272 */ The Java code: public...
For the Fibonacci sequence, prove the formula u2n+1 = un un+2 + (-1)n
For the Fibonacci sequence, prove the formula u2n+1 = un un+2 + (-1)n
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...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13,...
The Fibonacci sequence is the series of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . . See the pattern? Each element in the series is the sum of the preceding two elements. Here is a recursive formula for calculating the nth number of the sequence: Fib(N) = {N, if N = 0 or 1 Fib(N - 2) + Fib(N - 1), if N > 1 a) Write a recursive method fibonacci that returns...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int...
In Java: int[] A = new int[2]; A[0] = 0; A[1] = 2; f(A[0],A[A[0]]); void f(int x, int y) { x = 1; y = 3; } For each of the following parameter-passing methods, saw what the final values in the array A would be, after the call to f. (There may be more than one correct answer.) a. By value. b. By reference. c. By value-result.
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn =...
Let the Fibonacci sequence be defined by F0 = 0, F1 = 1 and Fn = Fn−1 + Fn−2 for n ≥ 2. Use induciton to prove that F0F1 + F1F2 + · · · + F2n−1 F2n = F^2 2n for all positive integer n.
Find f(1), f(2), f(3) and f(4) if f(n) is defined recursively by f(0)=4f(0)=4 and for n=0,1,2,…n=0,1,2,…...
Find f(1), f(2), f(3) and f(4) if f(n) is defined recursively by f(0)=4f(0)=4 and for n=0,1,2,…n=0,1,2,… by: (a) f(n+1)=−2f(n) f(1)= f(2)= f(3)= f(4)= (b) f(n+1)=4f(n)+5 f(1)= f(2)= f(3)= f(4)= (b) f(n+1)=f(n)2−4f(n)−2 f(1)= f(2)= f(3)= f(4)=
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 *...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 * 2 * 1 For example, 5! = 5 * 4 * 3 * 2 * 1 = 120 Write a function called factorial that takes as input an integer. Your function should verify that the input is positive (i.e. it is greater than 0). Then, it should compute the value of the factorial using a for loop and return the value. In main, display...
(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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT