Guest User

Untitled

a guest
Feb 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. # Factorial Finder
  2. # The Factorial of a positive integer, n, is defined as the product of the
  3. # sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as
  4. # being 1. Solve this using both loops and recursion.
  5.  
  6. def factorial(n):
  7. result = 1
  8.  
  9. if n == 0:
  10. return result
  11.  
  12. while n > 0:
  13. result *= n
  14. n -= 1
  15.  
  16. return result
  17.  
  18.  
  19. def factorial_rec(n):
  20. if n == 0:
  21. return 1
  22. else:
  23. return n * factorial_rec(n-1)
  24.  
  25.  
  26. def main():
  27. n = 5
  28. result = factorial_rec(n)
  29. print result
  30. result = factorial(n)
  31. print result
  32.  
  33.  
  34. if __name__ == "__main__":
  35. main()
Add Comment
Please, Sign In to add comment