...

/

Solution Review: Fibonacci Series

Solution Review: Fibonacci Series

Review the solution for the "Fibonacci Series" problem.

We'll cover the following...

Solution

Let’s explore the solution to the problem of the Fibonacci series.

Press + to interact
def fib(n):
# The first and second values will always be fixed
first = 0
second = 1
if n < 1:
return -1
if n == 1:
return first
if n == 2:
return second
count = 3 # Starting from 3 because we already know the first two values
while count <= n:
fib_n = first + second
first = second
second = fib_n
count += 1 # Increment count in each iteration
return fib_n
n = 7
print(fib(n))

Explanation

Here’s a line-by-line explanation of the code for the Fibonacci Series problem:

  • Line 1: Defines the function fib that takes one argument n, which is the position in the Fibonacci sequence. ...