Buckets:
| 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)) |
Xet Storage Details
- Size:
- 839 Bytes
- Xet hash:
- a0633c396b2438e6a09bfc4333868c04a04fa3fa49381e6424bbb85b890d16dd
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.