Hi,
I'd like to create a function fun(x) that returns a vector of Fibonacci numbers that are smaller or equal to x (assuming x is also a Fibonacci number). I've defined x as 21 so I would like to get back a vector containing numbers 1, 1, 2, 3, 5, 8, 13, 21. I've managed to write a code that looks like this:
> storage <- c()
> x <- 21
> storage[1] <- 1
> storage[2] <- 1
> fun <- function(x){
+ while(storage[2] <= x){
+ storage<-c(storage, storage[2])
+ temp <- storage[1] + storage[2]
+ storage[1] <- storage[2]
+ storage[2] <- temp
+ }
+ }
So I formed an empty vector and defined its first two values. Then, using while loop I tried to define the new vector and new values for storage[1] and [2] with a help of temporary (temp) variable. But the problem is that this code only gives me back a vector (1,1). What would be the easiest way to fix this? Thank you for your help!