1. Member 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;
}
}```
I run this program by an applet  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17

## 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.  Reply With Quote

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

##  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:

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;```  Reply With Quote

4. Member 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 ŖàΫ ỏ Ңόρę; 03-25-2010 at 04:11 AM.  Reply With Quote

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;```
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 ????
}```  Reply With Quote

6. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
17

## 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.  Reply With Quote

7. Member 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 ));
}
}```
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 ...  Reply With Quote

#### Posting Permissions

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