Skip to content

Instantly share code, notes, and snippets.

@nwstephens
Created January 19, 2023 23:36
Show Gist options
  • Select an option

  • Save nwstephens/af1a26e6a1ff5bad26c0183bd2a295d0 to your computer and use it in GitHub Desktop.

Select an option

Save nwstephens/af1a26e6a1ff5bad26c0183bd2a295d0 to your computer and use it in GitHub Desktop.
This code can calculate extremely large iterations (easily up to 100,000), because it separates digits into elements in a vector.
# Palendromic numbers
x<-196
i<-0
# Add elements together
g<-function(v){
v<-c(v%/%10,0)+c(0,v%%10)
if(v[1]==0) v[-1] else v
}
# Iterate over a single number
v<-as.numeric(unlist(strsplit(as.character(x),"")))
while(!all(rev(v)==v)){
v<-v+rev(v)
v<-g(g(v))
i<-i+1
if(i==1000) break
}
paste(v,collapse="")
i
@nwstephens
Copy link
Author

nwstephens commented Jan 19, 2023

Example output when x<-196.

> paste(v,collapse="")
[1] "13230707591174868833157935148687217748665915944206067029754662915478919332899514526208329938166906224631499899437310235812940620783084083502558271614880460591565511444524168310104625839108892312824710639751740666477593824693602831832328889104753649100377143534511546609596508732618176619547948929792693931764191373310089041453227086628310138916255149993339099635202564689197705924405105568487037769504408513377695710867961422"
> i
[1] 1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment