I looked at quite some different approaches to the Fibonacci function, and I start to wonder how the Fibonacci number develops with respect to its index. To look into this I want to make a 2D plot where the X-axis is the natural numbers and the Y-axis is the corresponding Fibonacci numbers.

To solve this problem I decided to use R. This is less of an obvious solution. Usually one would probably have gone for a spreadsheet, but already did that.

R is a good tool for doing numeric analysis and prototyping algorithms for machine learning. For that it provides good tools for visualizing data. This is what I am going to elaborate a bit on here.

# Day 8 - R

Usually I would have started implementing Fibonacci as a recursive function. This was also my first approach and it is certainly possible. It is, however, not the idiomatic way. R is an array oriented language and as such we work on arrays. Under this paradigm it is better to initialize an array, and then populate it with its elements.

The implementation takes offset in a function that takes n as input and returns a list of the n first Fibonacci numbers.

It was run using the Rscript command

When the number is large enough R converts the list into floating point numbers. This conversion is mostly OK when working with probabilities, but can be fatal when we need the exact result. In above example the last element has been cropped. R reports 354224800000000000000 while the 100th number is 354224848179261915075.

The type conversion does not matter when visualizing. It won’t even mean a pixel when plotted on the screen, so we will continue on and try to visualize the relationship between $n$ and $fib(n)$.

The relationship looks much like an exponential development. This, however, is a subject for a later article.

# Conclusion

I implemented the recursive edition of the Fibonacci algorithm in R. This algorithm is efficient, and R provides efficient subroutines for handling the array.

After the implementation a simple plot of the relationship between $n$ and $fib(n)$ was plotted. This was easily done in R, as R provides good abstractions for this.

Image