Fibonacchi Series

Author: Iqbal Hossain

Problem

Let,
f(0) = 0;
f(1) = 1;
f(n) = f(n-1) + f(n-2)

How to implement it?

Solution

As I prefer python for code development. Problem function could be sloved in iterative or recurssion way. I have mention below only iterative way becouase of it is efficient.
Problem statement is produce fibonacchi series.
def f(n):
    n0 = 0
    n1 = 1

    if n == 0:
        return 0
    elif n == 1:
        return 1
    elif n > 1:
        for i in range(2, n+1):
            # (n-1) + (n-2)
            ni = n0 + n1
            
            # forward cursor and update value
            n0 = n1
            n1 = ni
            
        return "f(%s) = %s" %(n, ni)
    else:
        return -1
        print(f(6))
Output:
f(6) = 8