diff --git a/lib/factorial.rb b/lib/factorial.rb index 6a4a408..ca76bfb 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -1,6 +1,17 @@ # Computes factorial of the input number and returns it -# Time complexity: ? -# Space complexity: ? -def factorial(number) - raise NotImplementedError +# Time complexity: O(n), linear, with n being the number input. +# Space complexity: O(n), linear, with n being the number input for the factorial. + +def factorial(num) + if num == nil || num < 0 + raise ArgumentError, "Must input positive number." + end + + factorial = 1 + + num.times do + factorial *= num + num = num - 1 + end + return factorial end