factorial.sf 395 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Factorial
  4. #
  5. # Recursive
  6. func factorial_recursive(n) {
  7. n == 0 ? 1 : (n * __FUNC__(n-1));
  8. };
  9.  
  10. # Iterative with Array#reduce
  11. func factorial_reduce(n) {
  12. 1..n -> reduce('*');
  13. };
  14.  
  15. # Iterative with Block#repeat
  16. func factorial_iterative(n) {
  17. var f = 1;
  18. {|i| f *= i } * n;
  19. return f;
  20. };
  21.  
  22. # Built-in Number#factorial:
  23. say 5!;