Skip to content

Instantly share code, notes, and snippets.

@syrusakbary
Last active June 9, 2024 12:33
Show Gist options
  • Save syrusakbary/7831365 to your computer and use it in GitHub Desktop.
Save syrusakbary/7831365 to your computer and use it in GitHub Desktop.
Fibonacci algorithm in O(1) ;)
SQRT_5 = Math.sqrt(5)
GOLDEN_NUMBER = (1+SQRT_5)/2
def fib(n)
(1/SQRT_5*(GOLDEN_NUMBER**n-(-1/GOLDEN_NUMBER)**n)).to_i
end
puts fib(7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment