Skip to content

Instantly share code, notes, and snippets.

@kexline4710
Last active December 22, 2015 08:38
Show Gist options
  • Save kexline4710/6445884 to your computer and use it in GitHub Desktop.
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…
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
@kexline4710
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment