Skip to content

Instantly share code, notes, and snippets.

@mosampaio
Created October 25, 2013 17:50
Show Gist options
  • Save mosampaio/7158889 to your computer and use it in GitHub Desktop.
Save mosampaio/7158889 to your computer and use it in GitHub Desktop.
Recursive factorial in Ruby
def factorial(n)
n>1 ? n * factorial(n-1) : n
end
puts factorial(1) # 1
puts factorial(2) # 2
puts factorial(3) # 6
puts factorial(4) # 24
puts factorial(5) # 120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment