Find Index of given fibonacci number in constant time | A simple C ++ program to find index of given Fibonacci number . ; if Fibonacci number is less than 2 , its index will be same as number ; iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } int main ( ) { int result = findIndex ( 21 ) ; printf ( " % d STRNEWLINE " , result ) ; }