Results 1 to 4 of 4
  1. #1
    Fingerz is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default Fib numbers recursivly

    Anyone suggest a more efficient way to write this recursivly? The common Fibonacci numbers.


    class Fibonacci{

    long oldNumber = 0;
    long lastNumber = 1;
    long newNumber;

    long Fib(long n){

    if ( n == 0 ) return 0;
    newNumber = oldNumber + lastNumber;
    oldNumber = lastNumber;
    lastNumber = newNumber;
    System.out.println(newNumber);

    return Fib(n-1);

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    gcalvin wrote a great post on how to approach any recursive problem, just yesterday in this forum. I suggest you look in the New To Java forums and search for the word "recursive" and you will find it, it will be very helpful for you for any recursion problem.

    For Fibonacci, the base case, will be when n = 0 and when n = 1, where we would simply return 0 and 1.

    Next, we try to represent our problem as a smaller instance of itself. Thus, if n does not fall into the cases mentioned above, then the nth Fib number is simply the sum of the previous two numbers. So the last case will be:
    Java Code:
    return Fib(n-1) + Fib(n-2);
    hope this helps.

    Best,
    --user0--

  3. #3
    Fingerz is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default Ok

    mind showing what code needs to be changed. TYVM

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    what do you mean? You know how to write some basic java correct? The answer I gave you is almost Java code no? Did you at least have a try to change your existing code?

    Please post some changes you have done to your code and we can see how to help you. I am relatively new here too, but I know that this forum exists to improve and expand your knowledge of Java, not for people to post code for you.

    Posting code is so easy, but then what do you and I gain from it?

    Kindly try to have an attempt to change your code, looking at your code, I can say you are very close to achieving what you need.

    Post here after that, and I'm sure we can help you proceed.

    best,
    --user0--

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 04:03 PM
  2. Comparing 3 or more numbers
    By W00tbeer1 in forum New To Java
    Replies: 8
    Last Post: 10-11-2010, 08:03 AM
  3. Sum of 4 numbers...!?
    By xmenus in forum New To Java
    Replies: 7
    Last Post: 02-14-2010, 04:36 PM
  4. How to Spell out numbers?
    By syntrax in forum New To Java
    Replies: 5
    Last Post: 10-22-2009, 04:34 PM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 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
  •