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
15 changes: 12 additions & 3 deletions lib/factorial.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Computes factorial of the input number and returns it
# Time complexity: ?
# Space complexity: ?
# Time complexity: O(n)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When you provide time complexity, please explain in a few words what n means. For example, in this case, is n equal to the value of number? Or is it equal to the number of things stored in number (which would be 1)? Or might it have anything to do with the number of decimal digits needed to represent number? It's important to define n for every new problem, since the meaning can change from algorithm to algorithm.

# Space complexity: O(1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also briefly explain why here.

def factorial(number)
raise NotImplementedError
if number == nil
raise ArgumentError
end

i = 1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm used to seeing i used as a variable name only when the variable is used to iterate over a collection. This variable is actually being used to accumulate the product of numbers for the factorial calculation. I'd name this something more like product in order to avoid confusion.

while number > 0
i = number * i
number -= 1
end
return i
end