Hello,

I wrote a code to calculate the fibonacci number. However, i noticed that it starts acting erratic when i input 1000 i.e it cannot calculate the 1000th fibonacci number.

I would appreciate it if you could show me how to make it work.

``````int f(int &n)
{
if ((n == 2) || (n == 1))
return(1);
else
return(f(n-1) + f(n-2));
}``````

thank you in advance for your help

3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by ShawnCplus

Are you sure you want

``int f (int & n)``

I get an error when I compile the above but I don't when I do this:

``int f (int n)``

Does anybody else get an error?

oh! u r right. sorry about that. i was messing up with the code to see if i could get the 1000th number but i guess i somehow put that in there.
thanks

Are you sure you want

``int f (int  n)``

I get an error when I compile the above but I don't when I do this:

``int f (int n)``

Does anybody else get an error?

The simplest fib sequence you can write is pretty much

``int fib( int n ){return ( n<2 )?n:fib( n-1 )+fib( n-2 );}``
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.