From 0b66cfdce59ffd391c2041b013ee1e1f6c3e0ef5 Mon Sep 17 00:00:00 2001 From: laneia Date: Wed, 19 Jun 2019 14:28:51 -0700 Subject: [PATCH] factorial solution --- lib/factorial.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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