R: Fibonacci Sequence

Flickr – Luca Postpischi


One one two, three five eight
Sounds so simple, nothing great
Thirteen, twenty-one, thirty-four
The hinges creak on an opening door
A repeating patter of the masters hand
Signing his work, the universal plan
Learn to look, the pattern’s plain to see
In the smile you flash, the dance of the honeybee
In the spirals of the pine cone and little acorn cap
In spiral arm galaxies and the ocean’s wave whitecap
In the swirl of the seashell, the air vortex of a wing
The hurricane’s eye and a thousand unseen things
Welcome to the mystery of the Greek letter phi
The measurement of beauty to the human eye
The golden ratio, one point six one eight
One, One, Two; Three, Five Eight

Benjamin Moon


It is actually very hard not to be familiar with the Fibonacci sequence. But we are not really going to go into details about its history and all, we just want to write a code in R which can produce the Fibonacci sequence up to the N-th element.

However, if you need to know more about the sequence, there are useful links at the end of the post.

Ok then, just to know what we are going to generate, we need a quick look at the Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ….

Basically, the first three elements are 0, 1 and 1 and after that every element in the sequence is the sum of the two previous numbers.

So, let’s have a quick look at the code:



#we want to create the Fibonnaci sequence up to the Nth element
#But, what is N? Let’s ask our esteemed user
N <- as.integer(readline(prompt = “Fella, up to what element do you wish to see the Fibonacci sequence?”))

#We first define the first two elements
Fibonacci[1] <- Fibonacci[2] <- 1

# # Now, we simply make the rest of the sequence by following the simple rule of the Fibonacci sequnce, i.e., every
# element is the sum of the previous two elements

#That calls for a for-loop!
for (i in 3:N){
Fibonacci[i] <- Fibonacci[i-2] + Fibonacci[i-1]

cat(“The first”, N, “elements of Fibonacci sequence are: “, “0”, Fibonacci, sep=” “)


For example, we use the code with N=10 :


Fella, up to what element do you wish to see the Fibonacci sequence? 10
The first 10 elements of Fibonacci sequence are: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610


If you understand it, then we are finished here. If not, a quick description of what the code does is as follows:

First things first, we ask the user to give us N (the N-th element in the sequence). Since the input is a character, we use as.integer function to turn it into an integer so that we can use it.

Then, we define the first two elements that we need to produce the next of the sequence, i.e, the two 1s at the very beginning of the sequence.

Now, every further element can be simply created just by adding the two previous ones. So, we need a for-loop. What does this loop do? We say that, for every i starting from 3, since we start creating the sequence from the 3rd element, up until the N-th element, create the i-th element by adding the i-1 and i-2 elements.

For example, when our script tries to calculate the 5th element, it add the 3rd (2) and the 4th (3) elements, which gives us 5. Simple as that.

Finally, to make a nice output, we use the CAT function (Concatenate and Print) to mix some default strings, along with N, and the created sequence.


Also watch



Further reading

Biography of Leonardo Pisano Fibonacci

What Is the Fibonacci Sequence?

Fibonacci number

Fibonacci Numbers and Nature