fibonacci: fibonacci

Description Usage Arguments Value Author(s) Examples

View source: R/pscript1.R

Description

Takes in an integer (n) and returns a vector containing the first n terms of the fibonacci sequence

Usage

1

Arguments

n

Number of terms of sequence to be returned in the vector

Value

Returns a vector containing the first n terms of the sequence specified by the input

Author(s)

Matt Heffernan, University of Illinois at Chicago

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
fibonacci(8)
Returns a vector of length 8 containing the first 8 terms of the fibonacci sequence (ie. 0, 1, 1, 2, 3, 5, 8, 13)
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
fibonacci <- function(n){
  a = 0
  b = 1
  count = 0
  sequence <- c()
  while(count < n)
  {
    sequence <- append(sequence, a)
    temp <- a
    a <- b
    b <- temp + b
    count <- count + 1
  }
  return(sequence)
}

mheffe3/MyFirstPackage documentation built on Dec. 21, 2021, 5:52 p.m.