Re:"The worst algorithm in the world?"
by shasta on Wednesday May 11, 2011 @10:59PM
(#2539326)
Well, both have computed the Fibonacci terms at a given level, so how different could it be? Here's my implementation:
It's almost identical runtime as the one in the article - a hair slower (15.32s vs. 16.17s to compute fib 10M). They're probably related by some well known relation between Fibonacci numbers.
[ Parent
| on HN ]
Re:"The worst algorithm in the world?"
by robinhouston on Thursday May 12, 2011 @02:32PM
(#2541752)
That’s very nice! And a little rearrangement will bring it from three to two “big multiplications”, making it faster than my routine:
def fib_ring2(n):
assert n >= 0
a, b = 2, 0 # invariant: phi^n = (a + b*sqrt(5)) / 2
for bit in bits(n):
ab = a*b
a, b = (a+b)*((a+5*b)//2) - 3*ab, ab
if bit: a, b = (a + 5*b)//2, (a+b)//2
return b