Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def fact x
- if x == 0
- 1
- else
- x * fact(x-1)
- end
- end
- >> fact 10
- => 3628800
- >> fact 7
- => 5040
- >> fact -1
- SystemStackError: stack level too deep
- from (irb):2:in `fact'
- from (irb):5:in `fact'
- from (irb):10
- def fact x
- if x < 0
- raise "Can't take the factorial of a negative number"
- elsif x == 0
- 1
- else
- x * fact(x-1)
- end
- end
- >> fact -1
- RuntimeError: Can't take the factorial of a negative number
- from (irb):3:in `fact'
- from (irb):10
- >> fact 10
- => 3628800
- >> fact 7
- => 5040
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement