## Le Monde puzzle [49]

**H**ere is a quick-and-dirty solution to * Le Monde* puzzle posted a few days ago: the R code counts the number of winning tickets between 1 and N, and stops when there is a proportion of 10% of winning tickets.

#winning ticket win=function(n){ #decimal digits decomposition x=rep(0,4) x[4]=n%%10 m=(n-x[4])/10 x[3]=m%%10 m=(m-x[3])/10 x[2]=m%%10 m=(m-x[2])/10 x[1]=m%%10 tic=0 for (i in 1:3) tic=max(tic,(x[i]==1)*(x[(i+1):4]==3)) return(tic) } #number of winning tickets nwt=0 for (i in 1:9999){ nwt=nwt+win(i) if ((i>999)&&(10*nwt==i)) break()} #solution print(i)

**T**he (only) solution is therefore N=3500. (I am using this home-made decomposition of a number into its decimal digits, but there must be some function doing that in R already!)

February 20, 2017 at 9:28 pm

[…] enjoy reading about the Le Monde puzzles (and other topics!) at Christian Robert's blog. Recently he asked how to convert a number with s digits into a numerical vector where each element […]

January 30, 2011 at 11:14 am

[…] I spent most of my Saturday perusing R codes to check the answers written by my students to the R exam I gave two weeks ago… The outcome is mostly poor, even though some managed to solve a fair part of the long problem. Except for the few hopeless cases who visibly never wrote a single line of R code before the exam, all students have managed the basics of R programming and graphics, if not of Monte Carlo approximations or of boostrapping. One of the problems involved the distribution of a disk area and I found that half of the [third year math!] students do not know the formula! Although I had repeatedly told them about the good training in trying to solve Le Monde puzzles (as well as checking my posts about them), only one student found the solution to puzzle #49… […]

December 31, 2010 at 1:14 pm

[…] last puzzle of the year in Le Monde reads as follows (as far as I understand its wording!): Iter(n,x,y) is the […]

December 31, 2010 at 8:54 am

[…] Monde puzzle [52] The last puzzle of the year reads as follows (as far as I understand its wording!): Iter(n,x,y) is the […]

December 7, 2010 at 8:13 pm

Placing Digits in a Number into a Vector…I enjoy reading about the Le Monde puzzles (and other topics!) at Christian Robert’s blog. Recently he asked how to convert a number with s digits into a numerical vector where each element of the vector contains the corresponding digit (by place valu…

December 7, 2010 at 4:08 pm

You could always use:

toDigits <- function(n){

x = as.character(n)

d =as.numeric(strsplit(x, "")[[1]])

lin = length(d)

return(c(rep(0, 4-lin), d))

}

Thanks for posting these!

Greg

December 7, 2010 at 9:13 pm

Thanks, Greg!