Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- The simplest possible example of recursion in Python is the calculation of the factorial of a number.
- This simple example demonstrates the basic concept of recursion: a function calling itself to solve a smaller instance of the same problem, with a base case to terminate the recursion.
- """
- def factorial(n):
- # Base case: if n is 0, return 1
- if n == 0:
- return 1
- # Recursive case: n * factorial of (n-1)
- else:
- return n * factorial(n-1)
- # Example usage
- print(factorial(5)) # Output: 120
Advertisement
Add Comment
Please, Sign In to add comment