Site icon R-bloggers

Le Monde puzzle [#1075]

[This article was first published on R – Xi'an's Og, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A new Le Monde mathematical puzzle in the digit category:

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.

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

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.