nathanwailes

Fibonacci - Iterative

Jun 18th, 2024 (edited)
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.33 KB | None | 0 0
  1. """
  2. The Fibonacci sequence is a series of numbers where each number is the sum of the two numbers before it.
  3. """
  4.  
  5. def fibonacci(n):
  6.     if n <= 0:
  7.         return 0
  8.     elif n == 1:
  9.         return 1
  10.  
  11.     a, b = 0, 1
  12.     for _ in range(2, n+1):
  13.         a, b = b, a + b
  14.     return b
  15.  
  16. print(fibonacci(20))  # output should be 6765
Advertisement
Add Comment
Please, Sign In to add comment