Skip to content
AI Platform / Playground

Welcome to the Playground

Test AI models with your prompts. Try asking about code, writing, analysis, or anything else.

U

You

Write a Python function that calculates the Fibonacci sequence up to n numbers.

AI Platform

Here's a Python function that generates the Fibonacci sequence:

def fibonacci(n):
    """
    Generate Fibonacci sequence up to n numbers.

    Args:
        n: Number of Fibonacci numbers to generate

    Returns:
        List of Fibonacci numbers
    """
    if n <= 0:
        return []
    elif n == 1:
        return [0]

    fib = [0, 1]
    for i in range(2, n):
        fib.append(fib[i-1] + fib[i-2])
    return fib

# Example usage
print(fibonacci(10))
# Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

This function:

  • Handles edge cases for n <= 0 and n = 1
  • Uses iterative approach for O(n) time complexity
  • Returns a list containing the Fibonacci sequence
AIP-4-Turbo | Temperature: 0.7 | Max tokens: 1024