0

So if I take a number, $n=10$, $$f(n)=6$$ Because there are only 6 Fibonacci terms below it ($1,1,2,3,5,8$). So is there any formula for this?

Rayreware
  • 980

2 Answers2

3

Well, the $k$th fibonacci number is $$ a_k = \frac{ \left(\frac{1+\sqrt{5}}{2}\right)^k - \left(\frac{1-\sqrt{5}}{2}\right)^k }{\sqrt{5}}. $$ which is very nearly equal to $$ b_k = \frac{ \left(\frac{1+\sqrt{5}}{2}\right)^k }{\sqrt{5}}. $$ If $n = b_k$, then $$ \ln(n) = k \ln \left(\frac{1+\sqrt{5}}{2}\right) - \ln(\sqrt{5}) $$ so $$ \ln(\sqrt{5}n) = k \ln \left(\frac{1+\sqrt{5}}{2}\right) $$ so $$ k = \frac{\ln(\sqrt{5}n)}{\ln \left(\frac{1+\sqrt{5}}{2}\right) } $$ So your answer is that for large $n$ (perhaps $20$ or so?) the number of fibonacci numbers less than or equal to $n$ is (very nearly) $$ k = \left\lfloor \frac{\ln(\sqrt{5}n)}{\ln \left(\frac{1+\sqrt{5}}{2}\right) } \right\rfloor $$

If you want the exact value, you can use this estimate of $k$, and then compute the $k-1$st, $k$th, and $k+1$st Fibonacci numbers (using the first formula) and then see which ones are less than $n$, and you'll have your answer.

A reasonably practical alternative is to start with twice the estimated value I've given for $k$, and then perform a binary search to find the largest $i$ for which $a_i < n$. Since you'll be computing powers of $\phi$ and $1/\phi$, you can probably cache the computed values and do this really quickly.

John Hughes
  • 93,729
1

If you want to find $n$ such that $$F_n=k$$ you could use Binet formula $$F_n = \frac{\varphi^n-\psi^n}{\varphi-\psi} = \frac{\varphi^n-\psi^n}{\sqrt 5} $$ $$\varphi = \frac{1 + \sqrt{5}}{2}\qquad \text{and} \qquad \psi=\frac 1 \varphi$$ As soon as $n$ becomes large, $\psi^n$ become negligible making $$F_n \sim \frac{\varphi^n}{\sqrt 5} $$ Now, take logarithms to get $n$.