On the Linear Time Algorithm For Finding Fibonacci Numbers
catonmat.net
catonmat.net
Here's a link to the closed form solution: http://en.wikipedia.org/wiki/Fibonacci_number#Closed_form_ex...
I'd be surprised if there isn't a true linear time algorithm for Fibonacci, it's just that it's more subtle than people think.
PS: Finding an optimal sequence of multiples and squares for computing an arbitrary power is still an open question.
I'm trying to figure out if you're intentionally making a sophisticated math joke or not -- and failing. Are you?
The precision to which you need to compute phi in order to compute the Nth Fibonacci number by exponentiation is the precision you'd obtain by computing the ratio of the Nth and (N-1)th Fibonacci numbers -- which is exactly how you evaluate the continued fraction for phi.
So your suggestion amounts to "compute the Nth Fibonacci number... then do a lot of work to use that value to compute the Nth Fibonacci number". :-)
That might just work in a lazy language.
> naturals = 0 : (map (+1) naturals)
actually works in Haskell.
(In Haskell a:b means the same as (cons a b) in Lisp. I also added parens to make the definition more readable for non-Haskellers.)
If you try to do the same thing for Fibonacci numbers, you would need to express F(n) in terms of earlier values, not in terms of F(n). What cperciva is jokingly proposing is the equivalent of:
naturals = naturals
which is hardly helpful for computing the values. Lazy languages would not help you there.This seems true empirically, but when I do the math, it looks like you only need to calculate F_n up to about N/2 to make the ratio precise enough. (Since:
|phi - (F_(n+1)/F_n)| < (F_n)^(-2) = phi^(-2n)/sqrt(5) (approximately)
And, if |a|>>|b|, then: |(a+b)^n - a^n| = n a^(n-1) b (approximately)
So our error just has to be < 1/(n phi^(n-1)), which should be true for roughly the N/2 convergent.) In either case, the algorithm is still basically quadratic.True -- I oversimplified. But computing both F_n and F_{n+1} (as you need in order to compute the ratio) takes effectively the same amount of time as computing F_{2n}, so it still doesn't win you anything.
Gosper showed that it's possible to compute the CF of Phi^2 (say) directly, without evaluating the CF of Phi. Lather, rinse, repeat. The numbers involved are all similar in size to F(n) and so the complexity is linear. The number of multiplications and squarings is log(n), so I think the whole thing is sub-quadratic. Possibly it's n log(n) rather than linear.
The point is that there are fast ways of doing these sorts of things, but they tend to be little-known and subtle. I've not gone into the details, there may be something I've missed.
In that case you can keep the value of sqrt(5) as sqrt(5) knowing it will cancel out at some point.
Overlooking this would lead one to believe there are known algorithms that solve NP-C problems in polynomial time (e.g., knapsack problem can be solved in polynomial time with respect to the decimal values of its inputs, but exponential with respect to the length of the binary encoding).
That doesn't make any sense. The number of binary and decimal digits has a linear relation. I think the ratio is log(10) / log(2) or about 3.32 .
the other is that addition of two D-digits numbers takes O(D) time (this is what the author means to expose in the article). as you have pointed out, the base (10, or 2, or anything else) does not matter for asymptotic analysis here, because the logarithm functions are all linearly related.
http://en.wikipedia.org/wiki/Fibonacci_Numbers#Closed_form_e...
Couldn't there be a faster way to evaluate it using an algebraic rather than a computational way? I just find the bound you give too high but I may be completely wrong.