Factorial in Ruby
Example for versions
Ruby 1.8.5
This example uses recursive factorial definition.
#! /usr/bin/env ruby
def factorial(n)
if n == 0
1
else
n * factorial(n - 1)
end
end
0.upto(16) do |n|
print(n, "! = ", factorial(n), "\n")
end
Comments
]]>blog comments powered by Disqus
]]>