Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Fibonacci sequence

    Hi

    I have a problem with the following code which is when I enter some of Fibonacci sequence numbers it gives me the numer is not in sequence

    e.g. if I enter 2,3, or 8 it gives me not in sequence although they are in sequence but if I enter 1 or 5 it gives me in sequence

    these are the Fibonacci sequence
    1, 1, 2, 3, 5, 8, 13, 21, 34, ...

    Java Code:
    public class FibonacciTest1 {
    
    	public static int getValue(int n){
    		int num1=1;
    		int num2=0;
    		
    		for(int i=0;i<n;i++){
    			int nth=num1+num2;
    			num1=num2;
    			num2=nth;
    		}
    		return num2;
    	}
    	
    	public static boolean inSequence(int value){
    		int num1=1;
    		int num2=0;
    		boolean test;
    		for(int i=0;i<value;i++){
    			int nth=num1+num2;
    			num1=num2;
    			num2=nth;
    		}
    		
    		if(num2==value){
    			test=false;
    		}else{
    			test=true;
    		}
    		return test;
    	}
    }
    I run this program by an applet

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Notice that 1 and 5 are a bit special. 1 is the first Fib number and 5 is the fifth.

    As an idiom for "does an element of my sequence have a given property" consider:

    Java Code:
    for(int i = 0; i < ???A; i++) {
        ele = calculate i-th element
        if(hasProperty(ele)) {
            return true;
        }
    }
    return false;
    In your case the property is "equaling val". The thing I've denoted ???A is a value estimating "how many sequence elements need to be checked" and it has to be big enough. Notice you might find it easier to use a while loop - that's because it might not be so easy to decide how many sequence elements need to be checked, but it might be really easy to decide when to stop checking.

    The basic difference between the pseudocode above and the code you posted is where you decide to return.

  3. #3
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Notice that 1 and 5 are a bit special. 1 is the first Fib number and 5 is the fifth.

    As an idiom for "does an element of my sequence have a given property" consider:

    Java Code:
    for(int i = 0; i < ???A; i++) {
        ele = calculate i-th element
        if(hasProperty(ele)) {
            return true;
        }
    }
    return false;
    In your case the property is "equaling val". The thing I've denoted ???A is a value estimating "how many sequence elements need to be checked" and it has to be big enough. Notice you might find it easier to use a while loop - that's because it might not be so easy to decide how many sequence elements need to be checked, but it might be really easy to decide when to stop checking.

    The basic difference between the pseudocode above and the code you posted is where you decide to return.
    so you mean the mistake in this block

    Java Code:
    if(num2==value){
    			test=false;
    		}else{
    			test=true;
    		}
    		return test;

  4. #4
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    Actually, I despaired so I used 'while loop' instead of 'for loop'

    I don't think I will do it with 'for loop'

    Java Code:
    public class FibonacciTest1 {
    
    	public static int getValue(int n){
    		int num1=1;
    		int num2=0;
    		
    		for(int i=0;i<n;i++){
    			int nth=num1+num2;
    			num1=num2;
    			num2=nth;
    		}
    		return num2;
    	}
    	
    	public static boolean inSequence(int value){
    		
    		int num1=1;
    		int num2=0;
    		boolean test;
    		
    		while(num2<value){
    			int nth=num1+num2;
    			num1=num2;
    			num2=nth;
    		}
    		
    		if(num2==value){
    			test=false;
    		}else{
    	test=true;
    		}
    		return test;
    	}
    }
    Last edited by ŖΫ ỏ Ңόρę; 03-25-2010 at 03:11 AM.

  5. #5
    sonny's Avatar
    sonny is offline Senior Member
    Join Date
    Feb 2010
    Location
    North West England
    Posts
    146
    Rep Power
    0

    Lightbulb

    i'm still a beginner at Java too,,


    According to some the fibonnacci sequence starts at 1, 1
    others say it is 0 , 1 Fibonacci - Wikipedia, the free encyclopedia

    if you declare int num2 = -1 your sequence will then start at 0
    thus 1 will not be the first number and 5 will not be the fifth number.

    what does your code do now??

    how many iterations (how many times round) does the loop need to go through before it gets to 1 or 5? or even 2 or 3? what would be the value of int i have to be to get as far as those numbers?
    0,1,1,2,3,5,
    does it need to make more iterations than you have allowed it to make by giving it a set number of times to go through?

    instead of a for loop with a set number of iterations,
    it would be more useful if you could keep on calculating the sequence of numbers until a certain condition is met.

    for example there would be no more point in generating and checking more of the Fibonacci sequence numbers,, if the result of num2 is greater than the value you are checking

    so you mean the mistake in this block

    Java Code:
    Code:
    if(num2==value){
    			test=false;
    		}else{
    			test=true;
    		}
    		return test;
    maybe your asking the question in the wrong place, your loop has finished at this point.... with reference to your question about 1 and 5 returning true in your code.. think why this might have happened.

    consider
    Java Code:
    public boolean inFibSequence(int value){
    	int num1=1;
    	int num2=0;
    	while(true){ 
    		int nth=num1+num2;
    		num1=num2;
    		num2=nth;
    		
    		/* a couple of questions do decide weather to  leave the loop*/
    		if (num2 == value)return ???? ; // cos i have my answer
    		if (num2 > value)  then i need to jump out of the loop and once ive jumped out i can return my answer 
    		}
    		return ????
    	}
    :p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes the mistake is returning after the while loop. Return from within the while loop if possible. But think about why: ie think about what the pseudocode I posted is supposed to do.

    [edit] Didn't see sonny's post. This is a reply to the last one of the OP.

  7. #7
    fceruti is offline Member
    Join Date
    Mar 2010
    Posts
    20
    Rep Power
    0

    Default

    heres another way of doing it:

    Java Code:
    public int fibonacciNumber ( int position ){
         if(position < 2){
              return position;
         } else {
            return (fibonacciNumber ( position -1) + fibonacciNumber( position -2 ));
         }
    }
    note: this code considers positions starting from zero.

    position: 0 1 2 3 4 5 6 7 ...
    fib num: 0 1 1 2 3 5 8 13 ...

Similar Threads

  1. help with fibonacci
    By likemine in forum New To Java
    Replies: 8
    Last Post: 01-07-2010, 02:32 AM
  2. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 01:30 AM
  3. help with fibonacci problem
    By thekrazykid in forum New To Java
    Replies: 4
    Last Post: 12-12-2008, 10:41 PM
  4. Printing Fibonacci Numbers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:43 PM
  5. Fibonacci Algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •