Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. def fact x
  2. if x == 0
  3. 1
  4. else
  5. x * fact(x-1)
  6. end
  7. end
  8.  
  9. >> fact 10
  10. => 3628800
  11. >> fact 7
  12. => 5040
  13.  
  14. >> fact -1
  15. SystemStackError: stack level too deep
  16. from (irb):2:in `fact'
  17. from (irb):5:in `fact'
  18. from (irb):10
  19.  
  20. def fact x
  21. if x < 0
  22. raise "Can't take the factorial of a negative number"
  23. elsif x == 0
  24. 1
  25. else
  26. x * fact(x-1)
  27. end
  28. end
  29.  
  30. >> fact -1
  31. RuntimeError: Can't take the factorial of a negative number
  32. from (irb):3:in `fact'
  33. from (irb):10
  34.  
  35. >> fact 10
  36. => 3628800
  37. >> fact 7
  38. => 5040
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement