Fibonacci vector in R
Builds the sequence into a preallocated vector rather than growing one.
fibonacci <- function(n) {
fib <- numeric(n)
fib[1:2] <- c(0, 1)
for (i in 3:n) {
fib[i] <- fib[i - 1] + fib[i - 2]
}
fib
}
Keywords and builtins used here
cforfunctioninnumeric
The run, in numbers
- Lines
- 8
- Characters to type
- 123
- Tokens
- 56
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 40 seconds.
Step 5 of 5 in Flow & functions, step 9 of 20 in Language basics.