-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
023a87f
commit f9c3405
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
begin | ||
if ARGV.empty? | ||
raise "Usage: please input a non-negative integer" | ||
end | ||
|
||
input = ARGV[0].to_i? | ||
|
||
if input.nil? | ||
raise "Usage: please input a non-negative integer" | ||
end | ||
|
||
input = input.not_nil! | ||
|
||
if input >= 0 && input <= 12 | ||
puts factorial(input) | ||
elsif input > 12 | ||
raise "#{input} is out of the reasonable bounds for calculation." | ||
else | ||
raise "Usage: please input a non-negative integer" | ||
end | ||
|
||
rescue e | ||
puts e.message | ||
end | ||
|
||
def factorial(n) | ||
return 1 if n == 0 | ||
n * factorial(n - 1) | ||
end |