Results 1 to 9 of 9
  1. #1
    likemine is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

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

    can someone help me please?

    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.

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by likemine View Post
    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 {
            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 :( :(

    can someone help me please?

    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 
    
    {
    //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

  3. #3
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    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.

  4. #4
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by bigj View Post
    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

  5. #5
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    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!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigj View Post
    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

  7. #7
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    Quote Originally Posted by bigj View Post
    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.

  8. #8
    likemine is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by bigj View Post
    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;

  9. #9
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    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,

Similar Threads

  1. Fibonacci summation problem
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 12-01-2009, 06:28 PM
  2. help with fibonacci problem
    By thekrazykid in forum New To Java
    Replies: 4
    Last Post: 12-12-2008, 10:41 PM
  3. Printing Fibonacci Numbers
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:43 PM
  4. A Fibonacci printing program
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 07:26 PM
  5. Fibonacci Algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:25 AM

Posting Permissions

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