Created
May 9, 2013 11:27
-
-
Save abrahamsangha/5546940 to your computer and use it in GitHub Desktop.
Check if a number i is part of the Fibonacci sequence. Attempted to use the integer square root method so that large numbers could be arguments, but no dice, yet.
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
| def is_fibonacci?(i) | |
| x = 5 * i**2 + 4 | |
| m = x | |
| p = x | |
| loop do | |
| r = (m + p.to_f / m).to_f / 2 | |
| if m <= r | |
| break | |
| else | |
| m = r | |
| end | |
| end | |
| y = 5 * i**2 - 4 | |
| n = y | |
| o = y | |
| loop do | |
| q = (n + o.to_f / n).to_f / 2 | |
| if n <= q | |
| break | |
| else | |
| n = q | |
| end | |
| end | |
| m.denominator == 1 || n.denominator == 1 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment