# Fibonacci sequence

• 03-24-2010, 11:47 PM
ŖàΫ ỏƒ Ңόρę
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, ...

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
• 03-25-2010, 01:02 AM
pbrockway2
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:

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.
• 03-25-2010, 02:18 AM
ŖàΫ ỏƒ Ңόρę
Quote:

Originally Posted by pbrockway2
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:

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

Code:

```if(num2==value){                         test=false;                 }else{                         test=true;                 }                 return test;```
• 03-25-2010, 04:08 AM
ŖàΫ ỏƒ Ңόρę
Actually, I despaired so I used 'while loop' instead of 'for loop'

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

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;         } }```
• 03-25-2010, 04:39 AM
sonny
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

Quote:

so you mean the mistake in this block

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
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 ????         }```
• 03-25-2010, 04:41 AM
pbrockway2
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.

 Didn't see sonny's post. This is a reply to the last one of the OP.
• 03-25-2010, 07:59 AM
fceruti
heres another way of doing it:

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