Write a Python function that calculates the Fibonacci sequence recursively and iteratively
- User
Write a Python function that calculates the Fibonacci sequence recursively and iteratively
- Assistant
Certainly! Below are two Python functions that calculate the Fibonacci sequence: one using recursion and the other using iteration.
Recursive Fibonacci Function
def fibonacci_recursive(n): if n <= 0: return 0 elif n == 1: return 1 else: return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2) # Example usage: n = 10 print(f"Fibonacci (recursive) of {n}: {fibonacci_recursive(n)}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.