Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

how to calculate fibonacci

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-sonnet-4-20250514’s answer.
  1. User

    how to calculate fibonacci

  2. Assistant

    The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence looks like this:

    0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

    How to calculate Fibonacci numbers

    1. Using a simple formula (recursive definition):

    [ F(0) = 0, \quad F(1) = 1 ] [ F(n) = F(n-1) + F(n-2) \quad \text{for } n \geq 2 ]

    Example:

    • (F(2) = F(1) + F(0) = 1 + 0 = 1)
    • (F(3) = F(2) + F(1) = 1 + 1 = 2)
    • (F(4) = F(3) + F(2) = 2 + 1 = 3)

    2. Iterative method (more efficient):

    def fibonacci(n):
        a, b = 0, 1
        for _ in range(n):
            a, b = b, a + b
        return a
    

    This calculates the nth Fibonacci number without the overhead of recursion.

    3. Using Binet’s formula (closed form):

    [ F(n) = \frac{\phi^n - \psi^n}{\sqrt{5}} ]

    where
    (\phi = \frac{1 + \sqrt{5}}{2} \approx 1.618) (the golden ratio)
    (\psi = \frac{1 - \sqrt{5}}{2} \approx -0.618)

    This formula gives the nth Fibonacci number directly but can suffer from floating-point precision issues for large (n).


    If you want, I can provide code examples in any programming language or explain any method in more detail!

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