12 ms·
Fast Fibonacci
- _delirium 12y agoA nitpick: > If we wanted to compute a single term in the sequence (e.g. F(n)), there are a couple of algorithms to do so, but some algorithms are much faster than others. If we wanted only a single term in the sequence, we would just use the closed-form equation, which takes O(1) bigint arithmetic operations, F(n) = (phi^n - (-phi)^-n) / sqrt(5) (Where phi is the golden ratio.) But if what's needed is the entire sequence F(0) up through a chosen F(n), then the algorithms in this article come in handy.
- jfarmer 12y agoThat quickly fails due to the nature of floating point arithmetic.
- andrewla 12y agoTo pick a little further at this nit; bigint is no good, right? The only way to compute phi^n with bigints is to do an O(log n) operations in the algebraic integers Z(5) (similar to exponentiating complex integers). Doing it with floats/doubles is a little more reasonable for an O(1) approach, but even that approach breaks down for large enough n.
- nayuki 12y agoRegarding the computation of a single term: No, the well-known Binet formula involving phi is unsuitable because it uses real numbers. See these detailed comments about handling quadratic surds properly, which is effectively the same as the fast doubling algorithm: - Chinjut: https://news.ycombinator.com/item?id=9316574 https://news.ycombinator.com/item?id=9316574 - xyzzyz: https://news.ycombinator.com/item?id=9316144 https://news.ycombinator.com/item?id=9316144 Regarding the computation of F(0) through F(n): No, this is not true either. The matrix method and fast doubling only compute O(log(n)) terms of the sequence, not the complete sequence from 0 to n.
- _delirium 12y agoAh, right, I am wrong on both counts. Thanks for the pointers!
- shasta 12y agoAs andrewla notes, big int operations aren't O(1) in an apples-to-apples comparison. In fact, if you work out all of the math for a Z(5) implementation, this approach is essentially the same as the fast matrix exponentiation approach from the article. As I recall, they differ only by an application of some Fibbonaci identity (because of these identities, there are many equivalent recurrence relations).
- arh68 12y agoGreat writeup! I only wish the author had benchmarked the approximate algorithm with phi and floating points too, because it's often 'good enough' >>> fib = lambda n: 5**-.5*((.5+.5*5**.5)**n - (.5-.5**.5)**n) >>> fib(12) 144.00138887270816 >>> fib(250) 7.896325826131797e+51 EDIT: Laughing, don't know why I called it factorial at first. Whoops
- omaranto 12y agofactorial(n) is an odd choice of name for the nth Fibonacci number. You should at the very least provide a doc string! :)
- arh68 12y agoThank you. The 'code' is bad enough, would never pass review ;)
- ddlatham 12y agoAgreed, a benchmark would be cool. From what I've read before it ends up taking longer than the integer methods anyway because you still need to keep log n bits of precision in the floating point values around to get the correct answer. Perhaps if you had a use case for approximate values of very large fibonacci numbers it would be useful? I'd love to hear that use case.
- jules 12y agoThat only works for the first 20 or so Fibonacci numbers due to limited floating point precision, so then you could also use a 20 entry lookup table.
- nayuki 12y agoIt would be incomparable because the 4 algorithms described on the page produce exact integer results, whereas a floating-point algorithm will not have enough precision to represent larger numbers.
- xyzzyz 12y ago
- spoonman1 12y agoMaybe someone can help me understand a bit better. The author talks about how slow a recursive algorithm is, but the "fast doubling" implementation itself uses recursion. Would not the same slower effect be felt by this implementation due to its use of recursion?
- ubernostrum 12y agoThe author is not saying "every algorithm which uses recursion is slow". The author is saying that the naive recursive algorithm is incredibly slow. That would be an implementation like this (in Python): def fib(n): if n < 2: return n else: return fib(n-1) + fib(n-2) And that implementation is slow. It's exponentially slow.
- nayuki 12y agoAgreed, this response is exactly correct. I didn't realize that my wording of "slow recursion" could be misread as "every recursion is slow", which is not true - especially because every iterative procedure can be expressed as a recursive procedure with the same time complexity. As a side note, the Java and C# implementations of the "fast doubling" algorithm actually used a fixed number of loop iterations instead of the mathematical recursive formula.
- brudgers 12y agoevery iterative procedure can be expressed as a recursive procedure. To use Ableson's precise language: Every iterative procedure can be expressed as a recursive *definition.* SICP is written to carefully avoid overloading terms: "Function" is reserved for mathematical functions and "closure" is reserved for algebraic closure.
- agumonkey 12y agoVery Sussman-y. His distate for ambiguity is very coherent.
- 12y ago
- cabinpark 12y agoGood stuff! I love Fibonacci algorithms and if you dig deeper, there are even faster algorithms that exploit even more identities like the fast doubling and look-up tables. It's a fun recreational mathematics thing, but unfortunately I've never come across a problem where I need to solve Fibonacci insanely quickly. However I still keep my hopes up!
- xtagon 12y agoJust a guess, but it might be useful for fractal rendering. That's the only real application I can think of.
- eru 12y agoThe techniques developed are useful in other areas. Eg Double_Cast talks about fast matrix multiplication above---certainly a useful technique elsewhere.
- brudgers 12y agoFibonacci numbers are so fun that Fibonacci Quarterly has been around for 52 years. http://www.fq.math.ca/ http://www.fq.math.ca/
- userbinator 12y agoInteresting to see that "slow dynamic programming" which is the classic exchange-and-add algorithm is the fastest up to around n=126, at which point fast doubling with naive multiplication takes the lead, and Karatsuba multiplication needs n over 5000 before it becomes faster than the other methods. But, assuming that you can much more easily get an order of magnitude improvement out of the "simple and slow" method than the more complex ones, then the break-even point moves up to n=1260. In other words, always choosing the asymptotically fastest (and often more complex - with larger constants) algorithms is a bad decision if most of your inputs never get large enough; simpler algorithms are good, even if they're not the theoretically fastest, because they also tend to be easier to optimise. However, the recursive algorithm being faster than slow-DP until n=5 is a little odd... some other effect could be responsible for that.
- brudgers 12y agoOptimizing on Big-O is always about working with interestingly sized data. If we're going to be doing lots of calculations and only talking about a limited range of possible values, memoization is asymptotically O(1) time.
- ksjjsk 12y agoyou know its actually an O(logn) operation with fast exponentiation through squaring..... http://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_the_golden_ratio http://en.wikipedia.org/wiki/Fibonacci_number#Relation_to_th...
- TheLoneWolfling 12y agoIt would be interesting to see what the asymtotic complexity would be in terms of number of (fixed-size) operations as opposed to number of bigint operations required. I mean, the nth Fibonacci number requires O(n) bits to store.
- fdej 12y agoIt's O(M(n)) where M(n) is the complexity of the algorithm used for multiplying n-bit integers. To see why, note that each step of the matrix powering or fast doubling algorithm 1) costs a constant C number of multiplications, and 2) roughly doubles the number of bits. F(n) has b = O(n) bits, so the cost is essentially C M(b) + C M(b/2) + C M(b/4) + ... <= 2 C M(b) = O(M(n)), using the natural assumption that M(2x) >= 2M(x).
- TheLoneWolfling 12y agoSo then: is there any better solution? Or is that the optimal?
- fdej 12y agoIt's almost certainly optimal, but proving that is likely an open problem.
- balls187 12y agoOne of my warm up questions is to ask to code Fib. It's easy enough to explain and leads to interesting questions about performance. I'm still waiting for a candidate to break out Binet's formula.
- brudgers 12y agoInteresting. Still runs in O(log n). Link: http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.ht...
- Double_Cast 12y agoThe Fast Fibonacci article links to an article on an algorithm called "karatsuba-multiplication". Oi - I just lost two hours of my life to reverse-engineering it. (But it was worth!). In case anyone else was wondering, here's what I got out of it. x*y = (x)(y) // fragment into binomials = (x/m*m + x%m)(y/m*m + y%m) // uses integer division // distribute binomials = (x/m*m)(y/m*m) + (x/m*m)(y%m) + (x%m)(y/m*m) + (x%m)(y%m) // extract modulus from each term = [(x/m)(y/m)]m^2 + [(x/m)(y%m) + (x%m)(y/m)]m + [(x%m)(y%m)] // rename terms = [A]m^2 + [B+C]m + [D] // where [A] is what nayuki calls (a), // [B+C] is what nayuki calls (a-b-c), and // [D] is what nayuki calls (c). Q.E.D. Geometrically, what we're doing is: 1) taking the area of two binomials; 2) shrinking Quadrant A by a factor of the modulus's perfect-square (e.g. 3^2); 3) shrinking Quadrant B and Quadrant C by a factor of the modulus per se (e.g. 3); and 4) leaving Quadrant D the same. [B B B D D] scaled by modulus [B D D] [A A A C C] ----------------> [A C C] [A A A C C] where m = 3 [A A A C C]
- jordigh 12y ago> I just lost two hours of my life to reverse-engineering it. I wouldn't call that "lost", myself. Good job. :-) There is also Strassen's algorithm for matrix multiplication. Something to keep in mind if you ever need to multiply really gigantic matrices. https://en.wikipedia.org/wiki/Strassen_algorithm https://en.wikipedia.org/wiki/Strassen_algorithm
- Chinjut 12y agoYou've left out the key observation that makes Karatsuba multiplication faster than naive multiplication: after having computed [A] and [D], we can compute the combined [B + C] with one multiplication rather than two. That is, to multiply two binomials of a given size, we only need three multiplications of binomials of half that size, rather than the naive four. This is what improves the runtime of Karatsuba multiplication to Ө(n^(log_2(3))) from the ordinary Ө(n^2). Why are we able to get away with only three recursive multiplications instead of four? The observation here is that in the product (Em + F)(Gm + H) = EGm^2 + (EH + FG)m + FH, after the first and last coefficients EG and FH have already been calculated, the middle coefficient (EH + FG) can be calculated using only the one new multiplication in (E + F)(G + H) - EG - FH, rather than the naive two in its definition.
- thomasfromcdnjs 12y agobm
- ggchappell 12y agoFun stuff. But why only take n up to around 4 million? Not too long ago, I was able to compute the billionth Fibonacci number on a pretty ordinary machine, using a Haskell implementation of the fast doubling method. (The number took 20 minutes to print out in my console window.) By the way, I did a Python implementation, too. And I learned that GHC large-integer multiplication is a lot faster than CPython large-integer multiplication. I wonder why. (No, it has nothing to do with dynamic typing; almost all of the time is going to be spent executing internal integer-multiplication routines written in C.)