def fibonacci_iterative(n): if n < 0: raise ValueError("Input must be a non-negative integer.") # Base cases for the first two Fibonacci numbers fib_1 = 0 fib_2 = 1 # Handle small inputs directly to avoid unnecessary calculations if n in [0, 1]: return n else: i = 3 # starting index of a new sequence (step) while i <= n: # Sum the last two numbers and set it as current number fib_next = fib_1 + fib_2 # Move forward by one step in Fibonacci's sequence i += 1 # Update "old" values to reflect this new value fib_1, fib_2 = fib_2, fib_next return fib_2 # Test the function with a non-trivial example print(fibonacci_iterative(9))