Print the Fibonacci sequence - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Print the Fibonacci sequence

guobianguobian Posts: 10Member
hi,i want to print out the fibonacci sequence:
1 1 2 3 5 8....
i know how to get the nth number in the sequence, but i stuck at
where to addin the println setence, here is my code:

public class Fibonacci
{
public Fibonacci()
{
}

public static void main(int n)
{
System.out.println("The nth Fibonacci number show as following: ");
recusion(n);
}

private int recusion(int n)
{
if (n==0 || n==1)
{
return 1;
}
else
{
int sum = recusion(n-1) + recusion(n-2);
System.out.print(sum + " ");
return sum;
}
}
}

Comments

  • VilanyeVilanye Posts: 684Member
    After you grab the return value, just stick it in the SOP in main.

    Unless you have to use recursion, I would avoid it for fibonacci type problems. It is a good example of recursion and a good example of when not to use it. An iterative version is much faster, due to not having a ton of methods being placed on the call stack.

    : hi,i want to print out the fibonacci sequence:
    : 1 1 2 3 5 8....
    : i know how to get the nth number in the sequence, but i stuck at
    : where to addin the println setence, here is my code:
    :
    : public class Fibonacci
    : {
    : public Fibonacci()
    : {
    : }
    :
    : public static void main(int n)
    : {
    : System.out.println("The nth Fibonacci number show as following: ");
    : recusion(n);
    : }
    :
    : private int recusion(int n)
    : {
    : if (n==0 || n==1)
    : {
    : return 1;
    : }
    : else
    : {
    : int sum = recusion(n-1) + recusion(n-2);
    : System.out.print(sum + " ");
    : return sum;
    : }
    : }
    : }
    :
    :

  • moomoo Posts: 147Member
    hi,
    don't use a recursive fibonacci function since it is very inefficient.
    try this:

    [code]
    /**
    * the recursive fibonacci function
    *
    * @param n
    * saves the index of the fibonacci number to compute
    * @return the Nth fibonacci number
    */
    private static long f(int n) {
    if ((n == 0) || (n == 1)) {
    return 1;
    }
    return f(n - 2) + f(n - 1);
    }

    /**
    * computes the Nth fibonacci number. BE CAREFULL! This method does no range
    * checking! It may return incorrect results if the result is larger than
    * {@link Long#MAX_VALUE}
    *
    * @param n
    * the index of the fibonacci number
    * @param recursive
    * indicates whether to compute the fibonacci numbers recursive
    * or iterative
    *
    * @return the Nth fibonacci number
    */
    public static long fibonacci(int n, boolean recursive) {
    if (n < 0) {
    throw new IllegalArgumentException("no negative numbers allowed");
    }
    if ((n == 0) || (n == 1)) {
    return 1;
    }

    if (recursive) {
    return f(n);
    }

    long current = 1;
    long pred = 1;
    long result = pred + current;

    for (int i = 2; i < n; i++) {
    pred = current;
    current = result;
    result = pred + current;
    }

    return result;
    }

    /**
    * print out the numbers...
    */
    public static void main(String[] args) {
    int n = 43;

    long start = System.currentTimeMillis();
    long result = fibonacci(n, true);
    long stop = System.currentTimeMillis();

    System.out.println("<<iterative>> fib("+n+") = "+result+" [computed in "+(stop-start)+" ms]");

    start = System.currentTimeMillis();
    result = fibonacci(n, false);
    stop = System.currentTimeMillis();

    System.out.println("<<recursive>> fib("+n+") = "+result+" [computed in "+(stop-start)+" ms]");
    }
    [/code]
Sign In or Register to comment.