topgun_ivard
topgun_ivard

Reputation: 8584

which is a better approach for fibonacci series generation

The two general approaches for Fibonacci series generation are:

  1. The traditional approach, i.e., running through a for loop inside a function.
  2. Recursion

I came across another solution

#include <iostream>

using namespace std;

void fibo() {
 static int y = 0;
 static int x = 1;
 cout << y << endl;
 y = x + y;
 x = y - x;
}

int main() {
 for (int i = 1; i <= 1; i++) {
  fibo();
 }
 return 0;
}

This solution looks to be working fine in the initial runs, but when compared to the traditional and recursion approach, does this hold any significant disadvantages?

I am sure static variables would add to space complexity, but at least we are not building a function table stack using recursion, correct?

Upvotes: 3

Views: 1899

Answers (6)

Jittin
Jittin

Reputation: 9

I am a C++ student (1.5 months into it).

Give feedback to this different way I have thought of for Fibonacci series.

#include<iostream>

using namespace std;

void fibseries(long int n)
{
    double x=0;double y=1;
    for (long int i=1;i<=n;i++)
     {
        if(i%2==1)
         {
            cout<<x<<" ";
            x=x+y;
         } 
        else 
         {
            cout<<y<<" ";
            y=x+y;
         }
     }
}

main()
{
    long int n=0;
    cout<<"The number of terms ";
    cin>>n;
    fibseries(n);
    return 0;
}

Upvotes: 1

leftaroundabout
leftaroundabout

Reputation: 120711

As was said before, the advantage of the static variables is, in principle, that it's cheaper to calculate the n -th Element of a sequence where the n - 1 -th has already been evaluated.

The big drawback, apart from the problems inherent to static variables, is that you don't have any way to get back to an earlier point in the sequence, nor do you really have a good control over where in the sequence you are at a given time.

Using a class, as recommended by Sevis, is certainly the better way of implementing such a static-like approach: this makes everything safer, gives you an easy way to get back to the sequence start (by simply reinitializing the object) and also makes it possible to implement further functionality, like going back k steps, looking up the present position, etc..

Upvotes: 0

James Kanze
James Kanze

Reputation: 153957

I'm not sure what this function is really supposed to do. It only works in the exact loop you present, and as others have pointed out, it only works once. (And there's probably a typo in your loop, since your complete program outputs "0", and nothing else.) What advantage does it offer over:

int y = 0;
int x = 1;
for ( int i = 0; i < count; ++ i ) {
    std::cout << y <<std::endl;
    y = x + y;
    x = y - x;
}

? It's more complex, far less robust, and far less useful.

Upvotes: 0

Komi Golov
Komi Golov

Reputation: 3471

The solution you found is decent for when you need to store the state (for example, when you calculate a Fibonacci number, do something based on it, and then calculate another), but using this from two places in your code will likely give funny results. This is because the static variables will always be the same, no matter from where you call it. I would instead suggest:

class FiboNumbers {
  public:
    FiboNumbers() :
        x_(1), y_() {}

    int getNext() {
        x_ += y_;
        y_ = x_ - y_;
        return x_;
    }

  private:
    int x_, y_;
};

This offers the same keeping-of-state, but allows you to create multiple instances of the class, therefore allowing you to have different parts of the code that calculate their own Fibonacci series.

Minor note: the code I posted will produce the same series as the example you posted, but it will produce the real Fibonacci sequence, which starts with 0 1 1 2...

Upvotes: 2

Shaishav7
Shaishav7

Reputation: 139

I think this pointer approach would be more useful for you.

void main()
{
    int i,p, *no,factorial,summ;

    int fib(int p);

    clrscr();

    printf("\n Enter The Number:");
    scanf("%d", no);
    printf("\n The Fibonnacci series: \n");

    for(i=0; i < *no; i++)
        printf("%d\n", fib(i));

    getch();
}

int fib(int p)
{
    if(p == 0)
        return(0);
    if(p >= 1 && p <= 2)
        return(1);
    else
        return(fib(p - 1) + fib(p - 2));
}

Upvotes: -3

Jon Skeet
Jon Skeet

Reputation: 1501656

Disadvantages I can immediately see:

  • By essentially making the state global, it's not thread-safe
  • You can only ever run through the sequence once, as there's no way to reset

I would favour an approach which keeps the state within an object which you can ask for the next value of - an iterator, basically. (I've never been certain how easily the Fibonacci sequence maps to C++ iterators; it works fine with C# and Java IEnumerable<T> and Iterable<T> though.)

Upvotes: 7

Related Questions