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
11 changes: 8 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the brute force O(2n) solution. Check the notes and see if you can approach this a better way.


# 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
7 changes: 4 additions & 3 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - o(n)
# Space Complexity - o(n^2)
def super_digit(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return n if n < 10
return super_digit(n.digits.sum)
end


Expand Down