From 200908ebceb4b928e04471a9dfa8fe9102587785 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 18 Nov 2019 18:56:49 -0800 Subject: [PATCH 1/2] completed --- lib/fibonacci.rb | 7 ++++++- lib/super_digit.rb | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..57340d5 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -4,5 +4,10 @@ # Space Complexity - ? (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) - + # base case: n = 0 => 0, n = 1 => 1 + return raise ArgumentError if n < 0 + return n if n == 0 || n == 1 + + # recursive case + return fibonacci(n - 2) + fibonacci(n - 1) # return fib(6) + fib(7) => 8 + 13 => 21 end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..0b10dbc 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -3,7 +3,8 @@ # Time Complexity - ? # Space Complexity - ? def super_digit(n) - + return n if n < 10 + return super_digit(n.digits.sum) end From 41c8795917b7a635f7ed8265494539fdeec9d9e8 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Tue, 19 Nov 2019 10:10:54 -0800 Subject: [PATCH 2/2] edited --- lib/fibonacci.rb | 4 ++-- lib/super_digit.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 57340d5..4f717cb 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,7 +1,7 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - o(n) +# Space Complexity - o(n) (should be O(n)) # Hint, you may want a recursive helper method def fibonacci(n) # base case: n = 0 => 0, n = 1 => 1 diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 0b10dbc..658c462 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,7 +1,7 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - o(n) +# Space Complexity - o(n^2) def super_digit(n) return n if n < 10 return super_digit(n.digits.sum)