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
20 changes: 17 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
# Improved Fibonacci

# Time Complexity - ?
# Space Complexity - ? (should be O(n))
# Time Complexity - O(n)
# Space Complexity - O(n) stack space
# 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(results, current, n)
raise ArgumentError if n < 0
return results[n] if n == 0 || n == 1

if current == n
return results[0] + results[1]
end

temp = results[0] + results[1]
results[0] = results[1]
results[1] = temp
Comment on lines +18 to +20

Choose a reason for hiding this comment

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

Yes!

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

# Time Complexity - ?
# Space Complexity - ?
# Time Complexity - O(logn) n is the number itself
# Space Complexity - O(logn)
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 == 0

sum = n % 10
while n/10 != 0
n = n/10
sum += n % 10
end
return super_digit(sum)
end


# Time Complexity - ?
# Space Complexity - ?

# Time Complexity - O(logn) n is the number itself
# Space Complexity - O(logn)
def refined_super_digit(n, k)

return n if k == 1 && n/10 == 0
sum = 0
sum += super_digit(n) * k
return super_digit(sum)
end

2 changes: 1 addition & 1 deletion 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