N-Queens Problem - Python Recurrsion
N-Queens Problem - Python Recurrsion
Imagine you're standing in front of a mirror holding another mirror. What do you see?
An infinite number of reflections, right? Each reflection is a smaller version of the
one before it. This is very similar to how recursion works—solving a problem by
breaking it into smaller versions of itself.
factorial(3) = 3 * 2 * 1 = 6
factorial(5) = 5 * 4 * 3 * 2 * 1 = 120
Let's write a Python function to calculate the factorial of a number using recursion.
def factorial(n):
# Base case: if n is 1 or 0, return 1
if n == 1 or n == 0:
return 1
else:
# Recursive case: n * factorial of (n - 1)
return n * factorial(n - 1)