From Fibonacci to N-bonacci
By: Kelvin Tan
Tags: Recursion, Python, Algorithms, Software Engineering, Fibonacci

The Fibonacci sequence gives rise to the golden ratio found in many beauties of nature. This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.

The Fibonacci sequence was one of the first mathematical concepts that I analysed when I started coding. In particular, this sequence was vital in enabling me to grasp recursion.

To construct a positive Fibonacci sequence, we start by initialising the first two terms: 0, 1.

The next term is derived from the sum of the prior two terms: 0 + 1 = 2. And so on.

In Python, we can automate the process via recursion. So if we want to determine the third fibonacci number: in the function fibonacci, we first check if the sequence already has the value by supplying k as the key into a sequence defined as a dictionary.

If k is in the sequence, then we simply return the value paired with k as the answer. However, as we have just initialised the sequence, k = 3 would not be found. So we go ahead to calculate the kth value of the sequence by recursively calling the fibonacci function twice to calculate the two terms before.

The recursion stack keeps piling on with half-completed function calls until the base case is satisfied — that is the sequence has already stored or cached the value in the fibonacci_sequence dictionary.

We could have returned the answer immediately, but we decide to add one more line to add our result into the sequence so that the machine would not need to waste time recalculating a value that we have seen before.

Now, what if we want to generalise the Fibonacci sequence into an N-bonacci sequence, where we initialise the first N-1 values as 0, while the N-th value is 1.

If you are interested in a related coding challenge, today’s post is inspired by this Edabit problem, so you could try to solve it before continuing on. To be sure, copying and pasting the code below would not work as-is as the answer that I am seeking is slightly different.

The approach I have taken is actually a generalised form of the solution, where the problem is solved via object-oriented programming rather than functional programming. OOP has the advantage of enabling us to first create and object representing some N-bonacci — e.g. Fibonacci is also a 2-bonacci — before adding a kth_term method that could be reused to find different kth values.

Let us create an object called Bonacci such that a Fibonacci sequence can be represented by Bonacci(2). In the __init__ magic method, we initialise the sequence self.ans to store the initial sequence values.

We also write a __repr__ magic method to easily identify the object upon instantiation, which is almost always more useful than printing some memory address of the object.

The kth-term method is very similar in process to the main code in the fibonacci function, except that we use comprehension to sum up the N prior terms when an unseen kth-term is found. This method also relies on calling recursion stacks to calculate all required N-bonacci values in reverse upon reaching the base case.

A few more examples on how we could use the object:

The Fibonacci sequence and the resulting golden ratio is found throughout nature — an example would be the seashell in the image above. With the computing power to generalise such sequences, I wonder what else could be discovered in time?

Enjoyed this article? If so, get more similar content by subscribing to Decoded, our YouTube channel!