0

just a fibonacci algorithm, how do i print every number of fibonacci sequence without repeat every step?

Does recursive functions is a good use in any way? I know it is more legible, but there is a visible delay in this simple algorithm if i put n = 40, whereas the iterative way of doing it is instantaneous.

int fib(int n)
{   
    if (n == 0)
    {
        return 0;
    }
    else if (n == 1)
    {
        return 1;
    }

    return fib(n - 1) + fib(n - 2);
}
6
  • 1
    perhaps this question and answers will shine some light on your very similar question: stackoverflow.com/q/38215004/1212725 Commented May 30, 2018 at 0:20
  • @bruceg still dont know what situation recursive is a good use, although that post surely does help, thanks. Commented May 30, 2018 at 0:41
  • 1
    A naïve recursive solution has to calculate fib(40) once, but that means calculating fib(39) once and fib(38) once. Calculating fib(39) means calculating fib(38) and fib(37) — so that's two lots of fib(38) that will be calculated. Of course, each of those also calculates fib(37) and fib(36), so fib(37) is calculated 3 times, and … it quickly builds up to a lot of recursive function calls, which is why the recursive solution takes more time than the iterative solution. No: (naïve) recursive Fibonacci sequence is a bad idea. A not-so-naive version stashes values as it goes. Commented May 30, 2018 at 0:42
  • Recursion is good only in the school. In the real programming is useless and some standards ban it completely as very dangerous and error prone. It is usually slower and consumes a lots of of the resources (time, stack space etc) Commented May 30, 2018 at 1:18
  • @JonathanLeffler the not-so-naive version has advantages, is equal or still worse than iterative way? Commented May 30, 2018 at 1:29

2 Answers 2

1

You can easily optimize the recursive solution by memoizing the already-computed values:

int fib(int n) {
    static int cache[48] = {0}; // should be enough to hold all int fibs
    if(n < 2) return n; // initial conditions
    else if(cache[n]) return cache[n]; // a value already computed and memoized
    else return cache[n] = fib(n - 1) + fib(n - 2); // the largest so far
}

Should speed up the computation by, uh, some factor.

Sign up to request clarification or add additional context in comments.

4 Comments

A good idea, but this doesn't really answer the OP's question.
It does, though the question is underformulated.
"how do i print every number of fibonacci sequence without repeat every step?" Seems fairly well formed to me.
Yes, but if you read the question body, this function only calculates F, while they are printed else where, and the only issue is about fib taking too long at large argument valued.
0

Imperative languages like C do not always map well to functional definitions of algorithms.

Non-recursive is generally faster because both the compiler and processor can more easily optimize/parallel'ize the execution and you're not wasting energy, needlessly pushing and popping the stack. Either way, all you need are the previous two fib values to calculate the next one:

void PrintNFibs(unsigned n)
{
    size_t a = 1;
    size_t b = 1;
    size_t sum;
    printf("0\n1\n1\n");
    while ( n-- )
    {
        sum = a + b;
        printf("%zu\n", sum);
        a = b;
        b = sum;
    }
}

It's one thing to define an algorithm in terms of itself (recursion) and another to implement it efficiently in C. For something as simple as Fibonacci however, I would not use recursion, but here's one anyway:

void PrintNFibsR(unsigned n)
{
    static size_t a = 0;
    static size_t b = 1;
    static size_t sum;
    sum = a + b;

    if ( a == 0 )
    {
        printf("0\n1\n");
    }

    printf("%zu\n", sum);
    if ( n > 1 )
    {
        a = b;
        b = sum;
        PrintNFibsR(n - 1);
    }
}

Notice that all we're really doing here is passing the loop counter. Wasteful but technically recursive, if not actually functional. The problem with writing C code that looks just like the recursive Fibonacci algorithm definition, is it burns energy and stack space for no good reason. The only way you can print the values in the correct order without calculating and storing each one of them in advance, is to alter the algorithm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.