Solve fibinoci theory in python in o(n) complexity
- User
Solve fibinoci theory in python in o(n) complexity
Assistant
Here's a Python solution for calculating Fibonacci numbers with O(n) time complexity:
pythondef fibonacci(n): if n <= 0: return 0 if n == 1: return 1 a, b = 0, 1 for _ in range(2, n + 1): a, b = b, a + b return b # Example usage n = 10 print(f"The {n}th Fibonacci number is {fibonacci(n)}")Would you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.