Here you will find daily news and tutorials about R, contributed by over 750 bloggers.
There are many ways to follow us - By e-mail:On Facebook: If you are an R blogger yourself you are invited to add your own R content feed to this site (Non-English R bloggers should add themselves- here)

Find the largest number such that each of its internal digits is strictly less than the average of its two neighbours. Same question when all digits differ.

For instance, n=96433469 is such a number. When trying pure brute force (with the usual integer2digits function!)

le=solz=3
while (length(solz)>0){
solz=NULL
for (i in (10^(le+1)-1):(9*10^le+9)){
x=as.numeric(strsplit(as.character(i), "")[[1]])
if (min(x[-c(1,le+1)]<(x[-c(1,2)]+x[-c(le,le+1)])/2)==1){ print(i);solz=c(solz,i); break()}}
le=le+1}

this is actually the largest number returned by the R code. There is no solution with 9 digits. Adding an extra condition

le=solz=3
while (length(solz)>0){
solz=NULL
for (i in (10^(le+1)-1):(9*10^le+9)){
x=as.numeric(strsplit(as.character(i), "")[[1]])
if ((min(x[-c(1,le+1)]<(x[-c(1,2)]+x[-c(le,le+1)])/2)==1)&
(length(unique(x))==le+1)){ print(i);solz=c(solz,i); break()}}
le=le+1}

produces n=9520148 (seven digits) as the largest possible integer.

Related

To leave a comment for the author, please follow the link and comment on their blog: R – Xi'an's Og.