Skip to content

Instantly share code, notes, and snippets.

@bf4
Forked from amitkgupta/is_fibonacci.rb
Created October 22, 2015 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/7d5cea255c9df98aee37 to your computer and use it in GitHub Desktop.
Save bf4/7d5cea255c9df98aee37 to your computer and use it in GitHub Desktop.
A very fast, near-constant runtime algorithm for determining whether a given number is Fibonacci, accurate up to numbers with trillions of trillions of digits (or something like that)
def is_fibonacci?(num)
n = (Math.log(num*2.23606797749979 + 0.5) * 2.0780869213681945).floor
num == (1.6180339887**n * 0.4472135954999579).round
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment