Created
October 23, 2014 19:57
-
-
Save DevoKun/ce90142237a9fb214545 to your computer and use it in GitHub Desktop.
Ruby for calculating a factorial using recursion.
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
#!/usr/bin/env ruby | |
def factorial(i) | |
if (i > 0) then | |
print i | |
return i = i * factorial(i - 1) | |
end ### if | |
return 1 | |
end ### def | |
puts | |
print " = " + factorial(5).to_s | |
puts | |
# | |
# Outputs: | |
# 54321 = 120 | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment