Skip to content

Instantly share code, notes, and snippets.

@pjlowry
Created January 24, 2013 23:58
Show Gist options
  • Save pjlowry/4629917 to your computer and use it in GitHub Desktop.
Save pjlowry/4629917 to your computer and use it in GitHub Desktop.
! boom !
def factorial(n)
if n <= 1
"1"
else (1..n).inject {|product, nb| product * nb }
end
end
puts "'#{factorial(5)}' should equal '120'."
puts "'#{factorial(0)}' should equal '1'."
puts "'#{factorial(-5)}' 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