Last active
December 22, 2015 08:38
-
-
Save kexline4710/6445884 to your computer and use it in GitHub Desktop.
This is my method to check if a given number is in the Fibonacci sequence by using the equation in the 'recognizing fibonacci numbers' section of this wikipedia page: http://en.wikipedia.org/wiki/Fibonacci_number#Recognizing_Fibonacci_numbers. I get that this can't handle large numbers as Float isn't precise enough at a certain point. Through my…
This file contains 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) | |
fib1 = Math.sqrt((5*(i**2)+4)) | |
fib2 = Math.sqrt((5*(i**2)-4)) | |
puts fib1 == fib1.floor || fib2 == fib2.floor ? true : false | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def is_fibonacci?(i)
fibs = [0,1]
while fibs.last < i
x = fibs.pop
y = fibs.pop
z = x + y
fibs << x
fibs << z
end
fibs.last == i ? true : false
end