I was trying to save all items in Fibonacci sequence till a given number n
. For example, if the function is fib
, my expected out would be
1 1 2
forfib(3)
1 1 2 3 5
forfib(5)
1 1 2 3 5 8 13 21
forfib(8)
so on and so forth.
My code is
fib <- function(n) {
if (n<= 2) {
return(1)
} else {
return(f(n-1)+f(n-2))
}
}
but it only gives a single value for the n
-th elements in the Fibonacci sequence.
Any clue to save the elements from 1
to n
as the output of fib(n)
? Thank you!