Skip to content

Instantly share code, notes, and snippets.

@mike-burns
Created June 20, 2013 22:59
Show Gist options
  • Save mike-burns/5827527 to your computer and use it in GitHub Desktop.
Save mike-burns/5827527 to your computer and use it in GitHub Desktop.
Is a number in the set of Fibonacci numbers?
module IsFibber
def fibonacci?(n)
perfect_square?(5 * n * n + 4) || perfect_square?(5 * n * n - 4)
end
private
def perfect_square?(f)
sqrt = Math.sqrt(f)
sqrt == sqrt.round
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment