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) because number of iterations is dependent on the size of the input number
# Space complexity: O(1) because only one int value (factorial) is ever stored
def factorial(number)
raise NotImplementedError
if number
factorial = 1
while number > 0
factorial *= number
number -= 1
end
return factorial
else
raise ArgumentError, "Input must be an integer"
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'd recommend having error handling at the top rather than the bottom so you break early. Something such as:

if !number 
  raise
else 
  do factorial
end

Otherwise good job on this assignment!

end
end