From 2c57dec99f69a5c187dd1b07edf6db062c6c6b3c Mon Sep 17 00:00:00 2001 From: Tatiana Quintana Date: Fri, 29 Mar 2019 14:37:38 -0700 Subject: [PATCH 1/4] first way to solve this algorithm --- lib/factorial.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 6a4a408..86b52ce 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -1,6 +1,15 @@ # Computes factorial of the input number and returns it -# Time complexity: ? -# Space complexity: ? +# Time complexity: On^n +# Space complexity: ?? def factorial(number) - raise NotImplementedError + raise ArgumentError, "Bad Value" if number == nil + + return 1 if number == 0 || number == 1 + + less = number - 1 + until less == 0 + number *= less + less -= 1 + end + return number end From 7731927e8f620867cb0702142f6c603b451170bb Mon Sep 17 00:00:00 2001 From: Tatiana Quintana Date: Fri, 29 Mar 2019 14:55:18 -0700 Subject: [PATCH 2/4] "tests pass" --- lib/factorial.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 86b52ce..819d1eb 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -12,4 +12,4 @@ def factorial(number) less -= 1 end return number -end +end \ No newline at end of file From 0827c2fae21d7dbe3c892c52f99ebcbf1c1881ac Mon Sep 17 00:00:00 2001 From: Tatiana Quintana Date: Mon, 8 Apr 2019 16:51:14 -0700 Subject: [PATCH 3/4] added space complexity --- lib/factorial.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 819d1eb..20c2456 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -1,6 +1,6 @@ # Computes factorial of the input number and returns it # Time complexity: On^n -# Space complexity: ?? +# Space complexity: Constant def factorial(number) raise ArgumentError, "Bad Value" if number == nil From 1238ed2b87001a1bbe7a785802c63b9bfae480b9 Mon Sep 17 00:00:00 2001 From: Tatiana Quintana Date: Thu, 11 Apr 2019 13:43:36 -0700 Subject: [PATCH 4/4] added clearer variable name --- lib/factorial.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 20c2456..314e0b0 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -1,15 +1,15 @@ # Computes factorial of the input number and returns it -# Time complexity: On^n +# Time complexity: On! # Space complexity: Constant def factorial(number) raise ArgumentError, "Bad Value" if number == nil return 1 if number == 0 || number == 1 - less = number - 1 - until less == 0 - number *= less - less -= 1 + minus_one = number - 1 + until minus_one == 0 + number *= minus_one + minus_one -= 1 end return number end \ No newline at end of file