Advertisement
pacho_the_python

factorials

Apr 22nd, 2024
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. def factorial_recursive(n):
  2.     """
  3.    Computes the factorial of a non-negative number n (using recursion)
  4.    """
  5.     if n == 0:
  6.         return 1
  7.     return n * factorial_recursive(n - 1)
  8.  
  9. def factorial_iterative(n):
  10.     """
  11.    Computes the factorial of a non-negative number n (using iteration)
  12.    """
  13.     result = 1
  14.     if n == 0:
  15.         return 1
  16.     for num in range(1, n + 1):
  17.         result *= num
  18.     return result
  19.  
  20.  
  21. factorial_recursive(15)
  22. factorial_iterative(20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement