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

Jane's fibonacci #25

Open
wants to merge 1 commit 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
20 changes: 19 additions & 1 deletion lib/fibonacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
# e.g. 1st fibonacci number is 1
# ....
# e.g. 6th fibonacci number is 8
# fibonacci(3).must_equal 2

# Time Complexity: O(n)

Choose a reason for hiding this comment

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

I'd recommend practicing elaborating the reasoning for your time and space complexity.

# Space Complexity: O(1)

def fibonacci(n)
raise NotImplementedError
one = 0
two = 1
sum = 0
i = 0
raise ArgumentError if n.nil?
raise ArgumentError if n < 0
return n if n == 1
while (n - 1) > i
sum = one + two
one = two
two = sum
i += 1
end
return sum
end