gpt-3.5-turbo-0314· OpenAI
Arena Human Preference 55K
Write a Python program to find the nth Fibonacci number using dynamic programming.
- User
Write a Python program to find the nth Fibonacci number using dynamic programming.
- Assistant
Certainly! Here's the code:
def fibonacci(n): # Create a list to store the Fibonacci numbers. fib = [0, 1] # If n is less than or equal to 1, return the nth Fibonacci number. if n <= 1: return fib[n] # Compute and store the Fibonacci numbers up to the nth number. for i in range(2, n+1): fib.append(fib[i-1] + fib[i-2]) # Return the nth Fibonacci number. return fib[n]To use this function, simply call
fibonacci(n)wherenis the index of the Fibonacci number you want to find. For example,fibonacci(5)will return5, since the 6th Fibonacci number is 5.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.