Skip to content

Instantly share code, notes, and snippets.

@abrahamsangha
Created May 9, 2013 11:27
Show Gist options
  • Select an option

  • Save abrahamsangha/5546940 to your computer and use it in GitHub Desktop.

Select an option

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.
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