In mathematics and computer science, the RecamĂ¡n’s sequence (or Recaman’s sequence) is a well known sequence defined by a recurrence relation, because its elements are related to the previous elements in a straightforward way, they are often defined using recursion.
It takes its name after its inventor Bernardo RecamĂ¡n Santos (BogotĂ¡, August 5, 1954), a Colombian mathematician.
(taken from Wikipedia: https://en.wikipedia.org/wiki/Recam%C3%A1n%27s_sequence)
Numberphile (youtube channel) challenged viewers to replicate Recaman Sequence graph, and have the output be:
Video can be found : https://www.youtube.com/watch?v=FGC5TdIiT9U Challenge is located in min 4 sec 30 until min 4 sec 41
funcionSecuenciaRecaman1<-function(numero) {
x<- vector()
y<- vector()
z<-0
i <- 1
while(i <= numero)
{
x[i] <- z
if(i == 1){
y[i] <- z
} else {
if(y[i-1] - x[i] > 0 & is.na(match(y[i-1] - x[i],y))) {
y[i] <- y[i-1] - x[i]
} else {
y[i] <- y[i-1] + x[i]
}
}
z<- z+1
i = i+1
}
return(cbind(x,y))
}
Let’s test it out:
funcionSecuenciaRecaman1(25)
## x y
## [1,] 0 0
## [2,] 1 1
## [3,] 2 3
## [4,] 3 6
## [5,] 4 2
## [6,] 5 7
## [7,] 6 13
## [8,] 7 20
## [9,] 8 12
## [10,] 9 21
## [11,] 10 11
## [12,] 11 22
## [13,] 12 10
## [14,] 13 23
## [15,] 14 9
## [16,] 15 24
## [17,] 16 8
## [18,] 17 25
## [19,] 18 43
## [20,] 19 62
## [21,] 20 42
## [22,] 21 63
## [23,] 22 41
## [24,] 23 18
## [25,] 24 42
plottingChallenge<-function(numeral){
#Recaman Sequence: (only retrieving values)
laSecuencia <-funcionSecuenciaRecaman1(numeral)[,2]
#declarandoVectoresNecesarios
puntosEnx <- vector()
puntosEny <- vector()
ciclo <- vector()
degrees <- c(1:179) #to simulate semi-circle we will need degrees from 1 : 179
numeral2 <- numeral - 1
#Comienza el Ciclo
for(i in 1:numeral2)
{
##These are the edges of the semi-circle (diameter of circle)
x1<- laSecuencia[i]
x2<- laSecuencia[i+1]
##Evaluating whether semi-circle will curve upwards or downwards
if(i%%2 == 0){curvaturaHaciaArriba <- 1} else {curvaturaHaciaArriba <- -1}
radio <- abs(x2 - x1)/2 #radius of circle
if(x2>x1){puntoMedio <- x1+radio}else{puntoMedio <- x2+radio }
alturas<- sin(degrees*pi/180)*radio*curvaturaHaciaArriba #all heights
distancias <- cos(degrees*pi/180)*radio #all lengths
if(x2>x1){distancias1 <- sort(distancias,decreasing = F)
} else {distancias1 <- distancias}
puntosEnx <- c(puntosEnx,x1,distancias1 + puntoMedio,x2)
puntosEny <- c(puntosEny,0,alturas,0)
}
matriz<-cbind(puntosEnx,puntosEny)
matrizUnica<-unique(matriz)
par(bg="black")
plot(matrizUnica[,1],matrizUnica[,2], type = "l",
main = "Secuencia de Recaman",
xlab = "Seq. Recaman",
ylab = "",
col="white", #color del grafico
#col.axis = "white", #estos son los numeritos,
col.axis = "yellow", #titulos de los axis
cex.lab = 1
)
points( tail(matrizUnica[,1],1), tail(matrizUnica[,2],1), pch = 11, col = "yellow" )
title("Recaman Sequence", col.main = "white")
}
Testing it out:
plottingChallenge(62)