R/problem2.R

Defines functions fibNext fibStart fibSeq p2

Documented in fibNext fibSeq fibStart p2

#' Problem 2 - Even Fibonacci numbers
#'
#' @description
#' This is a solution to \href{https://projecteuler.net/problem=2}{Euler problem 2}.
#'
#' Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
#'
#'   1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
#'
#' By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
#'
#' @return a double vector
#'
#' @export
#' @examples
#' p2()
#'
p2 <- function() {
  x <- fibSeq(2, value = 4000000)
  sum(x[which(x %% 2 == 0)])
}

#' @param x an integer number at which to begin the Fibonacci sequence
#' @param lim a length limit of the Fibonacci sequence to return
#' @param value a maximum value of the Fibonacci sequence to calculate to
#'
#' @details
#' # FibSeq
#' This function does not discriminate against starting integers. This means any number may be input
#' as `x` and a Fibonacci-like sequence will be calculated from that starting point. The actual
#' Fibonacci sequence will only be generated by inputting a number actually in the Fibonacci sequence.
#'
#' @describeIn p2 a function to generate the Fibonacci sequence
#' @export
#' @examples
#' fibSeq(0)
#'
fibSeq <- function(x, lim = 100, value = 1000000) {
  out <- fibStart(x)
  x <- tail(out, 1)
  i <- 0

  while(i < lim & x < value) {
    x <- fibNext(x)
    i <- i + 1
    if(x < value) out <- append(out, x)
  }
  return(out)
}

#' @param x an integer number at which to begin the Fibonacci sequence
#'
#' @details
#' # FibStart
#' This function generates the first three numbers of the Fibonacci sequence as necessary (0, 1, 1).
#' This is necessary because these numbers can not be generated by utilizing the golden ratio (see
#' \link[projecteuler]{fibNext} for further detail). If given a number other than 0 or 1, this function
#' will simply return that number.
#'
#' @describeIn p2 a function to generate the beginning of the Fibonacci sequence
#' @export
#' @examples
#' fibStart(0)
#' fibStart(1)
#' fibStart(5)
#'
fibStart <- function(x) {
  if(x == 1) c(1, 1)
  else if(x == 0) c(0, 1, 1)
  else x
}

#' @param x an integer number for which the following number in the Fibonacci sequence will be generated
#'
#' \loadmathjax
#' @details
#' # FibNext
#' This function generates the next number of the Fibonacci sequence given a preceding number, `x`.
#' The ratio of any two consecutive entries in the Fibonacci sequence rapidly approaches \mjeqn{\varphi}{ascii}.
#' `x` is multipled by \mjeqn{\varphi}{ascii} and rounded to the nearest integer to obtain the next number in sequence.
#'
#' Note this will not give the correct next number if `x = 0` or `x = 1`.
#'
#' \mjdeqn{ \varphi=\frac{1+\sqrt{5}}2 }{ascii}
#'
#' @describeIn p2 a function to generate the next number in a Fibonacci sequence
#' @export
#' @examples
#' fibNext(3)
#' fibNext(34)
#'
fibNext <- function(x) {
  phi <- (1+sqrt(5))/2
  round(x*phi)
}
dylanrussellmd/projecteuler documentation built on Oct. 10, 2020, 3:34 p.m.