From cba450f1c7406215e95d5a1baa1d2ac2852f3944 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Wed, 10 Apr 2019 21:52:39 -0700 Subject: [PATCH 1/2] created factorial method --- lib/factorial.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 6a4a408..4f39cb2 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: ? +# Time complexity: O(n), where n is the number passed in +# Space complexity: O(1), constant because no additional space is needed + def factorial(number) - raise NotImplementedError + if number == nil || number < 0 + raise ArgumentError, "Enter a positive integer" + end + + factorial = 1 + while number > 1 + factorial *= number + number -= 1 + end + + return factorial end From a5a16c4b41a1837c2a3774063b2b32f1664bed91 Mon Sep 17 00:00:00 2001 From: Amy Phung Date: Wed, 10 Apr 2019 21:54:22 -0700 Subject: [PATCH 2/2] removed weird spacing --- lib/factorial.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 4f39cb2..5fd34fa 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -12,6 +12,5 @@ def factorial(number) factorial *= number number -= 1 end - return factorial end