mardi 18 décembre 2018

Middle Square Algorithm in R

I have been struggling with this for a while. I am trying to make a Middle Square Algorithm but I can't make it work for some reason. I am fairly new to R, so I'm not that confident in it yet.

seed=123456890
nxt=seed
find=as.character(nxt)
find=as.numeric(unlist(strsplit(find,"")))
max=length(find)
middle=ceiling(max/2)
partstart=middle-2
partend=middle+3
part=(partstart:partend)
part=as.numeric(paste(part,collapse=""))
gens=c(part)

for(i in 1:10){
nxt=part^2
find=as.character(nxt)
find=as.numeric(unlist(strsplit(find,"")))
max=length(find)
middle=ceiling(max/2)
partstart=middle-2
partend=middle+3
part=(partstart:partend)
part=as.numeric(paste(part,collapse=""))
gens=c(gens,part)
}
cat(gens)

When I run the code, this is the output:

345678 456789 456789 456789 456789 456789 456789 456789 456789 456789 456789




Aucun commentaire:

Enregistrer un commentaire