# Problem in generating Fibonacci sequence in java

• April 23rd, 2009, 01:52 PM
big_c
Problem in generating Fibonacci sequence in java
hello all. i am working on the Fibonacci sequence constructor and i was wondering if i posted wha ti have so far if you could tell me if im on the right trac or not and maybe give me a few pointers to help me get it right heres what i have for code and the assignment so you can see what i have to do

Code :

```public class FibonacciGenerator { public static int fib(int n) { int fold1=0, fold2=1; public getNumber() for(int i=0; i<n; i++) { int savefold1 = fold1; fold1 = fold2; fold2 = savefold1 + fold2; } return fold1; } }```

Write a program that prompts the user for n and prints the nth value in the Fibonacci sequence. Use a class FibonacciGenerator with a method nextNumber .

There is no need to store all values for fn. You only need the last two values to compute the next one in the series:
fold1 = 1;
fold2 = 1;
fnew = fold1 + fold2;
After that, discard fold2 , which is no longer needed, and set fold2 to fold1 and fold1 to fnew .
• April 24th, 2009, 03:23 AM
Freaky Chris
Re: i could use a few pointers with the Fibonacci sequance
Your code makes no sense. You have random function things in the middle of another function.

What i can tell you is that a Fibonacci Sequence is denoted by the following mathematical Expression.

F(n) = F(n-1) + F(n-2) : Where F(0) = 0, F(1) = 1

using that you can write either a recurring formulae or looped to compute the answer.

Since you are trying to find the value of F(N) there is a mathematical equation that allows you to compute that and only that, which you could do as a side for extra merit.

the equation is as such

((((1 + ( 5^0.5 ) ) / 2)^n ) / ( 5^0.5 ) ) + 0.5

Then rounded by the rules of floor, which state the number is to be rounded down to the nearest whole number, NEVER up. So evern 3.999999999 would be rounded to 3. this is often the effect given via truncation when dealing with integer mathematics whilst programming.

If anyone reads this and is wondering about the solution using Math to find F(N) without computing the entire sequence up F(N) then here it is,
Code :

```import java.util.Scanner;   public class FibonacciGenerator {   public static void main(String[] args){ while(true){ System.out.println("Enter a number to compute the fibonacci number for:"); System.out.println(fib(new Scanner(System.in).nextInt())); } }     public static int fib(double n) { return (int)(( Math.pow(((1 + Math.sqrt(5)) / 2 ), n) / Math.sqrt(5) ) + 0.5); } }```
big_c it should be noted, you cannot hand that in because it is not what your assignment asks for :)

Thanks,
Chris
• April 24th, 2009, 08:52 AM
Freaky Chris
Re: i could use a few pointers with the Fibonacci sequance
Here are three different solutions, it is clear to see which are best. Of course the final looped solution is the best for the problem big_c is facing.

Code :

```import java.util.Scanner;   public class FibonacciGenerator { public static void main(String[] args){ while(true){ System.out.println("Enter a number to compute the fibonacci number for:"); int n = new Scanner(System.in).nextInt();   System.out.println(fib(n)); System.out.println(fibR(n)); System.out.println(fibL(n));   } }   public static int fib(double n) { return (int)(( Math.pow(((1 + Math.sqrt(5)) / 2 ), n) / Math.sqrt(5) ) + 0.5); }   public static int fibR(int n){ if(n == 0) return 0; else if(n <= 2) return 1; return fibR(n-1)+fibR(n-2); }   public static int fibL(int n){ int A = 0; int B = 1; int current = 0; if(n == 0) return 0; else if(n <= 2) return 1; for(int i = 0; i < n-1; i++){ current = A+B; A = B; B = current; } return current; } }```