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:
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!
Advanced R is a great free book, for taking your R to the next level. https://adv-r.hadley.nz/function-operators.html
Fibonnaci is used as an example of a function that can benefit from memoisation, even ignoring that though, the implementation is very elegant.
I see though that this doesn't directly answer your question as you asked for a vector and not simply the number. I'll get back to this when I have some time.
edit note, i dropped the equal sign in the while loop condition so on checking and getting the tail value equal to the desired x the while loop wont run (before doing this it would have calculated the next value after 21