r/programming • • Oct 03 '11

Node.js Cures Cancer

http://blog.brianbeck.com/post/node-js-cures-cancer
390 Upvotes

329 comments sorted by

View all comments

Show parent comments

18

u/angch Oct 03 '11

Or just use Binet's Fibonacci Number Formula. O(1), if you have decent exponential implementation.

5

u/moonrocks Oct 03 '11

That's stunning. Thanks for the link.

2

u/JAPH Oct 03 '11

You can come up with a formula like that for many (most?) recurrence relations.

1

u/julesjacobs Oct 03 '11 edited Oct 03 '11

You can do it for any linear recurrence by writing it as a matrix power and then using the eigenvalue formula. http://en.wikipedia.org/wiki/Diagonalizable_matrix

This is however a stupid thing to do from a computational perspective, because simply computing the matrix power is cheaper. This also holds for fibonacci numbers.

1

u/julesjacobs Oct 03 '11 edited Oct 03 '11

O(1) exponential implementations do not exist, for the simple reason that the output is already O(n) bits long.

Using Binets formula for calculating fibonacci numbers is stupid because you need to use arbitrary precision arithmetic. How many digits of precision suffice? Unknown. If you use floating point your algorithm will certainly already fail for the 100th fibonacci number.

They are probably using an integer recurrence like this one http://gmplib.org/manual/Fibonacci-Numbers-Algorithm.html