R/recurseq1.R

Defines functions recurseq1

Documented in recurseq1

#' Recursive sequence
#'
#' This takes as input a vector x containing the first three numeric elements of this
#' sequence and a positive integer n denoting the final nth element of the
#' sequence to calculate. The function will return element n.
#'
#' @param x vector
#' @param n integer
#'
#' @return element n
#' @export recurseq1
#'
#' @examples recurseq1(x = c(2, 3, 3), n = 3)
recurseq1 <- function(x, n) {
  stopifnot(n > 0 & length(x) == 3)
  calcn_v <- vector("numeric", length = n)
  for (i in seq_along(calcn_v)) {
    if(i <= 3){
      calcn_v[i] <- x[i]
    }else{
      calcn_v[i] <- calcn_v[i-1] + ((calcn_v[i-3] - calcn_v[i-2])/n)
    }
  }
  return(calcn_v[i])
}
STAT-413-613-21S/hw04paevans872 documentation built on Feb. 28, 2021, 12:12 a.m.