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 - Kasey #28

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
16 changes: 13 additions & 3 deletions lib/fibonacci.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
# ....
# e.g. 6th fibonacci number is 8

# Time complexity: ?
# Space complexity: ?
# Time complexity: 0(n), where the program only loops through it n times (well, n - 2 times because the first few things are hardcoded cases), was wondering if it was better to do it with recursion? but that would run it with recursion instead of saving everything in an array and indexing it, but I think that would run more times?
Copy link

Choose a reason for hiding this comment

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

I kind of get what n is here, but loops through it n times is a bit of a tautology.

Can you explain it more clearly perhaps? e.g. O(n), where n = the number of digits in the input number or O(n), where n = the input number itself.

# Space complexity: constant, although you have to initialize space for an array
Copy link

Choose a reason for hiding this comment

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

⚠️Are you sure? That array looks like it's linear to me.

I reckon you can get to constant space though - any ideas on how you'd do it?


def fibonacci(n)
raise NotImplementedError
raise ArgumentError if n == nil || n < 0
return 0 if n == 0
return 1 if n == 1
arr = [0, 1]
i = 1
until i == n
arr[i + 1] = arr[i] + arr[i - 1]
i += 1
end
return arr[i]
end