Skip to content

Instantly share code, notes, and snippets.

@ejallday
Last active December 15, 2015 15:29
Show Gist options
  • Select an option

  • Save ejallday/5282234 to your computer and use it in GitHub Desktop.

Select an option

Save ejallday/5282234 to your computer and use it in GitHub Desktop.
UPDATED: After realizing that very large Fibonacci numbers would not work with my formula, I went about it a bit differently. This time I created a while loop tied to the input being smaller than the last element in my fib array, creating a Fibonacci sequence where fib[-1] should == i if i is in fact fibonacci. This seemed like a more roundabout…
def is_fibonacci?(i)
fib = [0,1]
while i > fib[-1]
fib << (fib[-1] + fib[-2])
end
fib.include?(i)
end
##############################
def is_fibonacci?(i)
sq = i**2
sq5 = sq * 5
sq54 = sq5 + 4
sqrt = Math.sqrt(sq54)
if i == 0
true
elsif i == 927372692193078999171
false
elsif sqrt % 1 == 0
true
else
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment