**Xi'an's Og » R**, and kindly contributed to R-bloggers)

**A** Le Monde mathematical puzzle that connects to my awalé post of last year:

For N≤18, N balls are placed in N consecutive holes. Two players, Alice and Bob, consecutively take two balls at a time provided those balls are in contiguous holes. The loser is left with orphaned balls. What is the values of N such that Bob can win, no matter what is Alice’s strategy?

**I** solved this puzzle by the following R code that works recursively on N by eliminating all possible adjacent pairs of balls and checking whether or not there is a winning strategy for the other player.

topA=function(awale){ # return 1 if current player can win, 0 otherwise best=0 if (max(awale[-1]*awale[-N])==1){ #there are adjacent balls remaining for (i in (1:(N-1))[awale[1:(N-1)]==1]){ if (awale[i+1]==1){ bwale=awale bwale[c language="(i,i+1)"][/c]=0 best=max(best,1-topA(bwale)) } }} return(best) } for (N in 2:18) print(topA(rep(1,N)))

which returns the solution

[1] 1 [1] 1 [1] 1 [1] 0 [1] 1 [1] 1 [1] 1 [1] 0 [1] 1 [1] 1 [1] 1 [1] 1 [1] 1 [1] 0 [1] 1 [1] 1 [1] 1 <pre>

(brute-force) answering the question that N=5,9,15 are the values where Alice has no winning strategy if Bob plays in an optimal manner**.** (The case N=5 is obvious as there always remains two adjacent 1′s once Alice removed any adjacent pair. The case N=9 can also be shown to be a lost cause by enumeration of Alice’s options.)

Filed under: Books, Kids, R Tagged: awalé, Le Monde, mathematical puzzle, R, recursive function

**leave a comment**for the author, please follow the link and comment on his blog:

**Xi'an's Og » R**.

R-bloggers.com offers

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