From 9a962b18194443a5d940aafaf775f71a16599bb9 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Sat, 16 Nov 2019 17:37:02 -0800 Subject: [PATCH 1/3] Finsihed fibonacci, all tests passing --- lib/fibonacci.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..fddd8b6 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,20 @@ # 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) - + return fib_helper([0, 1], 2, n) +end + +def fib_helper(solutions, current, n) + raise ArgumentError if n < 0 + return n if n == 0 || n == 1 + if current == n + return solutions[0] + solutions[1] + end + temp = solutions[0] + solutions[1] + solutions[0] = solutions[1] + solutions[1] = temp + return fib_helper(solutions, current + 1, n) end From 13cc692a34e9b11c5b95ab6b4c4f6fd05e45d4c3 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Sun, 17 Nov 2019 23:45:17 -0800 Subject: [PATCH 2/3] Finished super digit method --- lib/super_digit.rb | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..c5e3c69 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,15 +1,8 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n^2) +# Space Complexity - O(n) def super_digit(n) - + return n if n < 10 + super_digit(n.digits.sum) end - - -# Time Complexity - ? -# Space Complexity - ? -def refined_super_digit(n, k) - -end - \ No newline at end of file From 85f43ce16b80cca60fdaa0b0cb527e6445e8a7b0 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 18 Nov 2019 09:19:52 -0800 Subject: [PATCH 3/3] Added refined super digit method --- lib/super_digit.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/super_digit.rb b/lib/super_digit.rb index c5e3c69..0996767 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -6,3 +6,14 @@ def super_digit(n) return n if n < 10 super_digit(n.digits.sum) end + + +def refined_super_digit(n, k) + n = n.digits.sum * k + return refined_helper(n) +end + +def refined_helper(n) + return n if n < 10 + super_digit(n.digits.sum) +end \ No newline at end of file