Generate Fibonacci Series — The Pythonic way

5 ways to generate Fibonacci series in python

Dinesh Kumar K B
Nerd For Tech

--

Photo by Christian Lambert on Unsplash

Introduction:

The most common mistakes we make in interviews are the most silly ones. Do you agree? This happens because we ignore the ones which we are quite familiar. During interviews, we prepare the most difficult data structure and algo questions and seldom give importance to fibonacci or prime number programs. However, sometimes we get stuck during interviews in these petty programs. This article is a refresher for fibonacci series generation using python.

🆗 Now, let’s cut to the chase!

Fibonacci Series:

A Fibonacci series or number is the sum of preceding 2 numbers starting from 0 and 1. For detailed information refer the wiki link here 🔗.

A typical Fibonacci series looks like this

0 1 1 2 3 5 8 13.....

5 ways to generate Fibonacci series in python

Method 1️⃣ — The traditional way

Output:

1 2 3 5 8 13 21 34 55

Well, to be honest this is not very pythonic except the tuple unpacking in line no 17. We just use 3 variables a,b and c , leverage the tuple unpacking to swap values.

Method 2️⃣ — The recursive way ️⃣

Output:

55

This uses recursion to calculate the fibonacci number.This isn’t a series generation. It just returns the nth fibonacci number.

Time Complexity ⌛️ : O(2^n)

Space Complexity: O(n)

Method 3️⃣ — Using generators️

I bet most of you would have been asked to write a Fibonacci series using generators in interviews. If not, well this could come in handy when you are asked. Or if you are asked to write a sample program in generators, you could use this.

This is how we write fibonacci series using generators. For more information on generators please refer here. Since generators are stateful, you may pause and resume the program at any given point of time. What does this mean? This means you can get the fibonacci number of any value ≤ n by stopping and resuming the iteration at your disposal.

Since myfib is a generator object you may use a for loop to iterate over the elements.

for i in myfib:
print(i)
Output:
1
2
3
5
8
13
21
34
55
89

Or you could call next(myfib) to get the next element from the generator object.

print(next(myfib))
print(next(myfib))
print(next(myfib))
print(next(myfib))
...
Output:
1
2
3
5
...

You could implement fibonacci using generators if you want to preserve the state of your program.

Method 4️⃣ — Using lists️⃣

This leverages the capability of lists to traverse backwards. This is my personal favorite ❤️ way of generating fibonacci series.

Do you wanna know a secret?

If you are a pythonista, I would bet you can never make mistakes in this during interviews. Also, this is a less commonly used method. Try using this method if you are asked to write fibonacci in your interviews. Of course, if you are not allowed to use a list, go with other methods.

Output:

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

At any given point of time, a fibonacci number is the sum of preceding 2 numbers. So adding 0 and 1 to a list and then consecutively calculating the sum of last 2 elements and appending them to the list gives us the fibonacci number.

Time Complexity ⏳: O(n)

Space Complexity: O(1)

Method 5️⃣ — Using Iterator:

This is a bonus method and an extended version of the generator method. We will be using Iterator to write a fibonacci series.

Write this if you are being asked to write a simple iterator program 🆒

Output:

1
2
3
5
8
13
21
34
55
89

Summary:

I reckon almost all programmers have come across Fibonacci series. I bet we have used it at least once for every new programming language we learn. We would love to try out the programs known to us in a new programming language. Don’t we? Of course we would.

I personally believe, trying out known programs in a new programming language we learn boosts our confidence and helps for better understanding of the language.

If you are python newbie, do try these as and when you learn iterators, generators and lists.

--

--