# Should use closed-form fibonacci function #1

Closed
opened this Issue Oct 3, 2011 · 14 comments

None yet

### 6 participants

commented Oct 3, 2011
 It would be much more performant to calculate the fibonacci number like so: ```function fib (n) { var a=1 , b=0 while (0 > n--) { var c = b b = b + a a = c } return b }``` Perhaps it might make sense to add this as a separate endpoint?
Owner
 I've now implemented this as in #7 - which keeps the recursive step - thus making it easier to work in releasing the loop periodically.
closed this Oct 4, 2011
commented Oct 4, 2011
 Actually there's a logarithmic-time algorithm, guys... Google it.
commented Oct 4, 2011
 @jkff GIST OR GTFO.
commented Oct 4, 2011
 ``````while (0 > n--) { `````` Should be < instead of >.
commented Oct 4, 2011
 True that. need tdd on this thing.
commented Oct 4, 2011
commented Oct 4, 2011
 Seriously guys, it's O(1). ``````var PHI = (1 + Math.sqrt(5)) / 2; function fib(n) { return Math.round(Math.pow(PHI, n) / Math.sqrt(5)); } ``````
Owner
 See issue #4 On 4 Oct 2011, at 18:34, leonth reply@reply.github.com wrote: Seriously guys, it's O(1). ``````var PHI = (1 + Math.sqrt(5)) / 2; function fib(n) { return Math.round(Math.pow(PHI, n) / Math.sqrt(5)); } `````` Reply to this email directly or view it on GitHub: #1 (comment)
commented Oct 4, 2011
 @leonth For which range of inputs does it give an exact answer?
commented Oct 4, 2011