Skip to content

Instantly share code, notes, and snippets.

@tyler
Created June 3, 2010 03:26
Show Gist options
  • Save tyler/423402 to your computer and use it in GitHub Desktop.
Save tyler/423402 to your computer and use it in GitHub Desktop.
# Fibonacci numbers in logarithmic time
def even?(n)
n % 2 == 0
end
def fib(n)
a = 1
b = 0
p = 0
q = 1
while n > 0
if even?(n)
op = p
oq = q
p = op**2 + oq**2
q = oq * (2*op + oq)
n /= 2
else
oa = a
ob = b
a = ob*q + oa*q + oa*p
b = ob*p + oa*q
n -= 1
end
end
b
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment