Results 1 to 7 of 7
Thread: Fibonacci sequence
 03242010, 11:47 PM #1Member
 Join Date
 Jan 2010
 Posts
 80
 Rep Power
 0
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; } }
 03252010, 01:02 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,671
 Rep Power
 13
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 ith element if(hasProperty(ele)) { return true; } } return false;
The basic difference between the pseudocode above and the code you posted is where you decide to return.
 03252010, 02:18 AM #3Member
 Join Date
 Jan 2010
 Posts
 80
 Rep Power
 0
 03252010, 04:08 AM #4Member
 Join Date
 Jan 2010
 Posts
 80
 Rep Power
 0
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 ŖàΫ ỏƒ Ңόρę; 03252010 at 04:11 AM.
 03252010, 04:39 AM #5
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;
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
 03252010, 04:41 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,671
 Rep Power
 13
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.
 03252010, 07:59 AM #7Member
 Join Date
 Mar 2010
 Posts
 20
 Rep Power
 0
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 )); } }
position: 0 1 2 3 4 5 6 7 ...
fib num: 0 1 1 2 3 5 8 13 ...
Similar Threads

help with fibonacci
By likemine in forum New To JavaReplies: 8Last Post: 01072010, 03:32 AM 
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12312009, 02:30 AM 
help with fibonacci problem
By thekrazykid in forum New To JavaReplies: 4Last Post: 12122008, 11:41 PM 
Printing Fibonacci Numbers
By Java Tip in forum java.langReplies: 0Last Post: 04092008, 06:43 PM 
Fibonacci Algorithm
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:25 AM
Bookmarks