diff --git a/lib/fibonacci.rb b/lib/fibonacci.rb index 7465c25..483a00d 100644 --- a/lib/fibonacci.rb +++ b/lib/fibonacci.rb @@ -1,8 +1,14 @@ # Improved Fibonacci -# Time Complexity - ? -# Space Complexity - ? (should be O(n)) +# Time Complexity - O(n!) +# Space Complexity O(n) # Hint, you may want a recursive helper method def fibonacci(n) - + if n < 0 + raise ArgumentError, "Invalid number" + elsif n == 0 || n == 1 + return n + else + return fibonacci(n - 2) + fibonacci(n - 1) + end end diff --git a/lib/super_digit.rb b/lib/super_digit.rb index 33e367f..3aa244c 100644 --- a/lib/super_digit.rb +++ b/lib/super_digit.rb @@ -1,10 +1,30 @@ # Superdigit -# Time Complexity - ? -# Space Complexity - ? -def super_digit(n) - +# Time Complexity - O(n) +# Space Complexity - O(n) +def super_digit(x) + if x < 10 + return x + elsif + sum = integer_calcuation(x) + super_digit(sum) + end end + +def integer_calcuation(x) + + digit_array = x.to_s.split('') + + sum = 0 + + digit_array.each do |element| + sum += element.to_i + end + + return sum +end + + # Time Complexity - ? diff --git a/test/super_digit_test.rb b/test/super_digit_test.rb index 60da3a1..20973f1 100644 --- a/test/super_digit_test.rb +++ b/test/super_digit_test.rb @@ -1,6 +1,6 @@ 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) @@ -33,7 +33,7 @@ expect(answer).must_equal 6 end - describe "refined superdigit" do + xdescribe "refined superdigit" do it "will return 1 for n = 1 and k = 1" do # Act answer = refined_super_digit(1, 1)