C++ numbers add to a negative
So I was just practicing coding a dynamic solution to the Fibonacci
sequence which would return the n'th Fibonacci number and I kept coming
across a problem which I can't quite figure out. I am getting two positive
numbers adding to a negative!
Code:
int fib(int n) {
vector<int> v;
v.push_back(1);
v.push_back(1);
for (int i = 2; i <= n; i++) {
v.push_back( v.at(i-1) + v.at(i-2) );
cout << v.at(i-1) << " + " << v.at(i-2) << " = " << (v.at(i-1) +
v.at(i-2)) << endl;
}
return v.at(n);
}
try running fib(50), note cout is just for debugging
No comments:
Post a Comment