Skip to content

Instantly share code, notes, and snippets.

@DevoKun
Created October 23, 2014 19:57
Show Gist options
  • Save DevoKun/ce90142237a9fb214545 to your computer and use it in GitHub Desktop.
Save DevoKun/ce90142237a9fb214545 to your computer and use it in GitHub Desktop.
Ruby for calculating a factorial using recursion.
#!/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