Skip to content
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
23 changes: 20 additions & 3 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: Linear or O(n), where n is the input
# Space complexity: Constant or O(1), because additional space doesn't rely on the input
def fibonacci(n)
raise NotImplementedError
raise ArgumentError if n == nil || n < 0

return 0 if n == 0
return 1 if n == 1

fibonacci = 0

first_number = 0
second_number = 1

(n - 1).times do
fibonacci = first_number + second_number

first_number = second_number
second_number = fibonacci
end

return fibonacci
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Your variable names are very clear. Good work on this solution!

end