I'm writing a short program in R that generates a Fibonacci sequence based upon two start numbers and a stop number. I decided to use recursion rather than a for loop in order to challenge myself and learn a bit more. However, I need to generate a vector containing each of the numbers of the sequence I generate. I created an empty vector at the start of the program to house the sequence. the issue is that, since the program is recursive, that vector is reset to be empty each time the function is called. I was hoping the internet had some ideas as to how I could keep using recursion, but not reset to an empty vector.
Code:
rec <- function (startN1, startN2, stopN2){
#the Fibonacci sequence is generate by starting with two numbers, adding them to generate a third.
# To continue generating numbers, you add the previous two values. Like so: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
#
# startN1 is the first number you add, startN2 is the second, and stopN2 is the number before which you stop counting.
fibVals <-vector(mode = "numeric", length = 0) #fibVals is the vector of Fibonacci sequence numbers
if (startN2 < stopN2) { #checks to make sure the stop number has not been exceeded.
s <- sum(startN1, startN2) #generates the next number in the sequence
fibVals <- append(fibVals, s) #appends the new number to the Fibonacci Sequence vector
rec(startN2, s, stopN2) #recursive call
} #end if statement
else{
print(fibVals) #prints the Fibonnaci sequence the code (should) generate
}#end else statement
} #end function
You can define the fibVals vector in the definition of rec and the pass the current value of fibVals to the next recursion call, e.g.:
rec <- function (startN1, startN2, stopN2, fibVals=vector(mode = "numeric", length = 0)){
#the Fibonacci sequence is generate by starting with two numbers, adding them to generate a third.
# To continue generating numbers, you add the previous two values. Like so: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
#
# startN1 is the first number you add, startN2 is the second, and stopN2 is the number before which you stop counting.
#fibVals <-vector(mode = "numeric", length = 0) #fibVals is the vector of Fibonacci sequence numbers
if (startN2 < stopN2) { #checks to make sure the stop number has not been exceeded.
s <- sum(startN1, startN2) #generates the next number in the sequence
fibVals <- append(fibVals, s) #appends the new number to the Fibonacci Sequence vector
rec(startN2, s,stopN2,fibVals=fibVals) #recursive call
} #end if statement
else{
print(fibVals) #prints the Fibonnaci sequence the code (should) generate
}#end else statement
} #end function
> rec(1,1,100)
[1] 2 3 5 8 13 21 34 55 89 144
Somewhat more compact version of your approach:
rec2 <- function(fibs,stopN2) {
n <- length(fibs)
if (fibs[n] < stopN2) {
next.n <- sum(fibs[(n-1):n])
rec2(append(fibs,next.n), stopN2=stopN2)
} else
fibs
}
> rec2(c(1,1),100)
[1] 1 1 2 3 5 8 13 21 34 55 89 144
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With