educative.io

Educative

Error in Solution Fibonaaci Series using Iteration

The condition mentioned in the explanation is: If n is negative or zero, return -1 .
But the solution ignores this condition by assigning 0 to n=1

first = 0

second = 1

if n < 1:

    return -1

if n == 1:

    return first

This causes the Fib(7) to be calculated as 8 instead of 13

Hi Nandan,

The Fibonacci series starts with 0. It is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…
Hence:

  1. Fib(1) equals to 0.
  2. Fib(7) is equal to 8 and not 13.

The solution doesn’t ignore this condition. “If n is negative or zero, return -1” condition is checked on lines 6-7 of the solution:

if n < 1:
    return -1

In this case, n < 1 and n <= 0 work exactly the same as we are using integers.

I hope this clears the confusion.