From f0108895c34df778810ac26995fcef7c94a998dc Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Mon, 8 Apr 2019 21:15:01 -0700 Subject: [PATCH 1/3] created factorial method --- lib/factorial.rb | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 6a4a408..19a7c2e 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -1,6 +1,47 @@ # Computes factorial of the input number and returns it -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), where n is the number passed into the method. +# Space complexity: Constant, O(1), the only extra space used is for the variable x + def factorial(number) - raise NotImplementedError + if number == nil + raise ArgumentError, "Not a valid number" + elsif number == 0 || number == 1 + return 1 + else + x = number - 1 + x.times do + number = number * x + x -= 1 + end + return number + end end + + + + + + + + + + + + + + + + + + + + + + +# x = number +# while x > 1 do +# x -= 1 +# number = number *= x +# end + +# return number \ No newline at end of file From 8a567f1ba23986f61f2cd4dfd554870685aed6d6 Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Mon, 8 Apr 2019 21:17:43 -0700 Subject: [PATCH 2/3] removed commented out code from earlier attempt --- lib/factorial.rb | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 19a7c2e..36b9d06 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -15,33 +15,4 @@ def factorial(number) end return number end -end - - - - - - - - - - - - - - - - - - - - - - -# x = number -# while x > 1 do -# x -= 1 -# number = number *= x -# end - -# return number \ No newline at end of file +end \ No newline at end of file From 577de3a9224357ec1ed5b5a9c7cdd17e8686be2c Mon Sep 17 00:00:00 2001 From: Margaret Finnan Date: Tue, 9 Apr 2019 20:14:59 -0700 Subject: [PATCH 3/3] worked out new method --- lib/factorial.rb | 66 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 15 deletions(-) diff --git a/lib/factorial.rb b/lib/factorial.rb index 36b9d06..9baf1f8 100644 --- a/lib/factorial.rb +++ b/lib/factorial.rb @@ -1,18 +1,54 @@ # Computes factorial of the input number and returns it -# Time complexity: O(n), where n is the number passed into the method. -# Space complexity: Constant, O(1), the only extra space used is for the variable x +# Time complexity: Linear or O(n) where n is the number input +# Space complexity: Constant or O(1), will have the same number of variables no matter the size of the input def factorial(number) - if number == nil - raise ArgumentError, "Not a valid number" - elsif number == 0 || number == 1 - return 1 - else - x = number - 1 - x.times do - number = number * x - x -= 1 - end - return number - end -end \ No newline at end of file +return 1 if number == 0 +if number == nil + raise ArgumentError, "Enter an integer" +end +a = 1 +b = number +while a < number + b = b * (number - a) + a += 1 +end +return b +end + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +