Write factorial function in python
- User
Write factorial function in python
Assistant
Here is a factorial function in Python:
pythondef factorial(n): """Calculates the factorial of n.""" if n == 1: return 1 else: return n * factorial(n-1)This is a recursive solution. A few examples:
pythonfactorial(1) # 1 factorial(2) # 2 factorial(3) # 6 (3 * 2 * 1) factorial(4) # 24 (4 * 3 * 2 * 1)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.