# Le Monde puzzle [#1049]

By xi’an

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

An algorithmic Le Monde mathematical puzzle with a direct

Alice and Bob play a game by picking alternatively one of the remaining digits between 1 and 10 and putting it in either one of two available stacks, 1 or 2. Their respective gains are the products of the piles (1 for Alice and 2 for Bob).

The problem is manageable by a recursive function

```facten=factorial(10)
pick=function(play=1,remz=matrix(0,2,5)){
if (sum(remz==0)==1){#finale
remz[remz==0]=(1:10)[!(1:10)%in%remz]
return(prod(remz[play,]))
}else{
gainz=0
if (min(remz[1,])==0){
for (i in (1:10)[!(1:10)%in%remz]){
propz=rbind(c(remz[1,remz[1,]>0],i,
rep(0,sum(remz[1,]==0)-1)),remz[2,])
gainz=max(gainz,facten/pick(3-play,remz=propz))}}
if (min(remz[2,])==0){
for (i in (1:10)[remz==0]){
propz=rbind(remz[1,],c(remz[2,remz[2,]>0],i,
rep(0,sum(remz[2,]==0)-1)))
gainz=max(gainz,facten/pick(3-play,remz=propz))}}
return(gainz)}}
```

that shows the optimal gain for Alice is 6480=3x5x6x8x9, versus Bob getting 1680=1x2x4x7x10. The moves ensuring the gain are 2-10-…