Fibbonacci series and Golden Ratio

t1=0
t2=1
n=5
for(i in 1:n)
{
print(t1)
nextTerm= t1+t2
t1=t2
t2=nextTerm
}
I have the above code for generating the Fibbonacci series. I want to plot the golden ratio = 1.618 which would be next term divided by previous term.

What do you mean by plot? Do you have a certain shape or figure in mind?

Also, would you mind formatting the code using this forum's Markdown syntax? It makes it easier to read.

2 Likes

When plotting the division of (n+1/n) fibonacci numbers, it would eventually be a horizontal line to x axis and showing 1.618 on y axis.

First a methodological and then a practical observation:

  1. for is very expensive in resources for R and R is, at heart a language of functions on objects, unlike most programs that users have run across before of do this then do that.
  2. Don't reinvent the wheel, especially for classic problems. There's a package for that: https://cran.r-project.org/web/packages/numbers/numbers.pdf
library(numbers)
>F <-  fibonacci(5, sequence = TRUE)
[1] 1 1 2 3 5
> F[5]/F[4]
[1] 1.666667
1 Like

I didn't wanted to use the fibonacci function. I wanted to try replicating it using a set of codes without involving the use of any function.

I understand. I went through the same process of trying to make R work like C, Perl, Python, Ruby, etc. In other words to act like a nice, normal well behaved imperative/procedural language.

It wasn't until I took up Haskell, a functional language, did I appreciate that R is much alike. The vast majority of day to day work is feeding arguments to functions. There are the minimal features available for limited flow control, but those are mostly vestigial carryovers that are kept around for old time's sake and backward compatibility.

Linear algebra is the classic example. Apply a function to an n-dimensional matrix or apply endless loops. R is interpreted, in-memory and doesn't have great garbage collection. The functional use of the language tempers these drawbacks, while control structures beyond the minimal exaggerate them.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.