From e22a9636fc922d056e783c2de0ab4ee018b2e244 Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Thu, 11 Apr 2019 00:28:51 -0700 Subject: [PATCH 1/2] added factorial method --- lib/factorial.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 6a4a408..5fb4ce1 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -2,5 +2,14 @@ # Time complexity: ? # Space complexity: ? def factorial(number) - raise NotImplementedError + if number == nil + raise ArgumentError + end + + i = 1 + while number > 0 + i = number * i + number -= 1 + end + return i end From a39668a545f9b3e47d1555836f9c7025b097d68f Mon Sep 17 00:00:00 2001 From: Ngoc Le Date: Thu, 11 Apr 2019 00:29:53 -0700 Subject: [PATCH 2/2] added time and space complexities --- lib/factorial.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 5fb4ce1..355c45a 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -1,6 +1,6 @@ # Computes factorial of the input number and returns it -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def factorial(number) if number == nil raise ArgumentError