Saturday, August 17, 2013

Coderbyte: First Factorial (Ruby)

PROBLEM

Have the function FirstFactorial(num) take the num parameter being passed and return the factorial of it (ie. if num = 4, return (4 * 3 * 2 * 1)). For the test cases, the range will be between 1 and 18.

SOLUTION

def FirstFactorial(num)
  result = 0
  num.downto(1) { |number| result == 0 ? result = number : result *= number }
  return result  
end

print FirstFactorial(STDIN.gets)