I interpret the question differently....given a number
as an input, what is the index
of that number in the series? e.g. input=5
, then index is 5
(given the sequence is 0 1 1 2 3 5
where the index begins with 0
)
This the code is as follows (which returns the index) [Disclaimer: Adapted from the code given at http://talkbinary.com/programming/c/fibonacci-in-c/ ]
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
if ( n== 1 )
return 1;
int fib1 = 0;
int fib2 = 1;
int fib = 0;
int i = 0;
for (i = 2; ; i++ )
{
fib = fib1 + fib2;
if ( n == fib )
break;
fib1 = fib2;
fib2 = fib;
}
return i;
}