Photo by Luca Bravo on Unsplash

Fibonacci series

Dibri Nsofor
2 min readNov 12, 2020

No this does not have anything to do with pasta but a lot to do with integer sequence. The Fibonacci series or sequence, is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21…. where each subsequent number is found by adding the two numbers before it. So in our case, the next number of the series would be 34.

The Fibonacci sequence was invented by the Italian, Leonardo Pisano Bigollo (1180–1250), who is known in mathematical history by several names: Leonardo of Pisa (Pisano means “from Pisa”) and Fibonacci (which means “son of Bonacci”).

I’ll show you how to find the value of the nth term of the series in python and try to explain each step as well as possible.

In mathematical terms, the sequence “Fn” (Fibonacci numbers) is defined by the recurrence relation

Fn = Fn-1 + Fn-2

with seed values

F0 = 0 and F1 = 1

To do this, I’ll assume you already have your python basics covered or else I would suggest learning about functions, if statements, and just recursion in general.

We would typically start off by creating a function, say, Fibonacci, to collect an input of n (for nth terms)

def Fibonacci(n):

next step: introduce condition statements for possible values of n, for when n = 0, n = 1 and n = other numbers. It should look something like this:

#For when n is less than or equal to zero the code should print “incorrect input” as n should not be anything less than zero

if n<=0:print(“Incorrect input”)#for when n is equal to 1elif n==1:return 0#for when n is equal to 2elif n==2:return 1#every other caseelse:return Fibonacci(n-1)+Fibonacci(n-2)

The next and final step should be to print the result.

print(Fibonacci(10))

which should return a result of 34.

There you have it, a script that finds the value of the nth term of the Fibonacci series. Feel free to copy the code and have it print other nth terms. Granted, this solution may not be space and time complexity conscious but it’s easy to follow.

--

--