## Fibonacci numbers ## Plot Fibonacci spiral library(shape) ## we need it for plotting ## Use install.packages("shape") if your R version does not know the ## shape package ## Function to produce the spiral plot plot.Fib <- function(x) { plot(-max(x):max(x), -max(x):max(x), type="n", xlab="", ylab="", main="Fibonacchi spiral") # empty plot x.coor <- c(0,0, cumsum(x*rep(c(1,0,-1,0), length=length(x)))) # x-coordinate for center of circle y.coor <- c(0,0, cumsum(x*rep(c(0,1,0,-1), length=length(x)))) # y-coordinate for center " " a1 <- 0:length(x)*pi/2 #angle start a2 <- 1:(length(x)+1)*p/2 #angle end for (i in 1:length(x)) { plotcircle(r = x[i], mid = c(x.coor[i],y.coor[i]), from = a1[i], to = a2[i]) # plot the quartercircles } } # Not-so-efficient function to generate Fibonacci numbers Fibonacci <- function(n) { if (!is.finite(n) || n < 1) stop("'n' must be number >= 1") if (n==1) { x <- 0 } else { x <- c(0,1) while (length(x) < n) { k <- length(x) x[k+1] <- x[k] + x[k-1] } } # end else plot.Fib(x) # finally call the plot function defined above x # and return the full series }