diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..0ca1e7b 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,30 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - O(n) +# Space Complexity - O(n) +# you only keep the last two solutions, so the solutions array does not grow, +# but you're still effectiving the stack by adding recursive calls # Hint, you may want a recursive helper method def fibonacci(n) - + if n < 0 + raise ArgumentError.new('Number cannot be less than zero') + end + + return fib_helper([0, 1], 2, n) +end + +def fib_helper(solutions, current, n) + if n == 0 || n == 1 + return n + end + + last_solution = solutions[-1] + new_solution = solutions[0] + solutions[1] + + if current == n + return new_solution + end + + solutions = [last_solution, new_solution] + return fib_helper(solutions, current + 1, n) end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..d2cdfc0 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,15 +1,31 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n) where n is the number of digits in the input number +# Space Complexity - O(log n) def super_digit(n) - + if (n / 10) == 0 + return n + else + sum = 0 + while ((n / 10.0) > 0) + sum += (n % 10) + n = (n / 10) + end + return super_digit(sum) + end end - -# Time Complexity - ? -# Space Complexity - ? +# Time Complexity - O(n) +# because the while loop to create the concatenated number +# and the while loop adding the digits of the number are not nested. +# Space Complexity - O(log k * n) def refined_super_digit(n, k) - + concatenated_n = '' + while k > 0 + concatenated_n += n.to_s + k -= 1 + end + + concatenated_n = concatenated_n.to_i + return super_digit(concatenated_n) end - \ No newline at end of file diff --git a/test/fibonacci_test.rb b/test/fibonacci_test.rb index 639f3b1..64282e5 100644 --- a/test/fibonacci_test.rb +++ b/test/fibonacci_test.rb @@ -41,10 +41,10 @@ end it "will return 5 for fib(5)" do # Act - answer = fibonacci(4) + answer = fibonacci(5) # Assert - expect(answer).must_equal 3 + expect(answer).must_equal 5 end it "will return 55 for fib(10)" do # Act diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 60da3a1..ce03b56 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -1,61 +1,69 @@ require_relative "test_helper" -xdescribe "super_digit" do +describe "super_digit" do it "will return 2 for super_digit(9875)" do # Act answer = super_digit(9875) - + # Assert expect(answer).must_equal 2 end - + it "will return 5 for super_digit(5)" do # Act answer = super_digit(5) - + # Assert expect(answer).must_equal 5 - end - + end + it "will return 6 for super_digit(123)" do # Act answer = super_digit(123) - + # Assert expect(answer).must_equal 6 end - + it "will return 6 for super_digit(12327)" do # Act answer = super_digit(12327) - + # Assert expect(answer).must_equal 6 end - + + it "will return 9 for super_digit(999_999_999_999)" do + # Act + answer = super_digit(999_999_999_999) + + # Assert + expect(answer).must_equal 9 + end + describe "refined superdigit" do it "will return 1 for n = 1 and k = 1" do # Act answer = refined_super_digit(1, 1) - + # Assert expect(answer).must_equal 1 end - + it "will return 8 for n=9875 and k = 4" do # Act answer = refined_super_digit(9875, 4) - + # Assert expect(answer).must_equal 8 end - + it "will return 3 for n=148 and k = 3" do - # Act - answer = refined_super_digit(148, 3) - - # Assert - expect(answer).must_equal 3 + # Act + answer = refined_super_digit(148, 3) + + # Assert + expect(answer).must_equal 3 end end end