Values chosen for n, in print(n=48) and plot(n=20) are for aesthetic
reasons.
# load packages
pacman::p_load(pacman,tidyverse,GGally,ggthemes,ggplot2,ggvis,httr,plotly,rio,rmarkdown,shiny)
# Generate Fibonacci Sequence
fibonacci <- function(n) {
if (n <= 2) {
# return(rep(1, n))
} else {
sequence <- c(1, 1)
for (i in 3:n) {
next.val <- sequence[i-1] + sequence[i-2]
sequence <- c(sequence, next.val)
}
return(sequence)
}
}
# Number of iterations you want for print
n <- 48
fib_sequence <- fibonacci(n)
# Print Sequence
x <- fib_sequence
print(x)
## [1] 1 1 2 3 5 8
## [7] 13 21 34 55 89 144
## [13] 233 377 610 987 1597 2584
## [19] 4181 6765 10946 17711 28657 46368
## [25] 75025 121393 196418 317811 514229 832040
## [31] 1346269 2178309 3524578 5702887 9227465 14930352
## [37] 24157817 39088169 63245986 102334155 165580141 267914296
## [43] 433494437 701408733 1134903170 1836311903 2971215073 4807526976
# Number of iterations you want for chart
n <- 20
fib_sequence <- fibonacci(n)
# Plot the sequence
plot(fib_sequence, type="o", col="red", main="Fibonacci Sequence", xlab="Iteration", ylab="Value", xaxt="n")
axis(1, at=1:n, labels=1:n)
