[Solved] The Fibonacci formula does not correctly output the Nth value


As mentioned in the comment above your program do its job and perfectly described the Fibonacci sequence. Here I implement the recursive version and then compare the result with your implementation (explicit form).

import math

def staircase(n):

    term_a = ((1 + math.sqrt(5)) / 2) ** n
    term_b = ((1 - math.sqrt(5)) / 2) ** n

    numerator = term_a - term_b
    denominator = math.sqrt(5)

    return int(numerator / denominator)


def fibo_rec(n):
    # initial conditions
    if n == 0: return 0
    if n == 1: return 1
    # recursive relationship
    return fibo_rec(n-1) + fibo_rec(n-2)

n = 20
test = [fibo_rec(i) for i in range(n)] == [staircase(i) for i in range(n)]
print(test)

Output

True

1

solved The Fibonacci formula does not correctly output the Nth value