-
Notifications
You must be signed in to change notification settings - Fork 43
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 - Ngoc #21
base: master
Are you sure you want to change the base?
Ports - Ngoc #21
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,23 @@ | |
# .... | ||
# e.g. 6th fibonacci number is 8 | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n), n is the number of computations this calculation needs to run to compute the nth fibonacci number | ||
# Space complexity: O(1), space complexity is constant because this method only require a storage for 3 variables (p,p1,p2) regardless of number of operations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. |
||
def fibonacci(n) | ||
raise NotImplementedError | ||
if n.nil? || n < 0 | ||
raise ArgumentError | ||
end | ||
if n == 0 | ||
return 0 | ||
else | ||
p1 = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have a good point. Do you think naming it p3 would make it easy to understand, since it is the number subsequent to p2 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually curious why you were using |
||
p2 = 1 | ||
|
||
(n - 1).times do | ||
p = p1 + p2 | ||
p1 = p2 | ||
p2 = p | ||
end | ||
return p2 | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the number of computations this calculation needs to run" is sort of what you're trying to define by explaining the "time complexity" of a method, so you don't want to use that in the definition. "n" as you're using it here is actually the value of the input variable
n
. The time complexity isn
because you're iteratively calculating each fibonacci number up ton
.