Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sockets - Hana #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 21 additions & 4 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@
# ....
# e.g. 6th fibonacci number is 8

# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n), where n is the the number of values in the array
# Space complexity: O(c), where c is the integers that are being rewritten

Choose a reason for hiding this comment

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

The space complexity is actually O(1) since it does not increase with the size of n.


def fibonacci(n)
raise NotImplementedError
end
if n == nil || n < 0
raise ArgumentError, "Invalid input for n, #{n}"
elsif n == 0 || n == 1
return n
else
fib_num_prev = 0 # 2
fib_num_curr = 1 # 3
fib_num_next = 0 # 3

(n-1).times do
fib_num_next = fib_num_prev + fib_num_curr
fib_num_prev = fib_num_curr
fib_num_curr = fib_num_next
end

return fib_num_next
end
end