Created
January 19, 2023 23:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output when
x<-196.