Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem in generating Fibonacci sequence in java

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

    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 .


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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,
    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
    Last edited by Freaky Chris; April 24th, 2009 at 07:59 AM.

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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.

    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;
    	}
    }

  4. The Following User Says Thank You to Freaky Chris For This Useful Post:

    JavaPF (April 24th, 2009)