Created
June 20, 2013 22:59
-
-
Save mike-burns/5827527 to your computer and use it in GitHub Desktop.
Is a number in the set of Fibonacci numbers?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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