Created
December 9, 2015 19:09
-
-
Save Bodacious/6d607af3cca4aefb1582 to your computer and use it in GitHub Desktop.
Another approach to fibonacci in Ruby
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
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