Hi everyone,
I was told to write a code for the fibonacci number and then find the dividend of the consecutive numbers to eventually find the golden ratio.
My code is:
fibonacci <- function(n)
{
if(n==1)
x<- 1
if(n==2)
x<- c(1,1)
if(n>2)
{
x<- fibonacci(2)
for(i in 3:n)
x[i] <- x[i-1]+x[i-2]
y <- x[i-1]
z <- x[i-2]
x[i] <- y/z
}
return(x)
}
When I do fibonacci (10), it gives me:
(1, 1, 2, 3, 5, 8, 13, 21, 34, 1.61904761904762)
I do not understand why it only gives me the dividend of the 9th and 10th number. Could someone explain to me why?
Thank you for your time in advance.