Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Created December 9, 2015 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bodacious/6d607af3cca4aefb1582 to your computer and use it in GitHub Desktop.
Save Bodacious/6d607af3cca4aefb1582 to your computer and use it in GitHub Desktop.
Another approach to fibonacci in Ruby
class Integer
# PHI - Some greek number
PHI = Rational(1836311903,1134903170).to_f
# Inverse of 1/PHI
I_PHI = -Rational(1, PHI).to_f
# Square root of 5
SQRT_5 = 5 ** 0.5
def fibonacci
raise ArgumentError, "Can't return fibonacci value for integer < 0" if self < 0
return ((PHI**self - I_PHI**self) / SQRT_5).round
end
end
puts 100.fibonacci
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment