# help with fibonacci

• 01-06-2010, 03:38 AM
likemine
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:(

Code:

```import java.io.*; public class FibonacciNum {     public static void main (String[]args) throws IOException {         BufferedReader dataIN = new BufferedReader(new InputStreamReader(System.in));             System.out.println("Enter a number:");             int num = Integer.parseInt(dataIN.readLine());             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
• 01-06-2010, 04:40 AM
[RaIdEn]
Quote:

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

Code:

```import java.io.*; public class FibonacciNum {     public static void main (String[]args) throws IOException {         BufferedReader dataIN = new BufferedReader(new InputStreamReader(System.in));             System.out.println("Enter a number:");             int num = Integer.parseInt(dataIN.readLine());             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

Code:

```import java.io.*; public class FibonacciNum {     public static void main (String[]args) throws IOException { //BufferedReader for the class   BufferedReader dataIN = new BufferedReader(new InputStreamReader(System.in)); //Asks for a number             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.             int num = Integer.parseInt(dataIN.readLine()); //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
• 01-06-2010, 08:01 AM
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...

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...
• 01-06-2010, 08:03 AM
[RaIdEn]
Quote:

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

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
• 01-06-2010, 08:07 AM
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!
• 01-06-2010, 08:52 AM
JosAH
Quote:

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
• 01-06-2010, 09:33 AM
[RaIdEn]
Quote:

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.
• 01-06-2010, 10:15 PM
likemine
Quote:

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

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
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?
Code:

`x <= 10;`
• 01-07-2010, 03:32 AM
bigj
Quote:

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,