Photo by Luca Bravo on Unsplash

Fibonacci series

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

with seed values

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)

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

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

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store