Hi, and welcome to community.rstudio.com! It sounds like this might be a homework question. If it is, please see our homework policy.
To give you a gentle nudge in the right direction, what happens if you run fibonacci(3)
? For n=3, I would expect the sequence 1 1 2
, but I get 1 1 1
. Similarly, fibonacci(4)
returns 1 1 2 2
, not the expected 1 1 2 3
. Do you see a pattern? You should start by re-thinking the line of code that deals with y/z
. Perhaps instead of overwriting x[i]
the division should be handled separately.
I hope this is helpful!