In this blog we will see how to find index of a particular number in fibonacci series and find the number at a particular index in fibonacci series
1. Find the number at a particular index in fibonacci series
The Fibonacci numbers are defined recursively by the following difference equation:
It is easy to compute the first few elements in the sequence:
0,1,1,2,3,5,8,13,21,34⋯
Derivation of the general formula
It is possible to derive a general formula for without computing all the previous numbers in the sequence. If a gemetric series (i.e. a series with a constant ratio between consecutive terms
) is to solve the difference equation, we must have
which is equivalent to
This equation has two unique solutions
In particular the larger root is known as the golden ratio
Now, since both roots solve the difference equation for Fibonacci numbers, any linear combination of the two sequences also solves it
It’s not hard to see that all Fibonacci numbers must be of this general form because we can uniquely solve for a and b such that the initial conditions of and
are met
yielding
We have therefore derived the general formula for the n-th Fibonacci number
Since the second term has an absolute value smaller than 1, we can see that the ratios of Fibonacci numbers converge to the golden ratio
Python code:
def findFibIndexValue(index):
golden_ratio = (1 + math.sqrt(5)) / 2
return round(((golden_ratio ** index) - ((1 - golden_ratio) ** index)) / math.sqrt(5))
2. Find index of a particular number in fibonacci series
We know that
where
and
where “round” means round to the nearest integer. For speed of computation we should simplify this to:
where the 𝛼 and 𝛽 constants are precomputed as:
Python code:
def findFibIndex(val):
return round(2.078087 * math.log(val) + 1.672276)
3. Count number of digits in the nth Fibonacci number
The above formula can be simplified,
Count of digits in Fib(n)
=
=
=
=
Python code:
phi=(1+5**.5)/2defnumberOfDig (n) :ifn==1:return1returnmath.ceil((n*math.log10(phi)-.5*math.log10(5)))