Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@

# 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)


return 1 if n == 1 || n == 2
return 0 if n == 0
raise ArgumentError if n < 0

fibonacci(n - 1) + fibonacci(n - 2)

end

20 changes: 14 additions & 6 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(n)
# 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)


n = n.digits.sum * k
super_digit(n)

end



p refined_super_digit(3212, 4)
2 changes: 1 addition & 1 deletion test/fibonacci_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

describe "Fibonacci" do
xdescribe "Fibonacci" do
it "will raise an error for n < 0" do
expect {
# Act
Expand Down
4 changes: 2 additions & 2 deletions test/super_digit_test.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down