Back

Embarrassing First Post

Fri 28 February 2014

Posted by: Charlie Mills

Python Useless

This post serves no purpose other than to fill the inevitable void. Last week at an interview I was asked to write a Fibonacci function, it seems regurgitation of Computer Science theory makes you employable, so here they are in Python.

Recursive

def f(n):
    if n == 0:
        return 0
    if n == 1:
        return 1
    else:
        return f(n-1) + f(n-2)

Iterative

def f(n):
    a = 0
    b = 1
    for i in range(0, n):
        a, b = b, a + b
    return a

Please revel in the usefulness of these numbers (excuse the bitterness).