gpt-3.5-turbo-0314· OpenAI
Arena Human Preference 55K
Write a simple Python function to calculate the factorial of a given positive integer.
- User
Write a simple Python function to calculate the factorial of a given positive integer.
- Assistant
Sure, here's a Python function that calculates the factorial of a given positive integer using recursion:
def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)Here's an example of how you can use this function to calculate the factorial of 5:
print(factorial(5)) # Output: 120This function works by recursively calling itself with n-1 until n equals 0. At that point, it returns 1, which is the base case of the recursion. The final result is the product of all the numbers from n down to 1.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.