Skip to content

Instantly share code, notes, and snippets.

@michaelrkn
Forked from pjlowry/factorial.rb
Last active December 11, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelrkn/4630007 to your computer and use it in GitHub Desktop.
Save michaelrkn/4630007 to your computer and use it in GitHub Desktop.
def factorial(n)
if n < 0
raise "You can't take the factorial of a negative number."
elsif n == 0
1
else
(1..n).inject(:*)
end
end
puts "'#{factorial(5)}' should equal '120'."
puts "'#{factorial(0)}' should equal '1'."
puts "'#{factorial(25)}' should equal '15511210043330985984000000'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment