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

Ports - Sopheary #34

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
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: O(n) where n is the number of the input. As n increases, the while loop operations will increase too.
# Space complexity: O(1). Thought there is an array to store previous_num and this num, the arrays doesn't increase when n increases.
def fibonacci(n)
raise NotImplementedError
raise ArgumentError if n.class != Integer
raise ArgumentError if n < 0
return 0 if n == 0
return 1 if n == 1
this_num = 1
previous_num = 0
index = 0

while index < n - 1
array = []
array << previous_num
array << this_num
sum = array[0] + array[1]
previous_num = this_num
this_num = sum
index += 1
end
return sum
end