Le Monde puzzle [1]

January 10, 2011
By

(This article was first published on Xi'an's Og » R, and kindly contributed to R-bloggers)

Following the presentation of the first Le Monde puzzle of the year, I tried a simulated annealing solution on an early morning in my hotel room. Here is the R code, which is unfortunately too rudimentary and too slow to be able to tackle n=1000.

#minimise sum_{i=1}^I x_i
#for 1le x_ile 2n+1, 1e ile I
#    Ige n, x_i ne x_j
#    a=x_i,b=x_j,i,jin I implies a+b=x_k for a kin I
n=6
m=2*n+1

complete=function(inde){

 len=length(inde)
 comp=outer(inde,inde,"+")
 diag(comp)=inde
 comp=sort(unique(comp[comp<m+1]))

while ((length(comp)>len)&&(length(comp)<m)){
 inde=comp
 len=length(inde)
 comp=outer(inde,inde,"+")
 diag(comp)=inde
 comp=sort(unique(comp[comp<m+1]))
 }

 comp
 }

move=function(inde,tempe){

 ind=inde

 # movin

 if (length(ind)<m)

 off=sample(ind,1,prob=ind)
 inn=sample((1:m)[-ind],1)
 newinde=sort(c(ind[ind!=off],inn))
 newinde=complete(newinde)

 if (tempe*log(runif(1))<(sum(ind)-sum(newinde)))
 ind=newinde
 }

# prunnin

 if (length(ind)>n){

 off=sample(ind,1,prob=ind)
 newinde=sort(ind[ind!=off])
 newinde=complete(newinde)

 if (tempe*log(runif(1))<sum(ind)-sum(newinde))
 ind=newinde
 }

 ind
 }

T=10^4
fact=0.1
tpt=fact*seq(1,log(1+T),le=T)

inde=complete(sample(1:m,n))

recor=list(ind=inde,val=sum(inde))
for (t in 1:T){

 inde=move(inde,tpt[t])
 if (sum(inde)<recor$val){

 recor=list(ind=inde,val=sum(inde))
 }
 }

print(recor)

The solution to the puzzle (given in the next Le Monde issue) is to take only the even digits, resulting in a minimum sum equal to n(n+1).

Filed under: R, Statistics Tagged: Le Monde, mathematical puzzle, simulated annealing

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

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , ,

Comments are closed.

Sponsors

Mango solutions





RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



http://www.eoda.de









ODSC

CRC R books series













Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)