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
18 changes: 15 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -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)

Choose a reason for hiding this comment

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

👍


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
Comment on lines +16 to +18

Choose a reason for hiding this comment

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

Yes!

return fib_helper(solutions, current + 1, n)
end
20 changes: 12 additions & 8 deletions lib/super_digit.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Superdigit

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(n^2)
# Space Complexity - O(n)
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
super_digit(n.digits.sum)
end


# Time Complexity - ?
# Space Complexity - ?

def refined_super_digit(n, k)

Choose a reason for hiding this comment

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

👍


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