So.. I decided to create a function for the Fibonacci sequance and woohoo made it!
I'll share it here because, well, im kinda happy for it but also i have some question.
If you want to take on the challenge and create a functuin (or a loop) to make the Fibonacci seq+Golden ratio, this part is SPOILER
fibonacci <- function(x){
for(s in 1){
a <- c(0,1)
gold <- c(0,1)
x <- x-2
for (i in 1:x) {
a[i+2] <- sum(a[i],a[i+1])
gold[i+2] <- a[i+1]/a[i]
}
}
return(data.frame(Fibonacci=a,"Golden Ratio"=gold))
}
# Example fibonacci(5)
*I actually realised i didnt have to use the first loop. Ill leave it anyway here since its my final version of the function WITHOUT seeing others
My questions are:
-
When i used tibble instead of data.frame it round the numbers to 2 decimals only. can i fix this?
-
even in a data.frame format, and actually in all R calculations, it didnt calculate a more accurate number than 1.618034. can it be fixed?
-
Another issue that i fixed by using x = x-2 is that i cant figure out a kneet way to tell R to run only till x, because i already created the first two numbers in the sequance.