1. Member Join Date
Jan 2010
Posts
2
Rep Power
0

## help with fibonacci

hello everyone i need big help....

i don't know how to use 'fibonacci' :(

our teacher asked us to make a program that will output how many possible siblings a certain animal can have,

for example if the user input 5 number of kangaroos, we're asked to make a program using fibonacci to output how many possible siblings a 5 kangaroos could have.... she gave us example but i could hardly understand that fibonacci:(

Java Code:
```import java.io.*;
public class FibonacciNum {
public static void main (String[]args) throws IOException {
System.out.println("Enter a number:");
fibonacci(num - 2) + fabonacci(num-1);
}
}```
i really don't know what to do, i can't even explain these codes i wrote :( :(

thank you very much

EDIT:

by the way she gave us this formula:

fibonacci(n-2)+fibonacci(n-1)
fibonacci n = 1
fibonacci o = o
Last edited by likemine; 01-06-2010 at 02:40 AM.  Reply With Quote

2. Senior Member Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
11

##  Originally Posted by likemine hello everyone i need big help....

i don't know how to use 'fibonacci' :(

our teacher asked us to make a program that will output how many possible siblings a certain animal can have,

for example if the user input 5 number of kangaroos, we're asked to make a program using fibonacci to output how many possible siblings a 5 kangaroos could have.... she gave us example but i could hardly understand that fibonacci:(

Java Code:
```import java.io.*;
public class FibonacciNum {
public static void main (String[]args) throws IOException {
System.out.println("Enter a number:");
fibonacci(num - 2) + fabonacci(num-1);
}
}```
i really don't know what to do, i can't even explain these codes i wrote :( :(

thank you very much

EDIT:

by the way she gave us this formula:

fibonacci(n-2)+fibonacci(n-1)
fibonacci n = 1
fibonacci o = o

take a look at this

Java Code:
```import java.io.*;
public class FibonacciNum {
public static void main (String[]args) throws IOException

{

System.out.println("Enter a number:");

//Here when the system asks for a number. and a user types it. The user //input is a String. So, we need to parse it into a String.

//hmm there doesnt seem to be sense. whats fibnacci(int) here?..Usually you //can do fibonacci through recursion.
fibonacci(num - 2) + fabonacci(num-1);
}
}```

take a look at recursion and fibonacci section. at

recusion  Reply With Quote

3. Member Join Date
Dec 2009
Posts
68
Rep Power
0

## ill share you mine.. an iterative way.. i had hard time too thinking of how to make a fibonacci program i hope you understand this one...

Java Code:
```public class FibonacciIterative2 {

public static void main(String[] args) {

int f1 = 1,
f2 = 1,
temp;

// fibonacci iterating sequence starting in F(N) where N = 3
for (int x = 3; x <= 10; x++) {

temp = f1;
f1 = f2;
f2 = temp + f2;

System.out.println(f2);
}
}
}```
i got this program in wikipedia. but i cant find the exact page where this program occurs, so ill share the code to you...
Last edited by bigj; 01-06-2010 at 07:05 AM.  Reply With Quote

4. Senior Member Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
11

##  Originally Posted by bigj ill share you mine.. an iterative way.. i hope you understand this one...

Java Code:
```public class FibonacciIterative2 {

public static void main(String[] args) {

int f1 = 1,
f2 = 1,
temp;

// fibonacci iterating sequence starting in F(N) where N = 3
for (int x = 3; x <= 10; x++) {

temp = f1;
f1 = f2;
f2 = temp + f2;

System.out.println(f2);
}
}
}```
this also another way to do it. But this way of doing is very inefficient for large numbers.

So, recursion is commonly used way for this type of problem  Reply With Quote

5. Member Join Date
Dec 2009
Posts
68
Rep Power
0

## oh sir.. tnx for the quick response to my code... any way.. atleast now i know
fibonacci is more efficient in recursive way... tnx sir!  Reply With Quote

6. ##  Originally Posted by bigj oh sir.. tnx for the quick response to my code... any way.. atleast now i know
fibonacci is more efficient in recursive way... tnx sir!
Aux contraire: recursive Fibonacci implementations might be short but they are extremely inefficient because you calculate the same numbers over and over again. Use an iterative implementation instead.

kind regards,

Jos  Reply With Quote

7. Senior Member Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
11

##  Originally Posted by bigj oh sir.. tnx for the quick response to my code... any way.. atleast now i know
fibonacci is more efficient in recursive way... tnx sir!
My apologies. Just cross checked. Its the other way around. Iterative is much

more efficient than recursive. Heres an evidence for that.

Recursive vs iterative

Again Sry for the wrong conclusion.  Reply With Quote

8. Member Join Date
Jan 2010
Posts
2
Rep Power
0

##  Originally Posted by bigj ill share you mine.. an iterative way.. i had hard time too thinking of how to make a fibonacci program i hope you understand this one...

Java Code:
```public class FibonacciIterative2 {

public static void main(String[] args) {

int f1 = 1,
f2 = 1,
temp;

// fibonacci iterating sequence starting in F(N) where N = 3
for (int x = 3; x <= 10; x++) {

temp = f1;
f1 = f2;
f2 = temp + f2;

System.out.println(f2);
}
}
}```
i got this program in wikipedia. but i cant find the exact page where this program occurs, so ill share the code to you...
the output for this one is
Java Code:
```2
3
5
8
13
21
34
55```
so is this the possible siblings of 3 kangaroos can have?

where did you get this one?
Java Code:
`x <= 10;`  Reply With Quote

9. Member Join Date
Dec 2009
Posts
68
Rep Power
0

## where did you get this one?
Code:
x <= 10;
it means that we are looking for the first 10 sequence of the fibonacci

f1 - f10, or could say 1 - 10,  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
•