Skip to content

Instantly share code, notes, and snippets.

@pjlowry
Created January 28, 2013 23:30
Show Gist options
  • Save pjlowry/4660278 to your computer and use it in GitHub Desktop.
Save pjlowry/4660278 to your computer and use it in GitHub Desktop.
0.1.1.2.3....infinity recursively
class Integer
def fibonacci
if self < 0
"Must be a positive number"
elsif self == 0
0
elsif self == 1
1
else
return (self - 1).fibonacci + (self - 2).fibonacci
end
end
end
puts "'#{8.fibonacci}' should equal '21'"
puts "'#{1.fibonacci}' should be '1'"
puts "'#{2.fibonacci}' should be '1'"
puts "'#{3.fibonacci}' should be '2'"
puts "'#{4.fibonacci}' should be '3'"
puts "'#{0.fibonacci}' should be '0'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment