Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0314· OpenAIHugging Face Arena Human Preference 55K

Write a Python program to find the nth Fibonacci number using dynamic programming.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with vicuna-13b’s answer.
  1. User

    Write a Python program to find the nth Fibonacci number using dynamic programming.

  2. 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) where n is the index of the Fibonacci number you want to find. For example, fibonacci(5) will return 5, since the 6th Fibonacci number is 5.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.