amusingly I asked the Artificial Intelligence chatbot over at ChatGPT (openai.com)
function to generate fibonacci sequence from 1 to n , in R ?
and it answered with the following
# define a function that generates the Fibonacci sequence
fibonacci <- function(n) {
# define the starting values
a <- 0
b <- 1
# check if the number of terms is 0 or 1
if (n == 0) {
return(a)
} else if (n == 1) {
return(c(a, b))
} else {
# generate the Fibonacci sequence
seq <- c(a, b)
for (i in 2:n) {
c <- a + b
seq <- c(seq, c)
a <- b
b <- c
}
return(seq)
}
}
# test the function with different values of n
print(fibonacci(0)) # 0
print(fibonacci(1)) # 0, 1
print(fibonacci(2)) # 0, 1, 1
print(fibonacci(3)) # 0, 1, 1, 2
print(fibonacci(4)) # 0, 1, 1, 2, 3