1

Looking for an algorithm that will give me the number of members that will result from calculating a Fibonacci series, given a particular limit.

For example, if I start the series at 1 and limit my results to <= 10000, determine the number of members such a Fibonacci series will contain. (I am writing some software that builds a Fibonacci series to a given limit: Before the calculation begins, it would be more efficient to know in advance how much memory I need to allocate for a container that will hold all the resulting members of the series)

Vector
  • 113

1 Answers1

2

You can use the formula $$F_n = \left\lfloor\frac{\varphi^n}{\sqrt5}+\frac{1}{2}\right\rfloor$$

Say your bound is $N$. You then want to find the first $n$ such that $$\frac{\varphi^n}{\sqrt5}+\frac{1}{2}\ge N+1$$ which can be rephrased as $$n\ge \log_\varphi\sqrt5\left(N+\frac{1}{2}\right)$$ Thus, given $N$ you can take $$n=\left\lceil\log_\varphi\sqrt5+\log_\varphi\left(N+\frac{1}{2}\right)\right\rceil-1$$ to be the last $n$ such that $F_n\le N$

  • This looks good but I'm still working on converting it into Golang so I can test it out - need a bit of help, I'm not very familiar with GoLang math libraries and representations. – Vector Mar 13 '14 at 16:09