Created
October 25, 2013 17:50
-
-
Save mosampaio/7158889 to your computer and use it in GitHub Desktop.
Recursive factorial 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
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