Devin Naquin

Jan 24

Elegant Fibonacci Iterator

After a bunch of versions of Fibonacci functions months back, the following iterator appeared in my notebook.

def fib():

    future = [0, 1]

    while True:

        future.append(sum(future))

        yield future.pop(0)

Though of no particular engineering marvel. It is, at O(n), slower than the best case O(log(n)) I can do using matrix multiplication of powers of two.

I find it particularly elegant.


Comments (View)
blog comments powered by Disqus
Page 1 of 1